Skip to content

Commit 09a0cd0

Browse files
authored
feat: add non-w3c but still need commands (appium#701)
* add non-w3c but still need commands * fix id as $
1 parent 56ce5b0 commit 09a0cd0

8 files changed

Lines changed: 66 additions & 18 deletions

File tree

Pipfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ verify_ssl = true
77
pre-commit = "~=2.17"
88

99
[packages]
10-
selenium = "~=4.0.0"
10+
selenium = "~=4.1.3"
1111

1212
black = "==22.1.0"
1313

appium/webdriver/extensions/action_helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,10 @@ def tap(self, positions: List[Tuple[int, int]], duration: Optional[int] = None)
126126
x = position[0]
127127
y = position[1]
128128

129-
# https://github.com/SeleniumHQ/selenium/blob/64447d4b03f6986337d1ca8d8b6476653570bcc1/py/selenium/webdriver/common/actions/pointer_input.py#L24
129+
# https://github.com/SeleniumHQ/selenium/blob/trunk/py/selenium/webdriver/common/actions/pointer_input.py
130130
new_input = actions.w3c_actions.add_pointer_input('touch', f'finger{finger}')
131131
new_input.create_pointer_move(x=x, y=y)
132-
new_input.create_pointer_down(MouseButton.LEFT)
132+
new_input.create_pointer_down(button=MouseButton.LEFT)
133133
if duration:
134134
new_input.create_pause(duration / 1000)
135135
else:

appium/webdriver/extensions/ime.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def available_ime_engines(self) -> List[str]:
3434
Returns:
3535
:obj:`list` of :obj:`str`: The available input methods for an Android device
3636
"""
37-
return self.execute(Command.GET_AVAILABLE_IME_ENGINES, {})['value']
37+
return self.execute(Command.GET_AVAILABLE_IME_ENGINES, {}).get('value', [])
3838

3939
def is_ime_active(self) -> bool:
4040
"""Checks whether the device has IME service active.
@@ -43,7 +43,7 @@ def is_ime_active(self) -> bool:
4343
Returns:
4444
`True` if IME service is active
4545
"""
46-
return self.execute(Command.IS_IME_ACTIVE, {})['value']
46+
return self.execute(Command.IS_IME_ACTIVE, {}).get('value', False)
4747

4848
def activate_ime_engine(self, engine: str) -> 'WebDriver':
4949
"""Activates the given IME engine on the device.
@@ -82,9 +82,10 @@ def active_ime_engine(self) -> str:
8282
Returns:
8383
str: The activity and package of the currently active IME engine
8484
"""
85-
return self.execute(Command.GET_ACTIVE_IME_ENGINE, {})['value']
85+
return self.execute(Command.GET_ACTIVE_IME_ENGINE, {}).get('value', '')
8686

8787
def _add_commands(self) -> None:
88+
"""Add IME commands. They are not in W3C spec."""
8889
# noinspection PyProtectedMember,PyUnresolvedReferences
8990
commands = self.command_executor._commands
9091
commands[Command.GET_AVAILABLE_IME_ENGINES] = (

appium/webdriver/extensions/location.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ def set_location(
3939
self,
4040
latitude: Union[float, str],
4141
longitude: Union[float, str],
42-
altitude: Union[float, str] = None,
43-
speed: Union[float, str] = None,
44-
satellites: Union[float, str] = None,
42+
altitude: Union[float, str, None] = None,
43+
speed: Union[float, str, None] = None,
44+
satellites: Union[float, str, None] = None,
4545
) -> 'WebDriver':
4646
"""Set the location of the device
4747
@@ -80,9 +80,10 @@ def location(self) -> Dict[str, float]:
8080
- longitude (float)
8181
- altitude (float)
8282
"""
83-
return self.execute(Command.GET_LOCATION)['value']
83+
return self.execute(Command.GET_LOCATION).get('value', {})
8484

8585
def _add_commands(self) -> None:
86+
"""Add location endpoints. They are not int w3c spec."""
8687
# noinspection PyProtectedMember,PyUnresolvedReferences
8788
commands = self.command_executor._commands
8889
commands[Command.TOGGLE_LOCATION_SERVICES] = (

appium/webdriver/mobilecommand.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ class MobileCommand:
1818
GET_SESSION = 'getSession'
1919
GET_ALL_SESSIONS = 'getAllSessions'
2020

21+
## MJSONWP for Selenium v4
2122
GET_LOCATION = 'getLocation'
2223
SET_LOCATION = 'setLocation'
2324

25+
## MJSONWP for Selenium v4
2426
GET_AVAILABLE_IME_ENGINES = 'getAvailableIMEEngines'
2527
IS_IME_ACTIVE = 'isIMEActive'
2628
ACTIVATE_IME_ENGINE = 'activateIMEEngine'
@@ -80,6 +82,16 @@ class MobileCommand:
8082
GET_EVENTS = 'getLogEvents'
8183
LOG_EVENT = 'logCustomEvent'
8284

85+
## MJSONWP for Selenium v4
86+
IS_ELEMENT_DISPLAYED = 'isElementDisplayed'
87+
GET_CAPABILITIES = 'getCapabilities'
88+
GET_SCREEN_ORIENTATION = 'getScreenOrientation'
89+
SET_SCREEN_ORIENTATION = 'setScreenOrientation'
90+
91+
# To override selenium commands
92+
GET_LOG = 'getLog'
93+
GET_AVAILABLE_LOG_TYPES = 'getAvailableLogTypes'
94+
8395
# Android
8496
OPEN_NOTIFICATIONS = 'openNotifications'
8597
START_ACTIVITY = 'startActivity'
@@ -107,7 +119,3 @@ class MobileCommand:
107119
# iOS
108120
TOUCH_ID = 'touchId'
109121
TOGGLE_TOUCH_ID_ENROLLMENT = 'toggleTouchIdEnrollment'
110-
111-
# To override selenium commands
112-
GET_LOG = 'getLog'
113-
GET_AVAILABLE_LOG_TYPES = 'getAvailableLogTypes'

appium/webdriver/webdriver.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
1919

2020
from selenium import webdriver
21-
from selenium.common.exceptions import InvalidArgumentException
21+
from selenium.common.exceptions import InvalidArgumentException, WebDriverException
2222
from selenium.webdriver.common.by import By
2323
from selenium.webdriver.remote.command import Command as RemoteCommand
2424
from selenium.webdriver.remote.remote_connection import RemoteConnection
@@ -80,7 +80,7 @@
8080
_EXTENSION_CAPABILITY = ':'
8181

8282
# override
83-
# Add appium prefix for the non-W3C capabilities
83+
# Add appium prefix for the MJSONWP capabilities
8484

8585

8686
def _make_w3c_caps(caps: Dict) -> Dict[str, Union[Dict[str, Any], List[Dict[str, Any]]]]:
@@ -491,6 +491,35 @@ def switch_to(self) -> MobileSwitchTo:
491491

492492
return MobileSwitchTo(self)
493493

494+
# MJSONWP for Selenium v4
495+
@property
496+
def orientation(self) -> str:
497+
"""
498+
Gets the current orientation of the device
499+
:Usage:
500+
::
501+
orientation = driver.orientation
502+
"""
503+
return self.execute(Command.GET_SCREEN_ORIENTATION)['value']
504+
505+
# MJSONWP for Selenium v4
506+
@orientation.setter
507+
def orientation(self, value: str) -> None:
508+
"""
509+
Sets the current orientation of the device
510+
:Args:
511+
- value: orientation to set it to.
512+
:Usage:
513+
::
514+
driver.orientation = 'landscape'
515+
"""
516+
allowed_values = ['LANDSCAPE', 'PORTRAIT']
517+
if value.upper() in allowed_values:
518+
self.execute(Command.SET_SCREEN_ORIENTATION, {'orientation': value})
519+
else:
520+
521+
raise WebDriverException("You can only set the orientation to 'LANDSCAPE' and 'PORTRAIT'")
522+
494523
def _add_commands(self) -> None:
495524
# call the overridden command binders from all mixin classes except for
496525
# appium.webdriver.webdriver.WebDriver and its sub-classes
@@ -504,6 +533,7 @@ def _add_commands(self) -> None:
504533
# noinspection PyProtectedMember,PyUnresolvedReferences
505534
commands = self.command_executor._commands
506535

536+
# FIXME: remove after a while as MJSONWP
507537
commands[Command.TOUCH_ACTION] = ('POST', '/session/$sessionId/touch/perform')
508538
commands[Command.MULTI_ACTION] = ('POST', '/session/$sessionId/touch/multi/perform')
509539
commands[Command.SET_IMMEDIATE_VALUE] = (
@@ -522,7 +552,15 @@ def _add_commands(self) -> None:
522552
'/session/$sessionId/element/$id/location_in_view',
523553
)
524554

555+
## MJSONWP for Selenium v4
556+
commands[Command.IS_ELEMENT_DISPLAYED] = ('GET', '/session/$sessionId/element/$id/displayed')
557+
commands[Command.GET_CAPABILITIES] = ('GET', '/session/$sessionId')
558+
559+
commands[Command.GET_SCREEN_ORIENTATION] = ('GET', '/session/$sessionId/orientation')
560+
commands[Command.SET_SCREEN_ORIENTATION] = ('POST', '/session/$sessionId/orientation')
561+
525562
# override for Appium 1.x
526563
# Appium 2.0 and Appium 1.22 work with `/se/log` and `/se/log/types`
564+
# FIXME: remove after a while
527565
commands[Command.GET_LOG] = ('POST', '/session/$sessionId/log')
528566
commands[Command.GET_AVAILABLE_LOG_TYPES] = ('GET', '/session/$sessionId/log/types')

appium/webdriver/webelement.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def is_displayed(self) -> bool:
7878
7979
Override for Appium
8080
"""
81-
return self._execute(RemoteCommand.IS_ELEMENT_DISPLAYED)['value']
81+
return self._execute(Command.IS_ELEMENT_DISPLAYED)['value']
8282

8383
def find_element(self, by: str = AppiumBy.ID, value: Union[str, Dict] = None) -> 'WebElement':
8484
"""Find an element given a AppiumBy strategy and locator

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,5 @@
4848
'Topic :: Software Development :: Quality Assurance',
4949
'Topic :: Software Development :: Testing',
5050
],
51-
install_requires=['selenium ~= 4.0.0'],
51+
install_requires=['selenium ~= 4.1.3'],
5252
)

0 commit comments

Comments
 (0)