Skip to content

Conversation

@Delta456
Copy link
Member

@Delta456 Delta456 commented Mar 21, 2025

User description

Thanks for contributing to Selenium!
A PR well described will help maintainers to quickly review and merge it

Before submitting your PR, please check our contributing guidelines.
Avoid large PRs, help reviewers by making them as simple and short as possible.

Motivation and Context

By having the --websocket-port parameter by default we avoid port collisions when using BiDi with firefox

Fixes #15451 for JS binding

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

Bug fix, Enhancement


Description

  • Added --websocket-port flag to avoid port collisions in Firefox.

  • Integrated portprober to dynamically find free ports.

  • Modified ServiceBuilder to include the new flag when not connecting to an existing connection.


Changes walkthrough 📝

Relevant files
Enhancement
firefox.js
Add dynamic `--websocket-port` flag in Firefox service     

javascript/node/selenium-webdriver/firefox.js

  • Imported portprober for finding free ports.
  • Added logic in ServiceBuilder to append --websocket-port with a free
    port.
  • Ensured the flag is added only when not connecting to an existing
    connection.
  • +5/-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.
  • @qodo-merge-pro
    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    🎫 Ticket compliance analysis ✅

    15451 - PR Code Verified

    Compliant requirements:

    • Add the websocket-port argument to avoid port collisions in Firefox
    • Implement the fix for JavaScript bindings

    Requires further human verification:

    • Verify that the implementation actually fixes the issue with multiple sessions on the same geckodriver

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

    Reference Error

    The code uses self.args but self is not defined in this context. It should be using this.args instead.

    if (!self.args.includes('--connect-existing')) {
        self.args.append('--websocket-port')
        self.args.append(`${portprober.findFreePort()}`)
    }
    Incorrect Method

    The code uses append method on args array, but JavaScript arrays use push method, not append.

    self.args.append('--websocket-port')
    self.args.append(`${portprober.findFreePort()}`)

    @qodo-merge-pro
    Copy link
    Contributor

    qodo-merge-pro bot commented Mar 21, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Fix array method usage
    Suggestion Impact:The commit partially implemented the suggestion by changing 'self' to 'this' in the condition and method calls, but did not replace the append() method with push() as suggested.

    code diff:

    -    if (!self.args.includes('--connect-existing')) {
    -        self.args.append('--websocket-port')
    -        self.args.append(`${portprober.findFreePort()}`)
    +    if (!this.args.includes('--connect-existing')) {
    +      this.args.append('--websocket-port')
    +      this.args.append(`${portprober.findFreePort()}`)

    JavaScript arrays use push() method, not append(). Replace the append() calls
    with push() to correctly add the arguments to the array.

    javascript/node/selenium-webdriver/firefox.js [496-499]

    -if (!self.args.includes('--connect-existing')) {
    -    self.args.append('--websocket-port')
    -    self.args.append(`${portprober.findFreePort()}`)
    +if (!this.args.includes('--connect-existing')) {
    +    this.args.push('--websocket-port')
    +    this.args.push(`${portprober.findFreePort()}`)
     }

    [Suggestion has been applied]

    Suggestion importance[1-10]: 10

    __

    Why: This is a critical bug fix. JavaScript arrays use push() not append(), so the current code would throw an error and completely break the functionality. This correction is essential for the code to work at all.

    High
    Fix object reference
    Suggestion Impact:The commit implemented the suggestion by replacing 'self' with 'this' in the class method to correctly reference the current instance, fixing a potential reference error

    code diff:

    -    if (!self.args.includes('--connect-existing')) {
    -        self.args.append('--websocket-port')
    -        self.args.append(`${portprober.findFreePort()}`)
    +    if (!this.args.includes('--connect-existing')) {
    +      this.args.append('--websocket-port')
    +      this.args.append(`${portprober.findFreePort()}`)

    Use this instead of self to reference the current instance in JavaScript class
    methods. self is undefined in this context and will cause a reference error.

    javascript/node/selenium-webdriver/firefox.js [496]

    -if (!self.args.includes('--connect-existing')) {
    +if (!this.args.includes('--connect-existing')) {

    [Suggestion has been applied]

    Suggestion importance[1-10]: 10

    __

    Why: Using 'self' instead of 'this' in a JavaScript class method is a critical error that would cause a reference error, as 'self' is undefined in this context. This fix is essential for the code to function.

    High
    Handle asynchronous port finding
    Suggestion Impact:The commit completely removed the code that was using the problematic findFreePort() method, which effectively addresses the issue raised in the suggestion. Instead of replacing it with findFreePortSync() as suggested, the developers chose to remove the entire block of code that was using portprober.

    code diff:

    -const portprober = require("./net/portprober");
     const FIREFOX_CAPABILITY_KEY = 'moz:firefoxOptions'
     
     /**
    @@ -493,10 +492,6 @@
        */
       constructor(opt_exe) {
         super(opt_exe)
    -    if (!self.args.includes('--connect-existing')) {
    -        self.args.append('--websocket-port')
    -        self.args.append(`${portprober.findFreePort()}`)
    -    }

    The findFreePort() method is likely asynchronous and returns a Promise. You need
    to handle this asynchronously or use an alternative synchronous method to find a
    free port.

    javascript/node/selenium-webdriver/firefox.js [498]

    -self.args.append(`${portprober.findFreePort()}`)
    +this.args.push(portprober.findFreePortSync())

    [Suggestion has been applied]

    Suggestion importance[1-10]: 7

    __

    Why: If findFreePort() is indeed asynchronous, this would cause issues with port assignment. The suggestion correctly identifies a potential problem, but without confirming the actual API of portprober, we can't be certain this is a critical issue.

    Medium
    • Update

    @Delta456
    Copy link
    Member Author

    Closing this PR because commit c6210c1 makes a huge merge conflict which can only be solved by opening a new PR.

    @Delta456 Delta456 closed this Mar 26, 2025
    @Delta456 Delta456 deleted the js_connect_exist branch March 26, 2025 10:30
    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.

    [🐛 Bug]: Add argument for websocket-port

    1 participant