Skip to content

Conversation

@navin772
Copy link
Member

@navin772 navin772 commented Oct 24, 2025

User description

🔗 Related Issues

💥 What does this PR do?

Adds support for set_locale_override commands from the Emulation module - https://w3c.github.io/webdriver-bidi/#command-emulation-setLocaleOverride

🔧 Implementation Notes

Usage:

  1. User context
    driver.emulation.set_locale_override(locale="en-US", user_contexts=[user_context])
  2. Browsing contexts
    driver.emulation.set_locale_override(locale="zh-Hans-CN", contexts=[context_id])

💡 Additional Considerations

Initially, I thought of adding validation of locale but it added a new dep - langcodes which is not good. Since validation is done on the remote end, it is not required and any error coming from validation from remote end will be propagated by selenium as WebdriverException to the user's terminal.

🔄 Types of changes

  • New feature (non-breaking change which adds functionality and tests!)

PR Type

Enhancement


Description

  • Add set_locale_override command to emulation module

  • Support both browsing contexts and user contexts

  • Include comprehensive parametrized tests for various locales

  • Validate mutually exclusive context parameters


Diagram Walkthrough

flowchart LR
  A["Emulation Module"] -->|"add method"| B["set_locale_override"]
  B -->|"accepts"| C["locale parameter"]
  B -->|"accepts"| D["contexts or user_contexts"]
  D -->|"validates"| E["mutual exclusivity"]
  B -->|"executes"| F["emulation.setLocaleOverride command"]
  G["Test Suite"] -->|"parametrized tests"| H["Multiple locale formats"]
  G -->|"tests"| I["Browsing contexts"]
  G -->|"tests"| J["User contexts"]
Loading

File Walkthrough

Relevant files
Enhancement
emulation.py
Implement set_locale_override emulation command                   

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

  • Add new set_locale_override method to the Emulation class
  • Accept optional locale parameter (BCP 47 format or None to clear)
  • Support both contexts and user_contexts parameters
  • Validate that only one of contexts or user_contexts is provided
  • Execute the emulation.setLocaleOverride command via BiDi
+34/-0   
Tests
bidi_emulation_tests.py
Add comprehensive tests for locale override functionality

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

  • Add helper function get_browser_locale to retrieve current browser
    locale
  • Add parametrized test for locale override with browsing contexts (5
    locale variants)
  • Add parametrized test for locale override with user contexts (4 locale
    variants)
  • Test locale normalization and various BCP 47 format combinations
+68/-0   

@selenium-ci selenium-ci added C-py Python Bindings B-devtools Includes everything BiDi or Chrome DevTools related labels Oct 24, 2025
@qodo-merge-pro
Copy link
Contributor

qodo-merge-pro bot commented Oct 24, 2025

PR Compliance Guide 🔍

Below is a summary of compliance checks for this PR:

Security Compliance
🟢
No security concerns identified No security vulnerabilities detected by AI analysis. Human verification advised for critical code.
Ticket Compliance
🟡
🎫 #5678
🔴 Investigate and resolve repeated "Error: ConnectFailure (Connection refused)" when
instantiating multiple ChromeDriver instances on Ubuntu 16.04.4 with Chrome
65/Chromedriver 2.35 using Selenium 3.9.0.
Provide guidance or a fix so subsequent ChromeDriver instantiations do not log connection
failure errors.
Ensure any solution is validated against the specified environment and versions or
document limitations.
🟡
🎫 #1234
🔴 Ensure WebDriver click() triggers JavaScript in link href for Firefox (reported regression
from 2.47.1 to 2.48.x on Firefox 42).
Provide a fix or regression test guaranteeing JS href execution on click in affected
versions.
Codebase Duplication Compliance
Codebase context is not defined

Follow the guide to enable codebase context checks.

Custom Compliance
No custom compliance provided

Follow the guide to enable custom compliance check.

  • Update
Compliance status legend 🟢 - Fully Compliant
🟡 - Partial Compliant
🔴 - Not Compliant
⚪ - Requires Further Human Verification
🏷️ - Compliance label

@qodo-merge-pro
Copy link
Contributor

