1818from typing import Any , Callable , Dict , List , Optional , Tuple , Union
1919
2020from selenium import webdriver
21- from selenium .common .exceptions import InvalidArgumentException
21+ from selenium .common .exceptions import InvalidArgumentException , WebDriverException
2222from selenium .webdriver .common .by import By
2323from selenium .webdriver .remote .command import Command as RemoteCommand
2424from selenium .webdriver .remote .remote_connection import RemoteConnection
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
8686def _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' )
0 commit comments