Skip to content
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

[rb][BiDi] Add Browser context activate command #15365

Merged
merged 4 commits into from
Mar 5, 2025

Conversation

aguspe
Copy link
Contributor

@aguspe aguspe commented Mar 3, 2025

User description

Motivation and Context

This change is added to add full support for the BiDi protocol for our Ruby bindings

For more documentation regarding the activate command:

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the contributing document.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

PR Type

Enhancement, Tests


Description

  • Added activate method to BrowsingContext for BiDi protocol support.

  • Implemented integration test for activate method in BrowsingContext.

  • Updated type signature for activate method in BrowsingContext RBS file.


Changes walkthrough 📝

Relevant files
Enhancement
browsing_context.rb
Add `activate` method to `BrowsingContext` class                 

rb/lib/selenium/webdriver/bidi/browsing_context.rb

  • Added activate method to BrowsingContext class.
  • Sends browsingContext.activate command with context ID.
  • +5/-0     
    browsing_context.rbs
    Update type signature for `activate` method                           

    rb/sig/lib/selenium/webdriver/bidi/browsing_context.rbs

  • Updated type signature for activate method.
  • Defined activate method with optional context_id parameter.
  • +2/-0     
    Tests
    browsing_context_spec.rb
    Add integration test for `activate` method                             

    rb/spec/integration/selenium/webdriver/bidi/browsing_context_spec.rb

  • Added integration test for activate method.
  • Verified browser context activation using document.hasFocus().
  • +11/-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.
  • Copy link
    Contributor

    qodo-merge-pro bot commented Mar 3, 2025

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

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

    Error Handling

    The activate method should include error handling for invalid context_id or failed activation attempts

    def activate(context_id: nil)
      context_id ||= @bridge.window_handle
      @bidi.send_cmd('browsingContext.activate', context: context_id)
    end
    Test Coverage

    The test should verify behavior with invalid context_id and multiple browser contexts

    it 'activates a browser context' do
      reset_driver!(web_socket_url: true) do |driver|
        browsing_context = described_class.new(driver)
        browsing_context.create
    
        expect(driver.execute_script('return document.hasFocus();')).to be_falsey
        browsing_context.activate
        expect(driver.execute_script('return document.hasFocus();')).to be_truthy
      end
    end

    Copy link
    Contributor

    qodo-merge-pro bot commented Mar 3, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Add delay for focus check

    Add a small delay after activation before checking focus state, as focus changes
    may not be instantaneous in browser

    rb/spec/integration/selenium/webdriver/bidi/browsing_context_spec.rb [92-93]

     browsing_context.activate
    +sleep 0.1 # Small delay to allow focus change to take effect
     expect(driver.execute_script('return document.hasFocus();')).to be_truthy
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    __

    Why: Adding a small delay is crucial for test reliability since browser focus changes can be asynchronous. Without the delay, the test may fail intermittently due to race conditions.

    Medium
    Learned
    best practice
    Add validation to ensure required instance variables are initialized before use to prevent unclear null reference errors

    Add validation for the @bidi instance variable before using it to send commands.
    This ensures we fail early with a clear error message if the BiDi connection is
    not properly initialized.

    rb/lib/selenium/webdriver/bidi/browsing_context.rb [104-107]

     def activate(context_id: nil)
    +  raise ArgumentError, '@bidi connection not initialized' if @bidi.nil?
       context_id ||= @bridge.window_handle
       @bidi.send_cmd('browsingContext.activate', context: context_id)
     end
    • Apply this suggestion
    Suggestion importance[1-10]: 6
    Low
    • Update

    @aguspe aguspe changed the title [rb][BiDi] activate command [rb][BiDi] Add Browser context activate command Mar 3, 2025
    @aguspe aguspe requested review from p0deje and luke-hill March 4, 2025 18:59
    Copy link
    Contributor

    @luke-hill luke-hill left a comment

    Choose a reason for hiding this comment

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

    Good test. I'm guessing in reality this is bringing the window up into focus and loading the about:blank or equivalent page? (i've not seen this bidi command before).

    @aguspe
    Copy link
    Contributor Author

    aguspe commented Mar 5, 2025

    Good test. I'm guessing in reality this is bringing the window up into focus and loading the about:blank or equivalent page? (i've not seen this bidi command before).

    Yes exactly

    @aguspe aguspe merged commit e69d5c5 into SeleniumHQ:trunk Mar 5, 2025
    23 checks passed
    sandeepsuryaprasad pushed a commit to sandeepsuryaprasad/selenium that referenced this pull request Mar 23, 2025
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants