Skip to content

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.

  • Implemented integration test for activate method functionality.

  • Updated type signature for activate method in RBS file.


Changes walkthrough 📝

Relevant files
Enhancement
browsing_context.rb
Introduced `activate` method in `BrowsingContext`               

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
    Updated RBS signature for `activate` method                           

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

  • Updated RBS type signature for activate method.
  • Defined optional context_id parameter and return type.
  • +2/-0     
    Tests
    browsing_context_spec.rb
    Added 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 JavaScript focus check.
  • +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.
  • @aguspe aguspe added the C-rb Ruby Bindings label Mar 3, 2025
    @qodo-merge-pro
    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 IDs or when activation fails. Consider adding rescue blocks to handle potential exceptions.

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

    The test only verifies basic activation. Consider adding tests for error cases like invalid context IDs and multiple window scenarios.

    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

    @qodo-merge-pro
    Copy link
    Contributor

    qodo-merge-pro bot commented Mar 3, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Add wait for async focus change

    Add a small wait/sleep after activation to ensure focus state has propagated
    before checking. Focus changes can be asynchronous in browsers.

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

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

    __

    Why: Adding a small wait is crucial for test reliability since browser focus changes can be asynchronous, preventing flaky test failures. Without the wait, the test could fail intermittently when focus hasn't fully propagated.

    Medium
    Learned
    best practice
    Add parameter validation to ensure required values are present before method execution

    Add validation to ensure @bidi and context_id are not nil before using them in
    the activate method. This helps catch potential issues early with clear error
    messages.

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

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

    @aguspe aguspe closed this Mar 3, 2025
    @aguspe aguspe deleted the rb_bidi_locate_nodes branch March 3, 2025 15:24
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Labels

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    1 participant