Skip to content

Commit b3a3989

Browse files
authored
[py][bidi]: add emulation command - set_locale_override (#16504)
1 parent d3293d4 commit b3a3989

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,37 @@ def set_timezone_override(
250250
params["userContexts"] = user_contexts
251251

252252
self.conn.execute(command_builder("emulation.setTimezoneOverride", params))
253+
254+
def set_locale_override(
255+
self,
256+
locale: Optional[str] = None,
257+
contexts: Optional[list[str]] = None,
258+
user_contexts: Optional[list[str]] = None,
259+
) -> None:
260+
"""Set locale override for the given contexts or user contexts.
261+
262+
Parameters:
263+
-----------
264+
locale: Locale string as per BCP 47, or None to clear override.
265+
contexts: List of browsing context IDs to apply the override to.
266+
user_contexts: List of user context IDs to apply the override to.
267+
268+
Raises:
269+
------
270+
ValueError: If both contexts and user_contexts are provided, or if neither
271+
contexts nor user_contexts are provided, or if locale is invalid.
272+
"""
273+
if contexts is not None and user_contexts is not None:
274+
raise ValueError("Cannot specify both contexts and userContexts")
275+
276+
if contexts is None and user_contexts is None:
277+
raise ValueError("Must specify either contexts or userContexts")
278+
279+
params: dict[str, Any] = {"locale": locale}
280+
281+
if contexts is not None:
282+
params["contexts"] = contexts
283+
elif user_contexts is not None:
284+
params["userContexts"] = user_contexts
285+
286+
self.conn.execute(command_builder("emulation.setLocaleOverride", params))

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ def get_browser_geolocation(driver, user_context=None):
6464
""")
6565

6666

67+
def get_browser_locale(driver):
68+
result = driver.script._evaluate(
69+
"Intl.DateTimeFormat().resolvedOptions().locale",
70+
{"context": driver.current_window_handle},
71+
await_promise=False,
72+
)
73+
return result.result["value"]
74+
75+
6776
def test_emulation_initialized(driver):
6877
"""Test that the emulation module is initialized properly."""
6978
assert driver.emulation is not None
@@ -293,3 +302,63 @@ def test_set_timezone_override_using_offset(driver, pages):
293302
assert timezone_string == "+05:30", f"Expected timezone '+05:30', got: {timezone_string}"
294303

295304
driver.emulation.set_timezone_override(timezone=None, contexts=[context_id])
305+
306+
307+
@pytest.mark.parametrize(
308+
"locale,expected_locale",
309+
[
310+
# Locale with Unicode extension keyword for collation.
311+
("de-DE-u-co-phonebk", "de-DE"),
312+
# Lowercase language and region.
313+
("fr-ca", "fr-CA"),
314+
# Uppercase language and region (should be normalized by Intl.Locale).
315+
("FR-CA", "fr-CA"),
316+
# Mixed case language and region (should be normalized by Intl.Locale).
317+
("fR-cA", "fr-CA"),
318+
# Locale with transform extension (simple case).
319+
("en-t-zh", "en"),
320+
],
321+
)
322+
def test_set_locale_override_with_contexts(driver, pages, locale, expected_locale):
323+
"""Test setting locale override with browsing contexts."""
324+
context_id = driver.current_window_handle
325+
326+
driver.emulation.set_locale_override(locale=locale, contexts=[context_id])
327+
328+
driver.browsing_context.navigate(context_id, pages.url("formPage.html"), wait="complete")
329+
330+
current_locale = get_browser_locale(driver)
331+
assert current_locale == expected_locale, f"Expected locale {expected_locale}, got {current_locale}"
332+
333+
334+
@pytest.mark.parametrize(
335+
"value",
336+
[
337+
# Simple language code (2-letter).
338+
"en",
339+
# Language and region (both 2-letter).
340+
"en-US",
341+
# Language and script (4-letter).
342+
"sr-Latn",
343+
# Language, script, and region.
344+
"zh-Hans-CN",
345+
],
346+
)
347+
def test_set_locale_override_with_user_contexts(driver, pages, value):
348+
"""Test setting locale override with user contexts."""
349+
user_context = driver.browser.create_user_context()
350+
try:
351+
context_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context)
352+
try:
353+
driver.switch_to.window(context_id)
354+
355+
driver.emulation.set_locale_override(locale=value, user_contexts=[user_context])
356+
357+
driver.browsing_context.navigate(context_id, pages.url("formPage.html"), wait="complete")
358+
359+
current_locale = get_browser_locale(driver)
360+
assert current_locale == value, f"Expected locale {value}, got {current_locale}"
361+
finally:
362+
driver.browsing_context.close(context_id)
363+
finally:
364+
driver.browser.remove_user_context(user_context)

0 commit comments

Comments
 (0)