Skip to content

[py][bidi]: add emulation command set_screen_settings_override#17030

Merged
navin772 merged 5 commits intoSeleniumHQ:trunkfrom
navin772:py-emulation-setScreenSettingsOverride
Feb 2, 2026
Merged

[py][bidi]: add emulation command set_screen_settings_override#17030
navin772 merged 5 commits intoSeleniumHQ:trunkfrom
navin772:py-emulation-setScreenSettingsOverride

Conversation

@navin772
Copy link
Copy Markdown
Member

🔗 Related Issues

💥 What does this PR do?

Adds support for emulation.setScreenSettingsOverride to the python bindings - https://w3c.github.io/webdriver-bidi/#command-emulation-setScreenSettingsOverride

🔧 Implementation Notes

💡 Additional Considerations

🔄 Types of changes

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

@selenium-ci selenium-ci added C-py Python Bindings B-devtools Includes everything BiDi or Chrome DevTools related labels Jan 31, 2026
@qodo-code-review
Copy link
Copy Markdown
Contributor

PR Type

Enhancement


Description

  • Add set_screen_settings_override method to emulation API

  • Supports screen dimension override via contexts or user contexts

  • Includes comprehensive validation for width/height parameters

  • Add tests for both contexts and user contexts scenarios


File Walkthrough

Relevant files
Enhancement
emulation.py
Add set_screen_settings_override emulation method               

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

  • Implement set_screen_settings_override method with width/height
    parameters
  • Add validation for parameter combinations (width/height must be both
    or neither)
  • Add validation for contexts vs user_contexts (mutually exclusive, at
    least one required)
  • Add type checking and range validation for width/height (must be
    non-negative integers)
+45/-0   
Tests
bidi_emulation_tests.py
Add tests for screen settings override functionality         

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

  • Add helper function get_screen_dimensions to retrieve screen
    width/height via script evaluation
  • Add test test_set_screen_settings_override_with_contexts for
    context-based override
  • Add test test_set_screen_settings_override_with_user_contexts for user
    context-based override
  • Both tests verify override application and restoration to initial
    dimensions
+61/-0   

@qodo-code-review
Copy link
Copy Markdown
Contributor

qodo-code-review bot commented Jan 31, 2026

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
🟡
🎫 #1234
🔴 Ensure click() triggers JavaScript in a link's href (regression between Selenium 2.47.1
and 2.48.x) when using Firefox.
🟡
🎫 #5678
🔴 Fix or prevent ChromeDriver "ConnectFailure (Connection refused)" errors when
instantiating multiple driver instances on Ubuntu/Chrome.
Codebase Duplication Compliance
Codebase context is not defined

Follow the guide to enable codebase context checks.

Custom Compliance
🟢
Generic: Comprehensive Audit Trails

Objective: To create a detailed and reliable record of critical system actions for security analysis
and compliance.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Meaningful Naming and Self-Documenting Code

Objective: Ensure all identifiers clearly express their purpose and intent, making code
self-documenting

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Robust Error Handling and Edge Case Management

Objective: Ensure comprehensive error handling that provides meaningful context and graceful
degradation

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Error Handling

Objective: To prevent the leakage of sensitive system information through error messages while
providing sufficient detail for internal debugging.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Logging Practices

Objective: To ensure logs are useful for debugging and auditing without exposing sensitive
information like PII, PHI, or cardholder data.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Security-First Input Validation and Data Handling

Objective: Ensure all data inputs are validated, sanitized, and handled securely to prevent
vulnerabilities

Status:
Incomplete input validation: The new set_screen_settings_override validates width/height but does not validate that
contexts/user_contexts are non-empty lists of strings before sending them over the BiDi
connection.

Referred Code
if contexts is not None:
    params["contexts"] = contexts
elif user_contexts is not None:
    params["userContexts"] = user_contexts

Learn more about managing compliance generic rules or creating your own custom rules

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

@qodo-code-review
Copy link
Copy Markdown
Contributor

qodo-code-review bot commented Jan 31, 2026

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
Correctly construct command parameters for override

