Path: blob/trunk/py/test/selenium/webdriver/common/bidi_emulation_tests.py
4041 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.16import pytest1718from selenium.webdriver.common.bidi.emulation import (19Emulation,20GeolocationCoordinates,21GeolocationPositionError,22ScreenOrientation,23ScreenOrientationNatural,24ScreenOrientationType,25)26from selenium.webdriver.common.bidi.permissions import PermissionState27from selenium.webdriver.common.window import WindowTypes282930def get_browser_timezone_string(driver):31result = driver.script._evaluate(32"Intl.DateTimeFormat().resolvedOptions().timeZone",33{"context": driver.current_window_handle},34await_promise=False,35)36return result.result["value"]373839def get_browser_timezone_offset(driver):40result = driver.script._evaluate(41"new Date().getTimezoneOffset()", {"context": driver.current_window_handle}, await_promise=False42)43return result.result["value"]444546def get_browser_geolocation(driver, user_context=None):47origin = driver.execute_script("return window.location.origin;")48driver.permissions.set_permission("geolocation", PermissionState.GRANTED, origin, user_context=user_context)4950return driver.execute_async_script("""51const callback = arguments[arguments.length - 1];52navigator.geolocation.getCurrentPosition(53position => {54const coords = position.coords;55callback({56latitude: coords.latitude,57longitude: coords.longitude,58accuracy: coords.accuracy,59altitude: coords.altitude,60altitudeAccuracy: coords.altitudeAccuracy,61heading: coords.heading,62speed: coords.speed,63timestamp: position.timestamp64});65},66error => {67callback({ error: error.message });68}69);70""")717273def get_browser_locale(driver):74result = driver.script._evaluate(75"Intl.DateTimeFormat().resolvedOptions().locale",76{"context": driver.current_window_handle},77await_promise=False,78)79return result.result["value"]808182def get_screen_orientation(driver, context_id):83result = driver.script._evaluate(84"screen.orientation.type",85{"context": context_id},86await_promise=False,87)88orientation_type = result.result["value"]8990result = driver.script._evaluate(91"screen.orientation.angle",92{"context": context_id},93await_promise=False,94)95orientation_angle = result.result["value"]9697return {"type": orientation_type, "angle": orientation_angle}9899100def get_browser_user_agent(driver):101result = driver.script._evaluate(102"navigator.userAgent",103{"context": driver.current_window_handle},104await_promise=False,105)106return result.result["value"]107108109def is_online(driver, context_id):110result = driver.script._evaluate("navigator.onLine", {"context": context_id}, await_promise=False)111return result.result["value"]112113114def get_screen_dimensions(driver, context_id):115width_result = driver.script._evaluate("screen.width", {"context": context_id}, await_promise=False)116height_result = driver.script._evaluate("screen.height", {"context": context_id}, await_promise=False)117return {"width": width_result.result["value"], "height": height_result.result["value"]}118119120def test_emulation_initialized(driver):121assert driver.emulation is not None122assert isinstance(driver.emulation, Emulation)123124125def test_set_geolocation_override_with_coordinates_in_context(driver, pages):126context_id = driver.current_window_handle127pages.load("blank.html")128coords = GeolocationCoordinates(45.5, -122.4194, accuracy=10.0)129130driver.emulation.set_geolocation_override(coordinates=coords, contexts=[context_id])131132result = get_browser_geolocation(driver)133134assert "error" not in result, f"Geolocation error: {result.get('error')}"135assert abs(result["latitude"] - coords.latitude) < 0.0001, f"Latitude mismatch: {result['latitude']}"136assert abs(result["longitude"] - coords.longitude) < 0.0001, f"Longitude mismatch: {result['longitude']}"137assert abs(result["accuracy"] - coords.accuracy) < 1.0, f"Accuracy mismatch: {result['accuracy']}"138139140def test_set_geolocation_override_with_coordinates_in_user_context(driver, pages):141# Create a user context142user_context = driver.browser.create_user_context()143144context_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context)145146driver.switch_to.window(context_id)147pages.load("blank.html")148149coords = GeolocationCoordinates(45.5, -122.4194, accuracy=10.0)150151driver.emulation.set_geolocation_override(coordinates=coords, user_contexts=[user_context])152153result = get_browser_geolocation(driver, user_context=user_context)154155assert "error" not in result, f"Geolocation error: {result.get('error')}"156assert abs(result["latitude"] - coords.latitude) < 0.0001, f"Latitude mismatch: {result['latitude']}"157assert abs(result["longitude"] - coords.longitude) < 0.0001, f"Longitude mismatch: {result['longitude']}"158assert abs(result["accuracy"] - coords.accuracy) < 1.0, f"Accuracy mismatch: {result['accuracy']}"159160driver.browsing_context.close(context_id)161driver.browser.remove_user_context(user_context)162163164def test_set_geolocation_override_all_coords(driver, pages):165context_id = driver.current_window_handle166pages.load("blank.html")167coords = GeolocationCoordinates(16845.5, -122.4194, accuracy=10.0, altitude=100.2, altitude_accuracy=5.0, heading=183.2, speed=10.0169)170171driver.emulation.set_geolocation_override(coordinates=coords, contexts=[context_id])172173result = get_browser_geolocation(driver)174175assert "error" not in result, f"Geolocation error: {result.get('error')}"176assert abs(result["latitude"] - coords.latitude) < 0.0001, f"Latitude mismatch: {result['latitude']}"177assert abs(result["longitude"] - coords.longitude) < 0.0001, f"Longitude mismatch: {result['longitude']}"178assert abs(result["accuracy"] - coords.accuracy) < 1.0, f"Accuracy mismatch: {result['accuracy']}"179assert abs(result["altitude"] - coords.altitude) < 0.0001, f"Altitude mismatch: {result['altitude']}"180assert abs(result["altitudeAccuracy"] - coords.altitude_accuracy) < 0.1, (181f"Altitude accuracy mismatch: {result['altitudeAccuracy']}"182)183assert abs(result["heading"] - coords.heading) < 0.1, f"Heading mismatch: {result['heading']}"184assert abs(result["speed"] - coords.speed) < 0.1, f"Speed mismatch: {result['speed']}"185186driver.browsing_context.close(context_id)187188189def test_set_geolocation_override_with_multiple_contexts(driver, pages):190# Create two browsing contexts191context1_id = driver.browsing_context.create(type=WindowTypes.TAB)192context2_id = driver.browsing_context.create(type=WindowTypes.TAB)193194coords = GeolocationCoordinates(45.5, -122.4194, accuracy=10.0)195196driver.emulation.set_geolocation_override(coordinates=coords, contexts=[context1_id, context2_id])197198# Test first context199driver.switch_to.window(context1_id)200pages.load("blank.html")201result1 = get_browser_geolocation(driver)202203assert "error" not in result1, f"Geolocation error in context1: {result1.get('error')}"204assert abs(result1["latitude"] - coords.latitude) < 0.0001, f"Context1 latitude mismatch: {result1['latitude']}"205assert abs(result1["longitude"] - coords.longitude) < 0.0001, f"Context1 longitude mismatch: {result1['longitude']}"206assert abs(result1["accuracy"] - coords.accuracy) < 1.0, f"Context1 accuracy mismatch: {result1['accuracy']}"207208# Test second context209driver.switch_to.window(context2_id)210pages.load("blank.html")211result2 = get_browser_geolocation(driver)212213assert "error" not in result2, f"Geolocation error in context2: {result2.get('error')}"214assert abs(result2["latitude"] - coords.latitude) < 0.0001, f"Context2 latitude mismatch: {result2['latitude']}"215assert abs(result2["longitude"] - coords.longitude) < 0.0001, f"Context2 longitude mismatch: {result2['longitude']}"216assert abs(result2["accuracy"] - coords.accuracy) < 1.0, f"Context2 accuracy mismatch: {result2['accuracy']}"217218driver.browsing_context.close(context1_id)219driver.browsing_context.close(context2_id)220221222def test_set_geolocation_override_with_multiple_user_contexts(driver, pages):223# Create two user contexts224user_context1 = driver.browser.create_user_context()225user_context2 = driver.browser.create_user_context()226227context1_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context1)228context2_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context2)229230coords = GeolocationCoordinates(45.5, -122.4194, accuracy=10.0)231232driver.emulation.set_geolocation_override(coordinates=coords, user_contexts=[user_context1, user_context2])233234# Test first user context235driver.switch_to.window(context1_id)236pages.load("blank.html")237result1 = get_browser_geolocation(driver, user_context=user_context1)238239assert "error" not in result1, f"Geolocation error in user_context1: {result1.get('error')}"240assert abs(result1["latitude"] - coords.latitude) < 0.0001, (241f"User context1 latitude mismatch: {result1['latitude']}"242)243assert abs(result1["longitude"] - coords.longitude) < 0.0001, (244f"User context1 longitude mismatch: {result1['longitude']}"245)246assert abs(result1["accuracy"] - coords.accuracy) < 1.0, f"User context1 accuracy mismatch: {result1['accuracy']}"247248# Test second user context249driver.switch_to.window(context2_id)250pages.load("blank.html")251result2 = get_browser_geolocation(driver, user_context=user_context2)252253assert "error" not in result2, f"Geolocation error in user_context2: {result2.get('error')}"254assert abs(result2["latitude"] - coords.latitude) < 0.0001, (255f"User context2 latitude mismatch: {result2['latitude']}"256)257assert abs(result2["longitude"] - coords.longitude) < 0.0001, (258f"User context2 longitude mismatch: {result2['longitude']}"259)260assert abs(result2["accuracy"] - coords.accuracy) < 1.0, f"User context2 accuracy mismatch: {result2['accuracy']}"261262driver.browsing_context.close(context1_id)263driver.browsing_context.close(context2_id)264driver.browser.remove_user_context(user_context1)265driver.browser.remove_user_context(user_context2)266267268@pytest.mark.xfail_firefox269def test_set_geolocation_override_with_error(driver, pages):270context_id = driver.current_window_handle271pages.load("blank.html")272273error = GeolocationPositionError()274275driver.emulation.set_geolocation_override(error=error, contexts=[context_id])276277result = get_browser_geolocation(driver)278assert "error" in result, f"Expected geolocation error, got: {result}"279280281def test_set_timezone_override_with_context(driver, pages):282context_id = driver.current_window_handle283pages.load("blank.html")284285initial_timezone_string = get_browser_timezone_string(driver)286287# Set timezone to Tokyo (UTC+9)288driver.emulation.set_timezone_override(timezone="Asia/Tokyo", contexts=[context_id])289290timezone_offset = get_browser_timezone_offset(driver)291timezone_string = get_browser_timezone_string(driver)292293# Tokyo is UTC+9, so the offset should be -540 minutes (negative because it's ahead of UTC)294assert timezone_offset == -540, f"Expected timezone offset -540, got: {timezone_offset}"295assert timezone_string == "Asia/Tokyo", f"Expected timezone 'Asia/Tokyo', got: {timezone_string}"296297# Clear the timezone override298driver.emulation.set_timezone_override(timezone=None, contexts=[context_id])299300# verify setting timezone to None clears the timezone override301timezone_after_clear_with_none = get_browser_timezone_string(driver)302assert timezone_after_clear_with_none == initial_timezone_string303304305def test_set_timezone_override_with_user_context(driver, pages):306user_context = driver.browser.create_user_context()307context_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context)308309driver.switch_to.window(context_id)310pages.load("blank.html")311312driver.emulation.set_timezone_override(timezone="America/New_York", user_contexts=[user_context])313314timezone_string = get_browser_timezone_string(driver)315assert timezone_string == "America/New_York", f"Expected timezone 'America/New_York', got: {timezone_string}"316317driver.emulation.set_timezone_override(timezone=None, user_contexts=[user_context])318319driver.browsing_context.close(context_id)320driver.browser.remove_user_context(user_context)321322323@pytest.mark.xfail_firefox(reason="Firefox returns UTC as timezone string in case of offset.")324def test_set_timezone_override_using_offset(driver, pages):325context_id = driver.current_window_handle326pages.load("blank.html")327328# set timezone to India (UTC+05:30) using offset329driver.emulation.set_timezone_override(timezone="+05:30", contexts=[context_id])330331timezone_offset = get_browser_timezone_offset(driver)332timezone_string = get_browser_timezone_string(driver)333334# India is UTC+05:30, so the offset should be -330 minutes (negative because it's ahead of UTC)335assert timezone_offset == -330, f"Expected timezone offset -540, got: {timezone_offset}"336assert timezone_string == "+05:30", f"Expected timezone '+05:30', got: {timezone_string}"337338driver.emulation.set_timezone_override(timezone=None, contexts=[context_id])339340341@pytest.mark.parametrize(342("locale", "expected_locale"),343[344# Locale with Unicode extension keyword for collation.345("de-DE-u-co-phonebk", "de-DE"),346# Lowercase language and region.347("fr-ca", "fr-CA"),348# Uppercase language and region (should be normalized by Intl.Locale).349("FR-CA", "fr-CA"),350# Mixed case language and region (should be normalized by Intl.Locale).351("fR-cA", "fr-CA"),352# Locale with transform extension (simple case).353("en-t-zh", "en"),354],355)356def test_set_locale_override_with_contexts(driver, pages, locale, expected_locale):357context_id = driver.current_window_handle358359driver.emulation.set_locale_override(locale=locale, contexts=[context_id])360361driver.browsing_context.navigate(context_id, pages.url("formPage.html"), wait="complete")362363current_locale = get_browser_locale(driver)364assert current_locale == expected_locale, f"Expected locale {expected_locale}, got {current_locale}"365366367@pytest.mark.parametrize(368"value",369[370# Simple language code (2-letter).371"en",372# Language and region (both 2-letter).373"en-US",374# Language and script (4-letter).375"sr-Latn",376# Language, script, and region.377"zh-Hans-CN",378],379)380def test_set_locale_override_with_user_contexts(driver, pages, value):381user_context = driver.browser.create_user_context()382try:383context_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context)384try:385driver.switch_to.window(context_id)386387driver.emulation.set_locale_override(locale=value, user_contexts=[user_context])388389driver.browsing_context.navigate(context_id, pages.url("formPage.html"), wait="complete")390391current_locale = get_browser_locale(driver)392assert current_locale == value, f"Expected locale {value}, got {current_locale}"393finally:394driver.browsing_context.close(context_id)395finally:396driver.browser.remove_user_context(user_context)397398399@pytest.mark.xfail_firefox(reason="Not yet supported")400def test_set_scripting_enabled_with_contexts(driver, pages):401context_id = driver.current_window_handle402403# disable scripting404driver.emulation.set_scripting_enabled(enabled=False, contexts=[context_id])405406driver.browsing_context.navigate(407context=context_id,408url="data:text/html,<script>window.foo=123;</script>",409wait="complete",410)411result = driver.script._evaluate("'foo' in window", {"context": context_id}, await_promise=False)412assert result.result["value"] is False, "Page script should not have executed when scripting is disabled"413414# clear override via None to restore JS415driver.emulation.set_scripting_enabled(enabled=None, contexts=[context_id])416driver.browsing_context.navigate(417context=context_id,418url="data:text/html,<script>window.foo=123;</script>",419wait="complete",420)421result = driver.script._evaluate("'foo' in window", {"context": context_id}, await_promise=False)422assert result.result["value"] is True, "Page script should execute after clearing the override"423424425@pytest.mark.xfail_firefox(reason="Not yet supported")426def test_set_scripting_enabled_with_user_contexts(driver, pages):427user_context = driver.browser.create_user_context()428try:429context_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context)430try:431driver.switch_to.window(context_id)432433driver.emulation.set_scripting_enabled(enabled=False, user_contexts=[user_context])434435url = pages.url("javascriptPage.html")436driver.browsing_context.navigate(context_id, url, wait="complete")437438# Check that inline event handlers don't work; this page has an onclick handler439click_field = driver.find_element("id", "clickField")440initial_value = click_field.get_attribute("value") # initial value is 'Hello'441click_field.click()442443# Get the value after click, it should remain unchanged if scripting is disabled444result_value = driver.script._evaluate(445"document.getElementById('clickField').value", {"context": context_id}, await_promise=False446)447assert result_value.result["value"] == initial_value, (448"Inline onclick handler should not execute, i.e, value should not change to 'clicked'"449)450451# Clear the scripting override452driver.emulation.set_scripting_enabled(enabled=None, user_contexts=[user_context])453454driver.browsing_context.navigate(context_id, url, wait="complete")455456# Click the element again, it should change to 'Clicked' now457driver.find_element("id", "clickField").click()458result_value = driver.script._evaluate(459"document.getElementById('clickField').value", {"context": context_id}, await_promise=False460)461assert result_value.result["value"] == "Clicked"462finally:463driver.browsing_context.close(context_id)464finally:465driver.browser.remove_user_context(user_context)466467468def test_set_screen_orientation_override_with_contexts(driver, pages):469context_id = driver.current_window_handle470initial_orientation = get_screen_orientation(driver, context_id)471472# Set landscape-primary orientation473orientation = ScreenOrientation(474natural=ScreenOrientationNatural.LANDSCAPE,475type=ScreenOrientationType.LANDSCAPE_PRIMARY,476)477driver.emulation.set_screen_orientation_override(screen_orientation=orientation, contexts=[context_id])478479url = pages.url("formPage.html")480driver.browsing_context.navigate(context_id, url, wait="complete")481482# Verify the orientation was set483current_orientation = get_screen_orientation(driver, context_id)484assert current_orientation["type"] == "landscape-primary", f"Expected landscape-primary, got {current_orientation}"485assert current_orientation["angle"] == 0, f"Expected angle 0, got {current_orientation['angle']}"486487# Set portrait-secondary orientation488orientation = ScreenOrientation(489natural=ScreenOrientationNatural.PORTRAIT,490type=ScreenOrientationType.PORTRAIT_SECONDARY,491)492driver.emulation.set_screen_orientation_override(screen_orientation=orientation, contexts=[context_id])493494# Verify the orientation was changed495current_orientation = get_screen_orientation(driver, context_id)496assert current_orientation["type"] == "portrait-secondary", (497f"Expected portrait-secondary, got {current_orientation}"498)499assert current_orientation["angle"] == 180, f"Expected angle 180, got {current_orientation['angle']}"500501driver.emulation.set_screen_orientation_override(screen_orientation=None, contexts=[context_id])502503# Verify orientation was cleared504assert get_screen_orientation(driver, context_id) == initial_orientation505506507@pytest.mark.parametrize(508("natural", "orientation_type", "expected_angle"),509[510# Portrait natural orientations511("Portrait", "portrait-primary", 0),512("portrait", "portrait-secondary", 180),513("portrait", "landscape-primary", 90),514("portrait", "landscape-secondary", 270),515# Landscape natural orientations516("Landscape", "Portrait-Primary", 90), # test with different casing517("landscape", "portrait-secondary", 270),518("landscape", "landscape-primary", 0),519("landscape", "landscape-secondary", 180),520],521)522def test_set_screen_orientation_override_with_user_contexts(driver, pages, natural, orientation_type, expected_angle):523user_context = driver.browser.create_user_context()524try:525context_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context)526try:527driver.switch_to.window(context_id)528529# Set the specified orientation530orientation = ScreenOrientation(natural=natural, type=orientation_type)531driver.emulation.set_screen_orientation_override(532screen_orientation=orientation, user_contexts=[user_context]533)534535url = pages.url("formPage.html")536driver.browsing_context.navigate(context_id, url, wait="complete")537538# Verify the orientation was set539current_orientation = get_screen_orientation(driver, context_id)540541assert current_orientation["type"] == orientation_type.lower()542assert current_orientation["angle"] == expected_angle543544driver.emulation.set_screen_orientation_override(screen_orientation=None, user_contexts=[user_context])545finally:546driver.browsing_context.close(context_id)547finally:548driver.browser.remove_user_context(user_context)549550551def test_set_user_agent_override_with_contexts(driver, pages):552context_id = driver.current_window_handle553url = pages.url("formPage.html")554driver.browsing_context.navigate(context_id, url, wait="complete")555initial_user_agent = get_browser_user_agent(driver)556557custom_user_agent = "Mozilla/5.0 (Custom Test Agent)"558driver.emulation.set_user_agent_override(user_agent=custom_user_agent, contexts=[context_id])559560assert get_browser_user_agent(driver) == custom_user_agent561562driver.emulation.set_user_agent_override(user_agent=None, contexts=[context_id])563assert get_browser_user_agent(driver) == initial_user_agent564565566def test_set_user_agent_override_with_user_contexts(driver, pages):567user_context = driver.browser.create_user_context()568try:569context_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context)570try:571driver.switch_to.window(context_id)572url = pages.url("formPage.html")573driver.browsing_context.navigate(context_id, url, wait="complete")574initial_user_agent = get_browser_user_agent(driver)575576custom_user_agent = "Mozilla/5.0 (Custom User Context Agent)"577driver.emulation.set_user_agent_override(user_agent=custom_user_agent, user_contexts=[user_context])578579assert get_browser_user_agent(driver) == custom_user_agent580581driver.emulation.set_user_agent_override(user_agent=None, user_contexts=[user_context])582assert get_browser_user_agent(driver) == initial_user_agent583finally:584driver.browsing_context.close(context_id)585finally:586driver.browser.remove_user_context(user_context)587588589@pytest.mark.xfail_firefox590def test_set_network_conditions_offline_with_context(driver, pages):591context_id = driver.current_window_handle592driver.browsing_context.navigate(context_id, pages.url("formPage.html"), wait="complete")593594assert is_online(driver, context_id) is True595596try:597# Set offline598driver.emulation.set_network_conditions(offline=True, contexts=[context_id])599assert is_online(driver, context_id) is False600finally:601# Reset602driver.emulation.set_network_conditions(offline=False, contexts=[context_id])603assert is_online(driver, context_id) is True604605606@pytest.mark.xfail_firefox607def test_set_network_conditions_offline_with_user_context(driver, pages):608user_context = driver.browser.create_user_context()609try:610context_id = driver.browsing_context.create(611type=WindowTypes.TAB,612user_context=user_context,613)614try:615driver.switch_to.window(context_id)616driver.browsing_context.navigate(context_id, pages.url("formPage.html"), wait="complete")617618assert is_online(driver, context_id) is True619620driver.emulation.set_network_conditions(offline=True, user_contexts=[user_context])621assert is_online(driver, context_id) is False622finally:623driver.emulation.set_network_conditions(offline=False, user_contexts=[user_context])624driver.browsing_context.close(context_id)625finally:626driver.browser.remove_user_context(user_context)627628629@pytest.mark.xfail_chrome630@pytest.mark.xfail_edge631def test_set_screen_settings_override_with_contexts(driver, pages):632context_id = driver.current_window_handle633driver.browsing_context.navigate(context_id, pages.url("formPage.html"), wait="complete")634initial_dimensions = get_screen_dimensions(driver, context_id)635636try:637# Set custom screen dimensions638driver.emulation.set_screen_settings_override(width=1024, height=768, contexts=[context_id])639640# Verify the screen dimensions were set641current_dimensions = get_screen_dimensions(driver, context_id)642assert current_dimensions["width"] == 1024, f"Expected width 1024, got {current_dimensions['width']}"643assert current_dimensions["height"] == 768, f"Expected height 768, got {current_dimensions['height']}"644645finally:646# Clear the override647driver.emulation.set_screen_settings_override(contexts=[context_id])648649# Verify the screen dimensions were restored650restored_dimensions = get_screen_dimensions(driver, context_id)651assert restored_dimensions == initial_dimensions, f"Expected dimensions to be restored to {initial_dimensions}"652653654@pytest.mark.xfail_chrome655@pytest.mark.xfail_edge656def test_set_screen_settings_override_with_user_contexts(driver, pages):657user_context = driver.browser.create_user_context()658try:659context_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context)660try:661driver.switch_to.window(context_id)662driver.browsing_context.navigate(context_id, pages.url("formPage.html"), wait="complete")663664initial_dimensions = get_screen_dimensions(driver, context_id)665666driver.emulation.set_screen_settings_override(width=800, height=600, user_contexts=[user_context])667668current_dimensions = get_screen_dimensions(driver, context_id)669assert current_dimensions["width"] == 800, f"Expected width 800, got {current_dimensions['width']}"670assert current_dimensions["height"] == 600, f"Expected height 600, got {current_dimensions['height']}"671672driver.emulation.set_screen_settings_override(user_contexts=[user_context])673674restored_dimensions = get_screen_dimensions(driver, context_id)675assert restored_dimensions == initial_dimensions, (676f"Expected dimensions to be restored to {initial_dimensions}"677)678finally:679driver.browsing_context.close(context_id)680finally:681driver.browser.remove_user_context(user_context)682683684