[py][bidi]: implement bidi module - emulation by navin772 · Pull Request #15819 · SeleniumHQ/selenium

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.