Initialize params as an empty dictionary and only add the screenArea key if
width and height are provided. This ensures the override is cleared correctly by
omitting the parameter instead of sending null.

py/selenium/webdriver/common/bidi/emulation.py [509-517]

-screen_area = None
+params: dict[str, Any] = {}
 if width is not None and height is not None:
     if not isinstance(width, int) or not isinstance(height, int):
         raise ValueError("width and height must be integers")
     if width < 0 or height < 0:
         raise ValueError("width and height must be >= 0")
-    screen_area = {"width": width, "height": height}
+    params["screenArea"] = {"width": width, "height": height}
 
-params: dict[str, Any] = {"screenArea": screen_area}
-
  • Apply / Chat
Suggestion importance[1-10]: 8

__

Why: This suggestion correctly identifies a potential bug where sending {"screenArea": null} might not clear the override as intended, and proposes a fix that aligns with common BiDi protocol patterns, improving the method's correctness.

Medium
General
Simplify test structure by removing nesting

Simplify the test structure by replacing the nested try...finally blocks with a
single block. This improves readability by reducing nesting while ensuring
proper resource cleanup.

py/test/selenium/webdriver/common/bidi_emulation_tests.py [655-682]

 @pytest.mark.xfail_chrome
 @pytest.mark.xfail_edge
 def test_set_screen_settings_override_with_user_contexts(driver, pages):
     user_context = driver.browser.create_user_context()
+    context_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context)
     try:
-        context_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context)
-        try:
-            driver.switch_to.window(context_id)
-            driver.browsing_context.navigate(context_id, pages.url("formPage.html"), wait="complete")
+        driver.switch_to.window(context_id)
+        driver.browsing_context.navigate(context_id, pages.url("formPage.html"), wait="complete")
 
-            initial_dimensions = get_screen_dimensions(driver, context_id)
+        initial_dimensions = get_screen_dimensions(driver, context_id)
 
-            driver.emulation.set_screen_settings_override(width=800, height=600, user_contexts=[user_context])
+        driver.emulation.set_screen_settings_override(width=800, height=600, user_contexts=[user_context])
 
-            current_dimensions = get_screen_dimensions(driver, context_id)
-            assert current_dimensions["width"] == 800, f"Expected width 800, got {current_dimensions['width']}"
-            assert current_dimensions["height"] == 600, f"Expected height 600, got {current_dimensions['height']}"
+        current_dimensions = get_screen_dimensions(driver, context_id)
+        assert current_dimensions["width"] == 800, f"Expected width 800, got {current_dimensions['width']}"
+        assert current_dimensions["height"] == 600, f"Expected height 600, got {current_dimensions['height']}"
 
-            driver.emulation.set_screen_settings_override(user_contexts=[user_context])
+        driver.emulation.set_screen_settings_override(user_contexts=[user_context])
 
-            restored_dimensions = get_screen_dimensions(driver, context_id)
-            assert restored_dimensions == initial_dimensions, (
-                f"Expected dimensions to be restored to {initial_dimensions}"
-            )
-        finally:
-            driver.browsing_context.close(context_id)
+        restored_dimensions = get_screen_dimensions(driver, context_id)
+        assert restored_dimensions == initial_dimensions, (
+            f"Expected dimensions to be restored to {initial_dimensions}"
+        )
     finally:
+        driver.browsing_context.close(context_id)
         driver.browser.remove_user_context(user_context)
  • Apply / Chat
Suggestion importance[1-10]: 4

__

Why: The suggestion improves code readability and structure by removing unnecessary nested try...finally blocks in the test, making the cleanup logic more straightforward.

Low
  • Update

@navin772 navin772 requested a review from cgoldberg January 31, 2026 16:41
Copy link
Copy Markdown
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

Copilot AI review requested due to automatic review settings February 1, 2026 21:02
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@navin772 navin772 merged commit 923eb53 into SeleniumHQ:trunk Feb 2, 2026
5 of 6 checks passed
@navin772 navin772 deleted the py-emulation-setScreenSettingsOverride branch February 2, 2026 07:24
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 3/5

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants