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

[bidi][java] Add dom mutation handler support #14304

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

  • Added support for handling DOM mutation events in the Java BiDi implementation.
  • Introduced DomMutation class to encapsulate DOM mutation details.
  • Implemented methods to add and remove DOM mutation handlers in RemoteScript.
  • Updated Script interface to include DOM mutation handler methods.
  • Added tests to verify the functionality of DOM mutation handlers.
  • Updated Bazel build files to include necessary resources and dependencies for DOM mutation handling.

Changes walkthrough 📝

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

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

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

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

  • Added new class DomMutation to represent DOM mutation events.
  • Included methods to get element, attribute name, current value, and
    old value.
  • +52/-0   
    RemoteScript.java
    Implement DOM mutation handler in `RemoteScript`                 

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

  • Added addDomMutationHandler method to handle DOM mutation events.
  • Added removeDomMutationHandler method to remove DOM mutation handlers.
  • Loaded and utilized a helper script for DOM mutation handling.
  • +66/-0   
    Script.java
    Extend `Script` interface with DOM mutation handler methods

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

    • Added methods to add and remove DOM mutation handlers.
    +4/-0     
    Tests
    WebScriptTest.java
    Add tests for DOM mutation handlers                                           

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

  • Added tests for adding and removing DOM mutation handlers.
  • Verified DOM mutation events are correctly handled.
  • +62/-3   
    Configuration changes
    BUILD.bazel
    Update Bazel build for DOM mutation support                           

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

  • Added bidi-mutation-listener script to resources.
  • Included dependencies for bidi-mutation-listener.
  • +8/-0     
    BUILD.bazel
    Export `bidi-mutation-listener.js` in Bazel build               

    javascript/bidi-support/BUILD.bazel

    • Added bidi-mutation-listener.js to exports.
    +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 implementation of addDomMutationHandler in RemoteScript.java lacks proper error handling for potential exceptions that might occur during the execution of the script or while fetching elements. Consider adding more robust error handling and logging mechanisms to ensure the stability and maintainability of the code.

    Synchronization Concern
    The use of synchronized block in addDomMutationHandler method might lead to performance issues if the block is accessed by multiple threads simultaneously. Evaluate if this synchronization is necessary or if there could be a more efficient way to handle concurrency.

    Copy link
    Contributor

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Possible bug
    Handle potential NoSuchElementException for better error handling

    Consider handling the potential NoSuchElementException when retrieving elements by
    CSS selector. This exception could be thrown if no elements match the given
    selector, which would cause the current implementation to fail. To handle this, you
    could check if the list elements is not only non-empty but also non-null.

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

     elements = this.driver.findElements(By.cssSelector(String.format("*[data-__webdriver_id='%s']", id)));
    +if (elements != null && !elements.isEmpty()) {
    +    DomMutation event = new DomMutation(elements.get(0), String.valueOf(values.get("name")), String.valueOf(values.get("value")), String.valueOf(values.get("oldValue")));
    +    consumer.accept(event);
    +}
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: This suggestion improves error handling by ensuring that the code does not fail if no elements match the given CSS selector. It addresses a potential bug and enhances the robustness of the code.

    8
    Enhancement
    Improve error handling for file reading operations

    Consider adding error handling for the IOException that could occur when reading the
    script file. Instead of throwing a generic IllegalStateException, it might be
    beneficial to log the error or retry reading the file.

    java/src/org/openqa/selenium/remote/RemoteScript.java [80-89]

     try (InputStream stream = RemoteScript.class.getResourceAsStream("/org/openqa/selenium/remote/bidi-mutation-listener.js")) {
         if (stream == null) {
             throw new IllegalStateException("Unable to find helper script");
         }
         scriptValue = new String(stream.readAllBytes(), UTF_8);
     } catch (IOException e) {
    -    throw new IllegalStateException("Unable to read helper script");
    +    // Log the error or handle it accordingly
    +    logger.error("Error reading helper script", e);
    +    throw new RuntimeException("Error processing the helper script", e);
     }
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: This suggestion enhances error handling by logging the IOException and providing more specific error information. It improves maintainability and debugging but is not critical for functionality.

    7
    Performance
    Improve thread handling by removing unnecessary synchronization

    Replace the use of synchronized block with a more concurrent-friendly approach such
    as using ConcurrentHashMap or similar thread-safe collections. This change would
    improve the performance by reducing the blocking time of threads, especially in a
    multi-threaded environment.

    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)));
    -}
    +elements = this.driver.findElements(By.cssSelector(String.format("*[data-__webdriver_id='%s']", id)));
     
    • Apply this suggestion
    Suggestion importance[1-10]: 5

    Why: While removing the synchronized block could improve performance, it might introduce concurrency issues. The suggestion is contextually correct but should be carefully evaluated for thread safety.

    5
    Best practice
    Ensure resources are closed properly to avoid leaks

    To avoid potential resource leaks, ensure that the InputStream is closed properly.
    Although the try-with-resources statement is used, explicitly checking and closing
    the stream in a finally block would be more robust, especially if modifications are
    made to the try block in the future.

    java/src/org/openqa/selenium/remote/RemoteScript.java [80-89]

    -try (InputStream stream = RemoteScript.class.getResourceAsStream("/org/openqa/selenium/remote/bidi-mutation-listener.js")) {
    +InputStream stream = null;
    +try {
    +    stream = RemoteScript.class.getResourceAsStream("/org/openqa/selenium/remote/bidi-mutation-listener.js");
         if (stream == null) {
             throw new IllegalStateException("Unable to find helper script");
         }
         scriptValue = new String(stream.readAllBytes(), UTF_8);
     } catch (IOException e) {
         throw new IllegalStateException("Unable to read helper script");
    +} finally {
    +    if (stream != null) {
    +        stream.close();
    +    }
     }
     
    • Apply this suggestion
    Suggestion importance[1-10]: 3

    Why: The current try-with-resources statement already ensures that the InputStream is closed properly. The suggestion is redundant and does not provide significant improvement.

    3

    @pujagani pujagani merged commit 48ebf7d into SeleniumHQ:trunk Jul 25, 2024
    28 of 30 checks passed
    sandeepsuryaprasad pushed a commit to sandeepsuryaprasad/selenium that referenced this pull request Oct 29, 2024
    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