SeleniumHQ / selenium

A browser automation framework and ecosystem.
https://selenium.dev
Apache License 2.0
30.42k stars 8.15k forks source link

[bidi][java] Add authentication handlers #14334

Closed pujagani closed 1 month ago

pujagani commented 2 months ago

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 #13993

Motivation and Context

Highi-level APIs for W3C BiDi for authentication

Types of changes

Checklist


PR Type

Enhancement, Tests


Description


Changes walkthrough 📝

Relevant files
Enhancement
Network.java
Update `onAuthRequired` method to return listener ID         

java/src/org/openqa/selenium/bidi/module/Network.java
  • Modified onAuthRequired method to return a long value.
  • Updated method to handle authentication listener with return type.
  • +3/-3     
    Network.java
    Introduce `Network` interface for authentication handlers

    java/src/org/openqa/selenium/remote/Network.java
  • Added new interface Network with methods for authentication handlers.
  • +31/-0   
    RemoteNetwork.java
    Implement `Network` interface in `RemoteNetwork` class     

    java/src/org/openqa/selenium/remote/RemoteNetwork.java
  • Implemented Network interface in RemoteNetwork class.
  • Added methods to manage authentication handlers.
  • +79/-0   
    RemoteWebDriver.java
    Add network management to `RemoteWebDriver`                           

    java/src/org/openqa/selenium/remote/RemoteWebDriver.java
  • Added remoteNetwork field and network method to RemoteWebDriver.
  • +9/-0     
    Tests
    WebNetworkTest.java
    Add tests for network authentication handlers                       

    java/test/org/openqa/selenium/WebNetworkTest.java
  • Added tests for authentication handler methods in WebNetworkTest.
  • +91/-0   
    Configuration changes
    BUILD.bazel
    Update Bazel build file for network package                           

    java/src/org/openqa/selenium/remote/BUILD.bazel - Updated Bazel build file to include new network package.
    +1/-0     

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

    codiumai-pr-agent-pro[bot] commented 2 months ago

    PR Reviewer Guide 🔍

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

    Return Type Change
    The method `onAuthRequired` has changed its return type from `void` to `long`. Ensure that this change is documented and that all calling code is updated to handle the new return type correctly. Error Handling
    The method `removeAuthenticationHandler` and `clearAuthenticationHandlers` lack error handling for cases where the `id` or `intercept` might not be valid or already removed. Consider adding error handling to prevent runtime exceptions.
    codiumai-pr-agent-pro[bot] commented 2 months ago

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Possible bug
    Validate non-null parameters in addAuthenticationHandler ___ **Consider checking for null or invalid inputs in addAuthenticationHandler method to
    prevent potential issues when the usernameAndPassword is null.** [java/src/org/openqa/selenium/remote/RemoteNetwork.java [44]](https://github.com/SeleniumHQ/selenium/pull/14334/files#diff-d08d67a345515124ab64ce0074d8d9ecadda2b054a3c643042b4ed02deb08be0R44-R44) ```diff public long addAuthenticationHandler(UsernameAndPassword usernameAndPassword) { + if (usernameAndPassword == null) { + throw new IllegalArgumentException("Username and password should not be null."); + } String intercept = network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED)); ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 10 Why: Adding a null check for `usernameAndPassword` in `addAuthenticationHandler` prevents potential null pointer exceptions, ensuring the method handles invalid inputs gracefully.
    10
    Enhancement
    Use ConcurrentHashMap to ensure thread safety ___ **Ensure thread safety for operations on authCallbackIdMap since it might be accessed
    by multiple threads. Consider using ConcurrentHashMap instead of HashMap for
    authCallbackIdMap.** [java/src/org/openqa/selenium/remote/RemoteNetwork.java [36]](https://github.com/SeleniumHQ/selenium/pull/14334/files#diff-d08d67a345515124ab64ce0074d8d9ecadda2b054a3c643042b4ed02deb08be0R36-R36) ```diff -private final Map authCallbackIdMap = new HashMap<>(); +private final Map authCallbackIdMap = new ConcurrentHashMap<>(); ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 9 Why: Ensuring thread safety by using `ConcurrentHashMap` is crucial for preventing concurrency issues, especially if `authCallbackIdMap` is accessed by multiple threads.
    9
    Add logging for removing intercepts and listeners ___ **When removing intercepts and listeners, consider logging these operations to help
    with debugging and maintaining the system.** [java/src/org/openqa/selenium/remote/RemoteNetwork.java [63-64]](https://github.com/SeleniumHQ/selenium/pull/14334/files#diff-d08d67a345515124ab64ce0074d8d9ecadda2b054a3c643042b4ed02deb08be0R63-R64) ```diff +logger.info("Removing intercept with ID: " + intercept); network.removeIntercept(intercept); +logger.info("Removing listener with ID: " + id); this.biDi.removeListener(id); ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 7 Why: Adding logging for removing intercepts and listeners can aid in debugging and maintaining the system, though it is a minor enhancement compared to functional changes.
    7
    Possible issue
    Add exception handling around addListener calls ___ **Consider handling the case where addListener might throw an exception due to invalid
    parameters or other issues. You can wrap the addListener calls in a try-catch block
    to manage such exceptions gracefully.** [java/src/org/openqa/selenium/bidi/module/Network.java [174-176]](https://github.com/SeleniumHQ/selenium/pull/14334/files#diff-e742aac674b88cc9d9b7d49a9adf66b4dd95c479327e712cf08a3f8c02a43e7fR174-R176) ```diff -return this.bidi.addListener(authRequired, consumer); -return this.bidi.addListener(browsingContextIds, authRequired, consumer); +try { + return this.bidi.addListener(authRequired, consumer); +} catch (Exception e) { + // handle exception +} +try { + return this.bidi.addListener(browsingContextIds, authRequired, consumer); +} catch (Exception e) { + // handle exception +} ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 8 Why: Adding exception handling around `addListener` calls is a good practice to manage potential runtime exceptions, improving the robustness of the code.
    8