Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/test/selenium/webdriver/common/bidi_emulation_tests.py
4020 views
1
# Licensed to the Software Freedom Conservancy (SFC) under one
2
# or more contributor license agreements. See the NOTICE file
3
# distributed with this work for additional information
4
# regarding copyright ownership. The SFC licenses this file
5
# to you under the Apache License, Version 2.0 (the
6
# "License"); you may not use this file except in compliance
7
# with the License. You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing,
12
# software distributed under the License is distributed on an
13
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
# KIND, either express or implied. See the License for the
15
# specific language governing permissions and limitations
16
# under the License.
17
import pytest
18
19
from selenium.webdriver.common.bidi.emulation import (
20
Emulation,
21
GeolocationCoordinates,
22
GeolocationPositionError,
23
ScreenOrientation,
24
ScreenOrientationNatural,
25
ScreenOrientationType,
26
)
27
from selenium.webdriver.common.bidi.permissions import PermissionState
28
from selenium.webdriver.common.window import WindowTypes
29
30
31
def get_browser_timezone_string(driver):
32
result = driver.script._evaluate(
33
"Intl.DateTimeFormat().resolvedOptions().timeZone",
34
{"context": driver.current_window_handle},
35
await_promise=False,
36
)
37
return result.result["value"]
38
39
40
def get_browser_timezone_offset(driver):
41
result = driver.script._evaluate(
42
"new Date().getTimezoneOffset()", {"context": driver.current_window_handle}, await_promise=False
43
)
44
return result.result["value"]
45
46
47
def get_browser_geolocation(driver, user_context=None):
48
origin = driver.execute_script("return window.location.origin;")
49
driver.permissions.set_permission("geolocation", PermissionState.GRANTED, origin, user_context=user_context)
50
51
return driver.execute_async_script("""
52
const callback = arguments[arguments.length - 1];
53
navigator.geolocation.getCurrentPosition(
54
position => {
55
const coords = position.coords;
56
callback({
57
latitude: coords.latitude,
58
longitude: coords.longitude,
59
accuracy: coords.accuracy,
60
altitude: coords.altitude,
61
altitudeAccuracy: coords.altitudeAccuracy,
62
heading: coords.heading,
63
speed: coords.speed,
64
timestamp: position.timestamp
65
});
66
},
67
error => {
68
callback({ error: error.message });
69
}
70
);
71
""")
72
73
74
def get_browser_locale(driver):
75
result = driver.script._evaluate(
76
"Intl.DateTimeFormat().resolvedOptions().locale",
77
{"context": driver.current_window_handle},
78
await_promise=False,
79
)
80
return result.result["value"]
81
82
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
101
def get_browser_user_agent(driver):
102
result = driver.script._evaluate(
103
"navigator.userAgent",
104
{"context": driver.current_window_handle},
105
await_promise=False,
106
)
107
return result.result["value"]
108
109
110
def is_online(driver, context_id):
111
result = driver.script._evaluate("navigator.onLine", {"context": context_id}, await_promise=False)
112
return result.result["value"]
113
114
115
def get_screen_dimensions(driver, context_id):
116
width_result = driver.script._evaluate("screen.width", {"context": context_id}, await_promise=False)
117
height_result = driver.script._evaluate("screen.height", {"context": context_id}, await_promise=False)
118
return {"width": width_result.result["value"], "height": height_result.result["value"]}
119
120
121
def test_emulation_initialized(driver):
122
assert driver.emulation is not None
123
assert isinstance(driver.emulation, Emulation)
124
125
126
def test_set_geolocation_override_with_coordinates_in_context(driver, pages):
127
context_id = driver.current_window_handle
128
pages.load("blank.html")
129
coords = GeolocationCoordinates(45.5, -122.4194, accuracy=10.0)
130
131
driver.emulation.set_geolocation_override(coordinates=coords, contexts=[context_id])
132
133
result = get_browser_geolocation(driver)
134
135
assert "error" not in result, f"Geolocation error: {result.get('error')}"
136
assert abs(result["latitude"] - coords.latitude) < 0.0001, f"Latitude mismatch: {result['latitude']}"
137
assert abs(result["longitude"] - coords.longitude) < 0.0001, f"Longitude mismatch: {result['longitude']}"
138
assert abs(result["accuracy"] - coords.accuracy) < 1.0, f"Accuracy mismatch: {result['accuracy']}"
139
140
141
def test_set_geolocation_override_with_coordinates_in_user_context(driver, pages):
142
# Create a user context
143
user_context = driver.browser.create_user_context()
144
145
context_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context)
146
147
driver.switch_to.window(context_id)
148
pages.load("blank.html")
149
150
coords = GeolocationCoordinates(45.5, -122.4194, accuracy=10.0)
151
152
driver.emulation.set_geolocation_override(coordinates=coords, user_contexts=[user_context])
153
154
result = get_browser_geolocation(driver, user_context=user_context)
155
156
assert "error" not in result, f"Geolocation error: {result.get('error')}"
157
assert abs(result["latitude"] - coords.latitude) < 0.0001, f"Latitude mismatch: {result['latitude']}"
158
assert abs(result["longitude"] - coords.longitude) < 0.0001, f"Longitude mismatch: {result['longitude']}"
159
assert abs(result["accuracy"] - coords.accuracy) < 1.0, f"Accuracy mismatch: {result['accuracy']}"
160
161
driver.browsing_context.close(context_id)
162
driver.browser.remove_user_context(user_context)
163
164
165
def test_set_geolocation_override_all_coords(driver, pages):
166
context_id = driver.current_window_handle
167
pages.load("blank.html")
168
coords = GeolocationCoordinates(
169
45.5, -122.4194, accuracy=10.0, altitude=100.2, altitude_accuracy=5.0, heading=183.2, speed=10.0
170
)
171
172
driver.emulation.set_geolocation_override(coordinates=coords, contexts=[context_id])
173
174
result = get_browser_geolocation(driver)
175
176
assert "error" not in result, f"Geolocation error: {result.get('error')}"
177
assert abs(result["latitude"] - coords.latitude) < 0.0001, f"Latitude mismatch: {result['latitude']}"
178
assert abs(result["longitude"] - coords.longitude) < 0.0001, f"Longitude mismatch: {result['longitude']}"
179
assert abs(result["accuracy"] - coords.accuracy) < 1.0, f"Accuracy mismatch: {result['accuracy']}"
180
assert abs(result["altitude"] - coords.altitude) < 0.0001, f"Altitude mismatch: {result['altitude']}"
181
assert abs(result["altitudeAccuracy"] - coords.altitude_accuracy) < 0.1, (
182
f"Altitude accuracy mismatch: {result['altitudeAccuracy']}"
183
)
184
assert abs(result["heading"] - coords.heading) < 0.1, f"Heading mismatch: {result['heading']}"
185
assert abs(result["speed"] - coords.speed) < 0.1, f"Speed mismatch: {result['speed']}"
186
187
driver.browsing_context.close(context_id)
188
189
190
def test_set_geolocation_override_with_multiple_contexts(driver, pages):
191
# Create two browsing contexts
192
context1_id = driver.browsing_context.create(type=WindowTypes.TAB)
193
context2_id = driver.browsing_context.create(type=WindowTypes.TAB)
194
195
coords = GeolocationCoordinates(45.5, -122.4194, accuracy=10.0)
196
197
driver.emulation.set_geolocation_override(coordinates=coords, contexts=[context1_id, context2_id])
198
199
# Test first context
200
driver.switch_to.window(context1_id)
201
pages.load("blank.html")
202
result1 = get_browser_geolocation(driver)
203
204
assert "error" not in result1, f"Geolocation error in context1: {result1.get('error')}"
205
assert abs(result1["latitude"] - coords.latitude) < 0.0001, f"Context1 latitude mismatch: {result1['latitude']}"
206
assert abs(result1["longitude"] - coords.longitude) < 0.0001, f"Context1 longitude mismatch: {result1['longitude']}"
207
assert abs(result1["accuracy"] - coords.accuracy) < 1.0, f"Context1 accuracy mismatch: {result1['accuracy']}"
208
209
# Test second context
210
driver.switch_to.window(context2_id)
211
pages.load("blank.html")
212
result2 = get_browser_geolocation(driver)
213
214
assert "error" not in result2, f"Geolocation error in context2: {result2.get('error')}"
215
assert abs(result2["latitude"] - coords.latitude) < 0.0001, f"Context2 latitude mismatch: {result2['latitude']}"
216
assert abs(result2["longitude"] - coords.longitude) < 0.0001, f"Context2 longitude mismatch: {result2['longitude']}"
217
assert abs(result2["accuracy"] - coords.accuracy) < 1.0, f"Context2 accuracy mismatch: {result2['accuracy']}"
218
219
driver.browsing_context.close(context1_id)
220
driver.browsing_context.close(context2_id)
221
222
223
def test_set_geolocation_override_with_multiple_user_contexts(driver, pages):
224
# Create two user contexts
225
user_context1 = driver.browser.create_user_context()
226
user_context2 = driver.browser.create_user_context()
227
228
context1_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context1)
229
context2_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context2)
230
231
coords = GeolocationCoordinates(45.5, -122.4194, accuracy=10.0)
232
233
driver.emulation.set_geolocation_override(coordinates=coords, user_contexts=[user_context1, user_context2])
234
235
# Test first user context
236
driver.switch_to.window(context1_id)
237
pages.load("blank.html")
238
result1 = get_browser_geolocation(driver, user_context=user_context1)
239
240
assert "error" not in result1, f"Geolocation error in user_context1: {result1.get('error')}"
241
assert abs(result1["latitude"] - coords.latitude) < 0.0001, (
242
f"User context1 latitude mismatch: {result1['latitude']}"
243
)
244
assert abs(result1["longitude"] - coords.longitude) < 0.0001, (
245
f"User context1 longitude mismatch: {result1['longitude']}"
246
)
247
assert abs(result1["accuracy"] - coords.accuracy) < 1.0, f"User context1 accuracy mismatch: {result1['accuracy']}"
248
249
# Test second user context
250
driver.switch_to.window(context2_id)
251
pages.load("blank.html")
252
result2 = get_browser_geolocation(driver, user_context=user_context2)
253
254
assert "error" not in result2, f"Geolocation error in user_context2: {result2.get('error')}"
255
assert abs(result2["latitude"] - coords.latitude) < 0.0001, (
256
f"User context2 latitude mismatch: {result2['latitude']}"
257
)
258
assert abs(result2["longitude"] - coords.longitude) < 0.0001, (
259
f"User context2 longitude mismatch: {result2['longitude']}"
260
)
261
assert abs(result2["accuracy"] - coords.accuracy) < 1.0, f"User context2 accuracy mismatch: {result2['accuracy']}"
262
263
driver.browsing_context.close(context1_id)
264
driver.browsing_context.close(context2_id)
265
driver.browser.remove_user_context(user_context1)
266
driver.browser.remove_user_context(user_context2)
267
268
269
@pytest.mark.xfail_firefox
270
def test_set_geolocation_override_with_error(driver, pages):
271
context_id = driver.current_window_handle
272
pages.load("blank.html")
273
274
error = GeolocationPositionError()
275
276
driver.emulation.set_geolocation_override(error=error, contexts=[context_id])
277
278
result = get_browser_geolocation(driver)
279
assert "error" in result, f"Expected geolocation error, got: {result}"
280
281
282
def test_set_timezone_override_with_context(driver, pages):
283
context_id = driver.current_window_handle
284
pages.load("blank.html")
285
286
initial_timezone_string = get_browser_timezone_string(driver)
287
288
# Set timezone to Tokyo (UTC+9)
289
driver.emulation.set_timezone_override(timezone="Asia/Tokyo", contexts=[context_id])
290
291
timezone_offset = get_browser_timezone_offset(driver)
292
timezone_string = get_browser_timezone_string(driver)
293
294
# Tokyo is UTC+9, so the offset should be -540 minutes (negative because it's ahead of UTC)
295
assert timezone_offset == -540, f"Expected timezone offset -540, got: {timezone_offset}"
296
assert timezone_string == "Asia/Tokyo", f"Expected timezone 'Asia/Tokyo', got: {timezone_string}"
297
298
# Clear the timezone override
299
driver.emulation.set_timezone_override(timezone=None, contexts=[context_id])
300
301
# verify setting timezone to None clears the timezone override
302
timezone_after_clear_with_none = get_browser_timezone_string(driver)
303
assert timezone_after_clear_with_none == initial_timezone_string
304
305
306
def test_set_timezone_override_with_user_context(driver, pages):
307
user_context = driver.browser.create_user_context()
308
context_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context)
309
310
driver.switch_to.window(context_id)
311
pages.load("blank.html")
312
313
driver.emulation.set_timezone_override(timezone="America/New_York", user_contexts=[user_context])
314
315
timezone_string = get_browser_timezone_string(driver)
316
assert timezone_string == "America/New_York", f"Expected timezone 'America/New_York', got: {timezone_string}"
317
318
driver.emulation.set_timezone_override(timezone=None, user_contexts=[user_context])
319
320
driver.browsing_context.close(context_id)
321
driver.browser.remove_user_context(user_context)
322
323
324
@pytest.mark.xfail_firefox(reason="Firefox returns UTC as timezone string in case of offset.")
325
def test_set_timezone_override_using_offset(driver, pages):
326
context_id = driver.current_window_handle
327
pages.load("blank.html")
328
329
# set timezone to India (UTC+05:30) using offset
330
driver.emulation.set_timezone_override(timezone="+05:30", contexts=[context_id])
331
332
timezone_offset = get_browser_timezone_offset(driver)
333
timezone_string = get_browser_timezone_string(driver)
334
335
# India is UTC+05:30, so the offset should be -330 minutes (negative because it's ahead of UTC)
336
assert timezone_offset == -330, f"Expected timezone offset -540, got: {timezone_offset}"
337
assert timezone_string == "+05:30", f"Expected timezone '+05:30', got: {timezone_string}"
338
339
driver.emulation.set_timezone_override(timezone=None, contexts=[context_id])
340
341
342
@pytest.mark.parametrize(
343
("locale", "expected_locale"),
344
[
345
# Locale with Unicode extension keyword for collation.
346
("de-DE-u-co-phonebk", "de-DE"),
347
# Lowercase language and region.
348
("fr-ca", "fr-CA"),
349
# Uppercase language and region (should be normalized by Intl.Locale).
350
("FR-CA", "fr-CA"),
351
# Mixed case language and region (should be normalized by Intl.Locale).
352
("fR-cA", "fr-CA"),
353
# Locale with transform extension (simple case).
354
("en-t-zh", "en"),
355
],
356
)
357
def test_set_locale_override_with_contexts(driver, pages, locale, expected_locale):
358
context_id = driver.current_window_handle
359
360
driver.emulation.set_locale_override(locale=locale, contexts=[context_id])
361
362
driver.browsing_context.navigate(context_id, pages.url("formPage.html"), wait="complete")
363
364
current_locale = get_browser_locale(driver)
365
assert current_locale == expected_locale, f"Expected locale {expected_locale}, got {current_locale}"
366
367
368
@pytest.mark.parametrize(
369
"value",
370
[
371
# Simple language code (2-letter).
372
"en",
373
# Language and region (both 2-letter).
374
"en-US",
375
# Language and script (4-letter).
376
"sr-Latn",
377
# Language, script, and region.
378
"zh-Hans-CN",
379
],
380
)
381
def test_set_locale_override_with_user_contexts(driver, pages, value):
382
user_context = driver.browser.create_user_context()
383
try:
384
context_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context)
385
try:
386
driver.switch_to.window(context_id)
387
388
driver.emulation.set_locale_override(locale=value, user_contexts=[user_context])
389
390
driver.browsing_context.navigate(context_id, pages.url("formPage.html"), wait="complete")
391
392
current_locale = get_browser_locale(driver)
393
assert current_locale == value, f"Expected locale {value}, got {current_locale}"
394
finally:
395
driver.browsing_context.close(context_id)
396
finally:
397
driver.browser.remove_user_context(user_context)
398
399
400
@pytest.mark.xfail_firefox(reason="Not yet supported")
401
def test_set_scripting_enabled_with_contexts(driver, pages):
402
context_id = driver.current_window_handle
403
404
# disable scripting
405
driver.emulation.set_scripting_enabled(enabled=False, contexts=[context_id])
406
407
driver.browsing_context.navigate(
408
context=context_id,
409
url="data:text/html,<script>window.foo=123;</script>",
410
wait="complete",
411
)
412
result = driver.script._evaluate("'foo' in window", {"context": context_id}, await_promise=False)
413
assert result.result["value"] is False, "Page script should not have executed when scripting is disabled"
414
415
# clear override via None to restore JS
416
driver.emulation.set_scripting_enabled(enabled=None, contexts=[context_id])
417
driver.browsing_context.navigate(
418
context=context_id,
419
url="data:text/html,<script>window.foo=123;</script>",
420
wait="complete",
421
)
422
result = driver.script._evaluate("'foo' in window", {"context": context_id}, await_promise=False)
423
assert result.result["value"] is True, "Page script should execute after clearing the override"
424
425
426
@pytest.mark.xfail_firefox(reason="Not yet supported")
427
def test_set_scripting_enabled_with_user_contexts(driver, pages):
428
user_context = driver.browser.create_user_context()
429
try:
430
context_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context)
431
try:
432
driver.switch_to.window(context_id)
433
434
driver.emulation.set_scripting_enabled(enabled=False, user_contexts=[user_context])
435
436
url = pages.url("javascriptPage.html")
437
driver.browsing_context.navigate(context_id, url, wait="complete")
438
439
# Check that inline event handlers don't work; this page has an onclick handler
440
click_field = driver.find_element("id", "clickField")
441
initial_value = click_field.get_attribute("value") # initial value is 'Hello'
442
click_field.click()
443
444
# Get the value after click, it should remain unchanged if scripting is disabled
445
result_value = driver.script._evaluate(
446
"document.getElementById('clickField').value", {"context": context_id}, await_promise=False
447
)
448
assert result_value.result["value"] == initial_value, (
449
"Inline onclick handler should not execute, i.e, value should not change to 'clicked'"
450
)
451
452
# Clear the scripting override
453
driver.emulation.set_scripting_enabled(enabled=None, user_contexts=[user_context])
454
455
driver.browsing_context.navigate(context_id, url, wait="complete")
456
457
# Click the element again, it should change to 'Clicked' now
458
driver.find_element("id", "clickField").click()
459
result_value = driver.script._evaluate(
460
"document.getElementById('clickField').value", {"context": context_id}, await_promise=False
461
)
462
assert result_value.result["value"] == "Clicked"
463
finally:
464
driver.browsing_context.close(context_id)
465
finally:
466
driver.browser.remove_user_context(user_context)
467
468
469
def test_set_screen_orientation_override_with_contexts(driver, pages):
470
context_id = driver.current_window_handle
471
initial_orientation = get_screen_orientation(driver, context_id)
472
473
# Set landscape-primary orientation
474
orientation = ScreenOrientation(
475
natural=ScreenOrientationNatural.LANDSCAPE,
476
type=ScreenOrientationType.LANDSCAPE_PRIMARY,
477
)
478
driver.emulation.set_screen_orientation_override(screen_orientation=orientation, contexts=[context_id])
479
480
url = pages.url("formPage.html")
481
driver.browsing_context.navigate(context_id, url, wait="complete")
482
483
# Verify the orientation was set
484
current_orientation = get_screen_orientation(driver, context_id)
485
assert current_orientation["type"] == "landscape-primary", f"Expected landscape-primary, got {current_orientation}"
486
assert current_orientation["angle"] == 0, f"Expected angle 0, got {current_orientation['angle']}"
487
488
# Set portrait-secondary orientation
489
orientation = ScreenOrientation(
490
natural=ScreenOrientationNatural.PORTRAIT,
491
type=ScreenOrientationType.PORTRAIT_SECONDARY,
492
)
493
driver.emulation.set_screen_orientation_override(screen_orientation=orientation, contexts=[context_id])
494
495
# Verify the orientation was changed
496
current_orientation = get_screen_orientation(driver, context_id)
497
assert current_orientation["type"] == "portrait-secondary", (
498
f"Expected portrait-secondary, got {current_orientation}"
499
)
500
assert current_orientation["angle"] == 180, f"Expected angle 180, got {current_orientation['angle']}"
501
502
driver.emulation.set_screen_orientation_override(screen_orientation=None, contexts=[context_id])
503
504
# Verify orientation was cleared
505
assert get_screen_orientation(driver, context_id) == initial_orientation
506
507
508
@pytest.mark.parametrize(
509
("natural", "orientation_type", "expected_angle"),
510
[
511
# Portrait natural orientations
512
("Portrait", "portrait-primary", 0),
513
("portrait", "portrait-secondary", 180),
514
("portrait", "landscape-primary", 90),
515
("portrait", "landscape-secondary", 270),
516
# Landscape natural orientations
517
("Landscape", "Portrait-Primary", 90), # test with different casing
518
("landscape", "portrait-secondary", 270),
519
("landscape", "landscape-primary", 0),
520
("landscape", "landscape-secondary", 180),
521
],
522
)
523
def test_set_screen_orientation_override_with_user_contexts(driver, pages, natural, orientation_type, expected_angle):
524
user_context = driver.browser.create_user_context()
525
try:
526
context_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context)
527
try:
528
driver.switch_to.window(context_id)
529
530
# Set the specified orientation
531
orientation = ScreenOrientation(natural=natural, type=orientation_type)
532
driver.emulation.set_screen_orientation_override(
533
screen_orientation=orientation, user_contexts=[user_context]
534
)
535
536
url = pages.url("formPage.html")
537
driver.browsing_context.navigate(context_id, url, wait="complete")
538
539
# Verify the orientation was set
540
current_orientation = get_screen_orientation(driver, context_id)
541
542
assert current_orientation["type"] == orientation_type.lower()
543
assert current_orientation["angle"] == expected_angle
544
545
driver.emulation.set_screen_orientation_override(screen_orientation=None, user_contexts=[user_context])
546
finally:
547
driver.browsing_context.close(context_id)
548
finally:
549
driver.browser.remove_user_context(user_context)
550
551
552
def test_set_user_agent_override_with_contexts(driver, pages):
553
context_id = driver.current_window_handle
554
url = pages.url("formPage.html")
555
driver.browsing_context.navigate(context_id, url, wait="complete")
556
initial_user_agent = get_browser_user_agent(driver)
557
558
custom_user_agent = "Mozilla/5.0 (Custom Test Agent)"
559
driver.emulation.set_user_agent_override(user_agent=custom_user_agent, contexts=[context_id])
560
561
assert get_browser_user_agent(driver) == custom_user_agent
562
563
driver.emulation.set_user_agent_override(user_agent=None, contexts=[context_id])
564
assert get_browser_user_agent(driver) == initial_user_agent
565
566
567
def test_set_user_agent_override_with_user_contexts(driver, pages):
568
user_context = driver.browser.create_user_context()
569
try:
570
context_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context)
571
try:
572
driver.switch_to.window(context_id)
573
url = pages.url("formPage.html")
574
driver.browsing_context.navigate(context_id, url, wait="complete")
575
initial_user_agent = get_browser_user_agent(driver)
576
577
custom_user_agent = "Mozilla/5.0 (Custom User Context Agent)"
578
driver.emulation.set_user_agent_override(user_agent=custom_user_agent, user_contexts=[user_context])
579
580
assert get_browser_user_agent(driver) == custom_user_agent
581
582
driver.emulation.set_user_agent_override(user_agent=None, user_contexts=[user_context])
583
assert get_browser_user_agent(driver) == initial_user_agent
584
finally:
585
driver.browsing_context.close(context_id)
586
finally:
587
driver.browser.remove_user_context(user_context)
588
589
590
@pytest.mark.xfail_firefox
591
def test_set_network_conditions_offline_with_context(driver, pages):
592
context_id = driver.current_window_handle
593
driver.browsing_context.navigate(context_id, pages.url("formPage.html"), wait="complete")
594
595
assert is_online(driver, context_id) is True
596
597
try:
598
# Set offline
599
driver.emulation.set_network_conditions(offline=True, contexts=[context_id])
600
assert is_online(driver, context_id) is False
601
finally:
602
# Reset
603
driver.emulation.set_network_conditions(offline=False, contexts=[context_id])
604
assert is_online(driver, context_id) is True
605
606
607
@pytest.mark.xfail_firefox
608
def test_set_network_conditions_offline_with_user_context(driver, pages):
609
user_context = driver.browser.create_user_context()
610
try:
611
context_id = driver.browsing_context.create(
612
type=WindowTypes.TAB,
613
user_context=user_context,
614
)
615
try:
616
driver.switch_to.window(context_id)
617
driver.browsing_context.navigate(context_id, pages.url("formPage.html"), wait="complete")
618
619
assert is_online(driver, context_id) is True
620
621
driver.emulation.set_network_conditions(offline=True, user_contexts=[user_context])
622
assert is_online(driver, context_id) is False
623
finally:
624
driver.emulation.set_network_conditions(offline=False, user_contexts=[user_context])
625
driver.browsing_context.close(context_id)
626
finally:
627
driver.browser.remove_user_context(user_context)
628
629
630
@pytest.mark.xfail_chrome
631
@pytest.mark.xfail_edge
632
def test_set_screen_settings_override_with_contexts(driver, pages):
633
context_id = driver.current_window_handle
634
driver.browsing_context.navigate(context_id, pages.url("formPage.html"), wait="complete")
635
initial_dimensions = get_screen_dimensions(driver, context_id)
636
637
try:
638
# Set custom screen dimensions
639
driver.emulation.set_screen_settings_override(width=1024, height=768, contexts=[context_id])
640
641
# Verify the screen dimensions were set
642
current_dimensions = get_screen_dimensions(driver, context_id)
643
assert current_dimensions["width"] == 1024, f"Expected width 1024, got {current_dimensions['width']}"
644
assert current_dimensions["height"] == 768, f"Expected height 768, got {current_dimensions['height']}"
645
646
finally:
647
# Clear the override
648
driver.emulation.set_screen_settings_override(contexts=[context_id])
649
650
# Verify the screen dimensions were restored
651
restored_dimensions = get_screen_dimensions(driver, context_id)
652
assert restored_dimensions == initial_dimensions, f"Expected dimensions to be restored to {initial_dimensions}"
653
654
655
@pytest.mark.xfail_chrome
656
@pytest.mark.xfail_edge
657
def test_set_screen_settings_override_with_user_contexts(driver, pages):
658
user_context = driver.browser.create_user_context()
659
try:
660
context_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context)
661
try:
662
driver.switch_to.window(context_id)
663
driver.browsing_context.navigate(context_id, pages.url("formPage.html"), wait="complete")
664
665
initial_dimensions = get_screen_dimensions(driver, context_id)
666
667
driver.emulation.set_screen_settings_override(width=800, height=600, user_contexts=[user_context])
668
669
current_dimensions = get_screen_dimensions(driver, context_id)
670
assert current_dimensions["width"] == 800, f"Expected width 800, got {current_dimensions['width']}"
671
assert current_dimensions["height"] == 600, f"Expected height 600, got {current_dimensions['height']}"
672
673
driver.emulation.set_screen_settings_override(user_contexts=[user_context])
674
675
restored_dimensions = get_screen_dimensions(driver, context_id)
676
assert restored_dimensions == initial_dimensions, (
677
f"Expected dimensions to be restored to {initial_dimensions}"
678
)
679
finally:
680
driver.browsing_context.close(context_id)
681
finally:
682
driver.browser.remove_user_context(user_context)
683
684