Skip to content

Commit 33de68c

Browse files
committed
add set_screen_orientation_override command
1 parent 189f556 commit 33de68c

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

py/selenium/webdriver/common/bidi/emulation.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,70 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18+
from enum import Enum
1819
from typing import Any, Optional, Union
1920

2021
from selenium.webdriver.common.bidi.common import command_builder
2122

2223

24+
class ScreenOrientationNatural(Enum):
25+
"""Natural screen orientation."""
26+
27+
PORTRAIT = "portrait"
28+
LANDSCAPE = "landscape"
29+
30+
31+
class ScreenOrientationType(Enum):
32+
"""Screen orientation type."""
33+
34+
PORTRAIT_PRIMARY = "portrait-primary"
35+
PORTRAIT_SECONDARY = "portrait-secondary"
36+
LANDSCAPE_PRIMARY = "landscape-primary"
37+
LANDSCAPE_SECONDARY = "landscape-secondary"
38+
39+
40+
class ScreenOrientation:
41+
"""Represents screen orientation configuration."""
42+
43+
def __init__(
44+
self,
45+
natural: Union[ScreenOrientationNatural, str],
46+
type: Union[ScreenOrientationType, str],
47+
):
48+
"""Initialize ScreenOrientation.
49+
50+
Args:
51+
natural: Natural screen orientation ("portrait" or "landscape").
52+
type: Screen orientation type ("portrait-primary", "portrait-secondary",
53+
"landscape-primary", or "landscape-secondary").
54+
55+
Raises:
56+
ValueError: If natural or type values are invalid.
57+
"""
58+
# Convert strings to enums if needed
59+
if isinstance(natural, str):
60+
try:
61+
self.natural = ScreenOrientationNatural(natural)
62+
except ValueError:
63+
raise ValueError(f"Invalid natural orientation: {natural}")
64+
else:
65+
self.natural = natural
66+
67+
if isinstance(type, str):
68+
try:
69+
self.type = ScreenOrientationType(type)
70+
except ValueError:
71+
raise ValueError(f"Invalid orientation type: {type}")
72+
else:
73+
self.type = type
74+
75+
def to_dict(self) -> dict[str, str]:
76+
return {
77+
"natural": self.natural.value,
78+
"type": self.type.value,
79+
}
80+
81+
2382
class GeolocationCoordinates:
2483
"""Represents geolocation coordinates."""
2584

@@ -312,3 +371,37 @@ def set_scripting_enabled(
312371
params["userContexts"] = user_contexts
313372

314373
self.conn.execute(command_builder("emulation.setScriptingEnabled", params))
374+
375+
def set_screen_orientation_override(
376+
self,
377+
screen_orientation: Optional[ScreenOrientation] = None,
378+
contexts: Optional[list[str]] = None,
379+
user_contexts: Optional[list[str]] = None,
380+
) -> None:
381+
"""Set screen orientation override for the given contexts or user contexts.
382+
383+
Args:
384+
screen_orientation: ScreenOrientation object to emulate, or None to clear the override.
385+
contexts: List of browsing context IDs to apply the override to.
386+
user_contexts: List of user context IDs to apply the override to.
387+
388+
Raises:
389+
ValueError: If both contexts and user_contexts are provided, or if neither
390+
contexts nor user_contexts are provided.
391+
"""
392+
if contexts is not None and user_contexts is not None:
393+
raise ValueError("Cannot specify both contexts and userContexts")
394+
395+
if contexts is None and user_contexts is None:
396+
raise ValueError("Must specify either contexts or userContexts")
397+
398+
params: dict[str, Any] = {
399+
"screenOrientation": screen_orientation.to_dict() if screen_orientation is not None else None
400+
}
401+
402+
if contexts is not None:
403+
params["contexts"] = contexts
404+
elif user_contexts is not None:
405+
params["userContexts"] = user_contexts
406+
407+
self.conn.execute(command_builder("emulation.setScreenOrientationOverride", params))

0 commit comments

Comments
 (0)