qodo-merge-pro bot commented Oct 24, 2025

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
Allow global locale override by default

Remove the check that raises a ValueError when both contexts and user_contexts
are None to align with the BiDi specification for a global locale override.

py/selenium/webdriver/common/bidi/emulation.py [276-277]

-if contexts is None and user_contexts is None:
-    raise ValueError("Must specify either contexts or userContexts")
+# This block should be removed.
  • Apply / Chat
Suggestion importance[1-10]: 8

__

Why: The suggestion correctly identifies that the implementation deviates from the BiDi specification, which states that omitting both contexts and userContexts should apply a global override, not raise an error.

Medium
Learned
best practice
Add robust test cleanup
Suggestion Impact:The test was refactored to wrap creation and use of the tab and user context in nested try/finally blocks, ensuring driver.browsing_context.close and driver.browser.remove_user_context are always executed.

code diff:

+    try:
+        context_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context)
+        try:
+            driver.switch_to.window(context_id)
+
+            driver.emulation.set_locale_override(locale=value, user_contexts=[user_context])
+
+            driver.browsing_context.navigate(context_id, pages.url("formPage.html"), wait="complete")
+
+            current_locale = get_browser_locale(driver)
+            assert current_locale == value, f"Expected locale {value}, got {current_locale}"
+        finally:
+            driver.browsing_context.close(context_id)
+    finally:
+        driver.browser.remove_user_context(user_context)

Ensure the tab and user context are cleaned up even if assertions fail by
wrapping them in try/finally blocks.

py/test/selenium/webdriver/common/bidi_emulation_tests.py [347-363]

 def test_set_locale_override_with_user_contexts(driver, pages, value):
     """Test setting locale override with user contexts."""
     user_context = driver.browser.create_user_context()
+    try:
+        context_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context)
+        try:
+            driver.switch_to.window(context_id)
+            driver.emulation.set_locale_override(locale=value, user_contexts=[user_context])
+            driver.browsing_context.navigate(context_id, pages.url("formPage.html"), wait="complete")
+            current_locale = get_browser_locale(driver)
+            assert current_locale == value, f"Expected locale {value}, got {current_locale}"
+        finally:
+            driver.browsing_context.close(context_id)
+    finally:
+        driver.browser.remove_user_context(user_context)
 
-    context_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context)
-
-    driver.switch_to.window(context_id)
-
-    driver.emulation.set_locale_override(locale=value, user_contexts=[user_context])
-
-    driver.browsing_context.navigate(context_id, pages.url("formPage.html"), wait="complete")
-
-    current_locale = get_browser_locale(driver)
-    assert current_locale == value, f"Expected locale {value}, got {current_locale}"
-
-    driver.browsing_context.close(context_id)
-    driver.browser.remove_user_context(user_context)
-

[Suggestion processed]

Suggestion importance[1-10]: 6

__

Why:
Relevant best practice - Enforce deterministic cleanup of created resources in tests to avoid leaks and cross-test interference.

Low
  • Update

Copy link
Member

@cgoldberg cgoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
The 2nd AI suggestion (test cleanup) might be worth doing.

Nice use of parametrize in the tests!

@navin772
Copy link
Member Author

Leaving a note here for future reference:

The BiDi emulation command set_locale_override should also override the location retrieved from navigator.language/s, but as of now it's not doing that.
There is a bug for it - https://bugzilla.mozilla.org/show_bug.cgi?id=1994396 and a corresponding PR.

So, when its merged and released, one should be able to override navigator.language too.
Currently, in the tests we are using the Intl API - Intl.DateTimeFormat().resolvedOptions().locale to retrieve locale, which works fine.

Related work and PR:

  1. Add hook to emulate navigator.language/s w3c/webdriver-bidi#1017
  2. Add checks for "navigator.language/s" to tests for "emulation.setLocaleOverride" command. web-platform-tests/wpt#55453

@navin772 navin772 merged commit b3a3989 into SeleniumHQ:trunk Oct 27, 2025
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

B-devtools Includes everything BiDi or Chrome DevTools related C-py Python Bindings Review effort 2/5

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants