Skip to content

[py][bidi]: implement bidi module - emulation #15819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: trunk
Choose a base branch
from

Conversation

navin772
Copy link
Member

@navin772 navin772 commented May 29, 2025

User description

🔗 Related Issues

💥 What does this PR do?

Adds support for the emulation module in python bindings - https://w3c.github.io/webdriver-bidi/#module-emulation

🔧 Implementation Notes

Usage:

  1. From contexts:

    from selenium.webdriver.common.bidi.emulation import GeolocationCoordinates
    
    context_id = driver.current_window_handle
    coords = GeolocationCoordinates(45.5, -122.4194, accuracy=10.0)
    
    driver.emulation.set_geolocation_override(coordinates=coords, contexts=[context_id])
  2. From user_contexts:

    from selenium.webdriver.common.bidi.emulation import GeolocationCoordinates
    
    user_context = driver.browser.create_user_context()
    context_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context)
    driver.switch_to.window(context_id)
    
    coords = GeolocationCoordinates(45.5, -122.4194, accuracy=10.0, altitude=100.2, altitude_accuracy=5.0, heading=183.2, speed=10.0)
    
    driver.emulation.set_geolocation_override(coordinates=coords, user_contexts=[user_context])

Important

Either user_contexts or contexts should be passed, both cannot coexist.

💡 Additional Considerations

The tests need the BiDi permissions module to grant the geolocation permission in order to fetch them.

Note

Currently, when the Error parameter is passed, it is returning an error invalid argument: Invalid input in "coordinates, hence the test is commented out for now.
It is fixed in chrome canary.

🔄 Types of changes

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

PR Type

Enhancement


Description

• Implement BiDi emulation module for Python bindings
• Add geolocation override functionality with coordinates support
• Include comprehensive test coverage for contexts and user contexts
• Support multiple browsing contexts and user contexts


Changes walkthrough 📝

Relevant files
Enhancement
emulation.py
New BiDi emulation module implementation                                 

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

• Create new emulation module with GeolocationCoordinates and
GeolocationPositionError classes
• Implement set_geolocation_override
method supporting contexts and user_contexts
• Add comprehensive
validation for coordinate ranges and parameter combinations
• Include
proper error handling and type annotations

+162/-0 
webdriver.py
WebDriver emulation property integration                                 

py/selenium/webdriver/remote/webdriver.py

• Add emulation property to WebDriver class
• Import Emulation module
and initialize _emulation attribute
• Provide lazy initialization with
BiDi connection setup

+24/-0   
Tests
bidi_emulation_tests.py
Comprehensive emulation module test suite                               

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

• Add comprehensive test suite for emulation module functionality

Test geolocation override with coordinates in contexts and user
contexts
• Include tests for multiple contexts and all coordinate
parameters
• Add helper function for browser geolocation testing

+214/-0 

Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • @selenium-ci selenium-ci added C-py Python Bindings B-devtools Includes everything BiDi or Chrome DevTools related labels May 29, 2025
    @navin772
    Copy link
    Member Author

    The tests require geolocation permission access which can be done once #15830 is merged.

    @navin772 navin772 force-pushed the py-bidi-emulation branch from b8d7862 to 18eeca9 Compare May 30, 2025 18:48
    @navin772 navin772 marked this pull request as ready for review June 10, 2025 18:42
    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    🎫 Ticket compliance analysis ❌

    5678 - Not compliant

    Non-compliant requirements:

    • Fix ChromeDriver connection failure error that occurs after first instance
    • Resolve "ConnectFailure (Connection refused)" error for subsequent ChromeDriver instances
    • Ensure proper ChromeDriver instantiation without connection issues

    1234 - Not compliant

    Non-compliant requirements:

    • Fix JavaScript execution in link href on click() method for Selenium 2.48+
    • Ensure JavaScript alerts are triggered when clicking links with JavaScript in href
    • Restore functionality that worked in Selenium 2.47.1 but broke in 2.48.0/2.48.2

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Validation Logic

    The heading validation allows 0.0 to 360.0 but excludes 360.0 with the condition 0.0 <= heading < 360.0. This may be inconsistent with geolocation standards where 360.0 degrees should be equivalent to 0.0 degrees.

    if heading is not None and not (0.0 <= heading < 360.0):
        raise ValueError("Heading must be between 0.0 and 360.0")
    Resource Cleanup

    Test functions create browsing contexts and user contexts but some tests don't properly clean up all created resources, which could lead to resource leaks in test environments.

    driver.browsing_context.close(context_id)

    Copy link
    Contributor

    qodo-merge-pro bot commented Jun 10, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Remove invalid context close

    This line attempts to close context_id which is the current window handle, not a
    created browsing context. This will likely cause an error since context_id
    wasn't created by browsing_context.create().

    py/test/selenium/webdriver/common/bidi_emulation_tests.py [120]

    -driver.browsing_context.close(context_id)
    +# Remove this line as context_id is the current window handle, not a created context
    • Apply / Chat
    Suggestion importance[1-10]: 7

    __

    Why: The suggestion correctly identifies a bug in the test test_set_geolocation_override_all_coords. The variable context_id holds the current window handle, not a newly created browsing context. Attempting to close it is incorrect and could cause test suite instability.

    Medium
    • Update

    @navin772 navin772 requested a review from shbenzer June 10, 2025 18:45
    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.

    2 participants