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

[dotnet] Fix JavaScriptEngine.ScriptCallbackBindings not containing new bindings #15221

Merged
merged 8 commits into from
Feb 3, 2025

Conversation

RenderMichael
Copy link
Contributor

@RenderMichael RenderMichael commented Feb 3, 2025

User description

… new bindings

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

If a use accesses JavaScriptEngine.ScriptCallbackBindings, the list will always be empty.

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, Tests


Description

  • Fixed JavaScriptEngine.ScriptCallbackBindings to correctly track bindings.

  • Replaced List<string> with HashSet<string> for bindings to ensure uniqueness.

  • Added comprehensive tests for initialization scripts and script callback bindings.

  • Improved resource management with using statements in tests.


Changes walkthrough 📝

Relevant files
Bug fix
JavaScriptEngine.cs
Refactor bindings to use `HashSet` for uniqueness

dotnet/src/webdriver/JavaScriptEngine.cs

  • Changed bindings from List to HashSet for uniqueness.
  • Updated AddScriptCallbackBinding to use HashSet.Add for validation.
  • Modified RemoveScriptCallbackBinding to discard the result of Remove.
  • +3/-3     
    Tests
    ExecutingJavascriptTest.cs
    Add tests for script bindings and initialization scripts 

    dotnet/test/common/ExecutingJavascriptTest.cs

  • Added tests for initialization scripts and script callback bindings.
  • Verified behavior of adding, removing, and clearing script bindings.
  • Improved resource management using using for JavaScriptEngine.
  • Added validation for script execution and alert handling.
  • +102/-1 

    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 Feb 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 RemoveScriptCallbackBinding method discards the result of Remove operation without checking if the binding existed, which could mask potential issues

    await this.session.Value.Domains.JavaScript.RemoveBinding(bindingName).ConfigureAwait(false);
    _ = this.bindings.Remove(bindingName);

    Copy link
    Contributor

    qodo-merge-pro bot commented Feb 3, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Add parameter validation

    Add null check for bindingName parameter in AddScriptCallbackBinding to prevent
    potential NullReferenceException when adding bindings.

    dotnet/src/webdriver/JavaScriptEngine.cs [272-277]

     public async Task AddScriptCallbackBinding(string bindingName)
     {
    +    if (bindingName == null)
    +    {
    +        throw new ArgumentNullException(nameof(bindingName));
    +    }
         if (!this.bindings.Add(bindingName))
         {
             throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "A binding named {0} has already been added", bindingName));
         }
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Adding null validation for the bindingName parameter is a good defensive programming practice that prevents potential NullReferenceException and provides clearer error messages. The suggestion correctly identifies the location and provides a proper implementation.

    7

    @RenderMichael
    Copy link
    Contributor Author

    Failures are not related to changes in this PR:

    //dotnet/test/common:BiDi/Network/NetworkEventsTest-firefox
    //java/test/org/openqa/selenium/mobile:NetworkConnectionTest
    //java/test/org/openqa/selenium/remote:RemoteWebDriverScreenshotTest
    //java/test/org/openqa/selenium/remote:RemoteWebDriverScreenshotTest-firefox-beta

    @RenderMichael RenderMichael merged commit 421680f into SeleniumHQ:trunk Feb 3, 2025
    9 of 10 checks passed
    @RenderMichael RenderMichael deleted the script-callbacks branch February 3, 2025 19:18
    sandeepsuryaprasad pushed a commit to sandeepsuryaprasad/selenium that referenced this pull request Mar 23, 2025
    … new bindings (SeleniumHQ#15221)
    
    * [dotnet] Fix `JavaScriptEngine.ScriptCallbackBindings` not containing new bindings
    
    * Add tests
    
    * Implement IEquatable on `InitializationScript`
    
    * Remove implementation of IEquatable, override normal `Equals`
    
    * Override InitializationScript.GetHashCode
    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