Skip to content

Commit ef0492a

Browse files
committed
add tests
1 parent 33de68c commit ef0492a

File tree

1 file changed

+98
-1
lines changed

1 file changed

+98
-1
lines changed

py/test/selenium/webdriver/common/bidi_emulation_tests.py

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@
1616
# under the License.
1717
import pytest
1818

19-
from selenium.webdriver.common.bidi.emulation import Emulation, GeolocationCoordinates, GeolocationPositionError
19+
from selenium.webdriver.common.bidi.emulation import (
20+
Emulation,
21+
GeolocationCoordinates,
22+
GeolocationPositionError,
23+
ScreenOrientation,
24+
ScreenOrientationNatural,
25+
ScreenOrientationType,
26+
)
2027
from selenium.webdriver.common.bidi.permissions import PermissionState
2128
from selenium.webdriver.common.window import WindowTypes
2229

@@ -73,6 +80,24 @@ def get_browser_locale(driver):
7380
return result.result["value"]
7481

7582

83+
def get_screen_orientation(driver, context_id):
84+
result = driver.script._evaluate(
85+
"screen.orientation.type",
86+
{"context": context_id},
87+
await_promise=False,
88+
)
89+
orientation_type = result.result["value"]
90+
91+
result = driver.script._evaluate(
92+
"screen.orientation.angle",
93+
{"context": context_id},
94+
await_promise=False,
95+
)
96+
orientation_angle = result.result["value"]
97+
98+
return {"type": orientation_type, "angle": orientation_angle}
99+
100+
76101
def test_emulation_initialized(driver):
77102
assert driver.emulation is not None
78103
assert isinstance(driver.emulation, Emulation)
@@ -419,3 +444,75 @@ def test_set_scripting_enabled_with_user_contexts(driver, pages):
419444
driver.browsing_context.close(context_id)
420445
finally:
421446
driver.browser.remove_user_context(user_context)
447+
448+
449+
def test_set_screen_orientation_override_with_contexts(driver, pages):
450+
context_id = driver.current_window_handle
451+
initial_orientation = get_screen_orientation(driver, context_id)
452+
453+
# Set landscape-primary orientation
454+
orientation = ScreenOrientation(
455+
natural=ScreenOrientationNatural.LANDSCAPE,
456+
type=ScreenOrientationType.LANDSCAPE_PRIMARY,
457+
)
458+
driver.emulation.set_screen_orientation_override(screen_orientation=orientation, contexts=[context_id])
459+
460+
url = pages.url("formPage.html")
461+
driver.browsing_context.navigate(context_id, url, wait="complete")
462+
463+
# Verify the orientation was set
464+
current_orientation = get_screen_orientation(driver, context_id)
465+
assert current_orientation["type"] == "landscape-primary", f"Expected landscape-primary, got {current_orientation}"
466+
assert current_orientation["angle"] == 0, f"Expected angle 0, got {current_orientation['angle']}"
467+
468+
# Set portrait-secondary orientation
469+
orientation = ScreenOrientation(
470+
natural=ScreenOrientationNatural.PORTRAIT,
471+
type=ScreenOrientationType.PORTRAIT_SECONDARY,
472+
)
473+
driver.emulation.set_screen_orientation_override(screen_orientation=orientation, contexts=[context_id])
474+
475+
# Verify the orientation was changed
476+
current_orientation = get_screen_orientation(driver, context_id)
477+
assert current_orientation["type"] == "portrait-secondary", (
478+
f"Expected portrait-secondary, got {current_orientation}"
479+
)
480+
assert current_orientation["angle"] == 180, f"Expected angle 180, got {current_orientation['angle']}"
481+
482+
driver.emulation.set_screen_orientation_override(screen_orientation=None, contexts=[context_id])
483+
484+
# Verify orientation was cleared
485+
assert get_screen_orientation(driver, context_id) == initial_orientation
486+
487+
488+
def test_set_screen_orientation_override_with_user_contexts(driver, pages):
489+
user_context = driver.browser.create_user_context()
490+
try:
491+
context_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context)
492+
try:
493+
driver.switch_to.window(context_id)
494+
495+
# Set landscape-secondary orientation on portrait natural using string
496+
orientation = ScreenOrientation(
497+
natural="portrait",
498+
type="landscape-secondary",
499+
)
500+
driver.emulation.set_screen_orientation_override(
501+
screen_orientation=orientation, user_contexts=[user_context]
502+
)
503+
504+
url = pages.url("formPage.html")
505+
driver.browsing_context.navigate(context_id, url, wait="complete")
506+
507+
# Verify the orientation was set
508+
current_orientation = get_screen_orientation(driver, context_id)
509+
assert current_orientation["type"] == "landscape-secondary", (
510+
f"Expected landscape-secondary, got {current_orientation}"
511+
)
512+
assert current_orientation["angle"] == 270, f"Expected angle 270, got {current_orientation['angle']}"
513+
514+
driver.emulation.set_screen_orientation_override(screen_orientation=None, user_contexts=[user_context])
515+
finally:
516+
driver.browsing_context.close(context_id)
517+
finally:
518+
driver.browser.remove_user_context(user_context)

0 commit comments

Comments
 (0)