|
15 | 15 | # specific language governing permissions and limitations |
16 | 16 | # under the License. |
17 | 17 |
|
| 18 | +from enum import Enum |
18 | 19 | from typing import Any, Optional, Union |
19 | 20 |
|
20 | 21 | from selenium.webdriver.common.bidi.common import command_builder |
21 | 22 |
|
22 | 23 |
|
| 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 | + |
23 | 82 | class GeolocationCoordinates: |
24 | 83 | """Represents geolocation coordinates.""" |
25 | 84 |
|
@@ -312,3 +371,37 @@ def set_scripting_enabled( |
312 | 371 | params["userContexts"] = user_contexts |
313 | 372 |
|
314 | 373 | 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