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

Add script pinning methods #14305

Merged
merged 1 commit into from
Jul 25, 2024

Conversation

pujagani
Copy link
Contributor

@pujagani pujagani commented Jul 25, 2024

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.

Description

Related to #13992

Motivation and Context

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

  • Modified onMessage method in Script class to return a listener ID.
  • Introduced DomMutation class to handle DOM mutation events.
  • Added methods in RemoteScript to handle DOM mutation events and script pinning/unpinning.
  • Extended Script interface to include methods for DOM mutation handling and script pinning.
  • Added tests for DOM mutation handling and script pinning/unpinning in WebScriptTest.
  • Updated Bazel build files to include bidi-mutation-listener.js resource.

Changes walkthrough 📝

Relevant files
Enhancement
Script.java
Modify `onMessage` method to return listener ID                   

java/src/org/openqa/selenium/bidi/module/Script.java

  • Changed onMessage method to return a long value.
+3/-3     
DomMutation.java
Introduce `DomMutation` class for DOM mutation events       

java/src/org/openqa/selenium/remote/DomMutation.java

  • Added new class DomMutation to handle DOM mutation events.
+52/-0   
RemoteScript.java
Add DOM mutation handling and script pinning methods         

java/src/org/openqa/selenium/remote/RemoteScript.java

  • Added methods to handle DOM mutation events.
  • Added methods to pin and unpin scripts.
  • +76/-0   
    Script.java
    Extend `Script` interface for DOM mutation and script pinning

    java/src/org/openqa/selenium/remote/Script.java

  • Added interface methods for DOM mutation handling and script pinning.
  • +8/-0     
    Tests
    WebScriptTest.java
    Add tests for DOM mutation handling and script pinning     

    java/test/org/openqa/selenium/WebScriptTest.java

  • Added tests for DOM mutation handling.
  • Added tests for script pinning and unpinning.
  • +105/-3 
    Configuration changes
    BUILD.bazel
    Update Bazel build for DOM mutation listener resource       

    java/src/org/openqa/selenium/remote/BUILD.bazel

    • Added resource for bidi-mutation-listener.js.
    +8/-0     
    BUILD.bazel
    Configure Bazel for `bidi-mutation-listener.js` visibility

    javascript/bidi-support/BUILD.bazel

    • Added visibility and export for bidi-mutation-listener.js.
    +3/-0     

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    Copy link
    Contributor

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Key issues to review

    Error Handling
    The method addDomMutationHandler in RemoteScript.java uses a try-with-resources to load a script from a resource file and throws an IllegalStateException if the file is not found or cannot be read. It's crucial to ensure that this error handling is robust enough to prevent the application from crashing and provide meaningful error messages to the user.

    Synchronization
    The method addDomMutationHandler uses a synchronized block on this to perform a find operation on driver. This might lead to performance issues if the synchronization is not strictly necessary or if it can be narrowed down to a smaller scope of data.

    Resource Management
    The method addDomMutationHandler reads all bytes from an input stream without checking the file size, which could potentially lead to memory issues if the file is unexpectedly large.

    Copy link
    Contributor

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Possible bug
    Add null safety checks to prevent NullPointerException

    Consider handling the potential NullPointerException that could occur if
    message.getData().getValue() returns an optional that is not present. This can be
    addressed by checking if the optional value is present before calling get().

    java/src/org/openqa/selenium/remote/RemoteScript.java [95]

    -String value = message.getData().getValue().get().toString();
    +Optional<ChannelValue> optionalValue = message.getData().getValue();
    +if (optionalValue.isPresent()) {
    +    String value = optionalValue.get().toString();
    +    // existing code continues here...
    +} else {
    +    // appropriate handling code, e.g., log an error or throw an exception
    +}
     
    Suggestion importance[1-10]: 9

    Why: This suggestion addresses a potential NullPointerException, which is a significant issue. Adding null safety checks improves the robustness of the code.

    9
    Enhancement
    Use more descriptive exceptions for script errors

    Instead of throwing a generic IllegalStateException, throw a more specific exception
    or create a custom exception class that better describes the error scenario when the
    helper script cannot be found or read.

    java/src/org/openqa/selenium/remote/RemoteScript.java [84-88]

    -throw new IllegalStateException("Unable to find helper script");
    -throw new IllegalStateException("Unable to read helper script");
    +throw new ScriptNotFoundException("Helper script not found");
    +throw new ScriptReadException("Failed to read helper script");
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Using more descriptive exceptions improves the clarity and maintainability of the code by providing more specific error information.

    8
    Performance
    Improve the thread-safety and performance of element finding

    Replace the synchronized block with a more concurrent-friendly approach using
    ConcurrentHashMap or similar for better scalability, especially if
    this.driver.findElements is expected to be called frequently.

    java/src/org/openqa/selenium/remote/RemoteScript.java [102-104]

    -synchronized (this) {
    -    elements = this.driver.findElements(By.cssSelector(String.format("*[data-__webdriver_id='%s']", id)));
    -}
    +// Assuming `elements` is a ConcurrentHashMap or similar thread-safe collection
    +elements = this.driver.findElements(By.cssSelector(String.format("*[data-__webdriver_id='%s']", id)));
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: This suggestion improves the performance and scalability of the code by replacing the synchronized block with a more concurrent-friendly approach. However, it assumes that elements is a thread-safe collection, which may require additional changes.

    7
    Maintainability
    Implement better error handling and logging for script loading failures

    Consider using a logging framework instead of throwing an exception for missing or
    unreadable scripts to allow for better error handling and potentially retry
    mechanisms.

    java/src/org/openqa/selenium/remote/RemoteScript.java [84-88]

    -throw new IllegalStateException("Unable to find helper script");
    -throw new IllegalStateException("Unable to read helper script");
    +logger.error("Helper script not found, attempting retry...");
    +// Retry logic or alternative handling
     
    Suggestion importance[1-10]: 6

    Why: This suggestion enhances maintainability by using a logging framework for better error handling. However, it may require additional implementation for retry mechanisms.

    6

    @pujagani pujagani force-pushed the add-dom-mutation-support-java branch from 0a5d01a to d07a5be Compare July 25, 2024 12:24
    @pujagani pujagani merged commit 5dd385c into SeleniumHQ:trunk Jul 25, 2024
    28 of 30 checks passed
    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.

    1 participant