You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The core logic did not take time, but trying to avoid the RemoteWebDriver casting took a lot of time to figure out but after spending sufficient time could not find a way to avoid circular dependency issues. Hence this PR took time. We could restructure it in a later release like Selenium 6, when everything uses BiDi. The current goal is to focus on getting the high-level APIs implemented.
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)
Possible Bug:
The implementation of RemoteScript assumes that the WebDriver instance passed to it will always implement HasBiDi. This might not always be the case, which could lead to a ClassCastException. Consider adding a check to ensure that the driver actually implements this interface before casting.
Design Concern:
The RemoteScript class is tightly coupled with the LogInspector and BiDi implementations. This could make unit testing difficult and the code less modular. Consider abstracting these dependencies or using dependency injection for better testability and modularity.
Add type checking before casting to avoid ClassCastException
Consider checking if the driver instance is of type HasBiDi before casting it. This will prevent a potential ClassCastException if the driver does not implement the HasBiDi interface.
-this.biDi = ((HasBiDi) driver).getBiDi();+if (driver instanceof HasBiDi) {+ this.biDi = ((HasBiDi) driver).getBiDi();+} else {+ throw new IllegalArgumentException("Driver must implement HasBiDi");+}
Apply this suggestion
Suggestion importance[1-10]: 10
Why: This suggestion addresses a potential ClassCastException by adding a type check before casting, which is crucial for preventing runtime errors and ensuring the code's robustness.
10
Add null checks for driver to prevent NullPointerException
Consider adding null checks for driver in the constructor to prevent NullPointerException in subsequent method calls if driver is null.
public RemoteScript(WebDriver driver) {
+ if (driver == null) {+ throw new IllegalArgumentException("Driver cannot be null");+ }
this.biDi = ((HasBiDi) driver).getBiDi();
this.logInspector = new LogInspector(driver);
}
Apply this suggestion
Suggestion importance[1-10]: 10
Why: This suggestion prevents a potential NullPointerException by adding null checks for the driver parameter, which is essential for ensuring the stability and reliability of the code.
10
Thread safety
Add synchronization to ensure thread-safe lazy initialization of remoteScript
To ensure thread safety when accessing remoteScript, consider synchronizing the block of code where remoteScript is checked and instantiated. This prevents potential issues in a multi-threaded environment where remoteScript could be instantiated multiple times.
-if (this.remoteScript == null) {- this.remoteScript = new RemoteScript(this);+synchronized (this) {+ if (this.remoteScript == null) {+ this.remoteScript = new RemoteScript(this);+ }
}
return this.remoteScript;
Apply this suggestion
Suggestion importance[1-10]: 9
Why: This suggestion improves thread safety by synchronizing the block of code where remoteScript is checked and instantiated, preventing potential issues in a multi-threaded environment.
9
Possible issue
Ensure unique IDs for handlers to prevent incorrect listener removal
It is recommended to handle the scenario where logInspector.onConsoleEntry(consumer) and logInspector.onJavaScriptException(consumer) might return the same id for different handlers, potentially leading to incorrect removal of listeners.
Why: This suggestion addresses a potential issue where duplicate IDs could lead to incorrect listener removal. Ensuring unique IDs is important for the correct functioning of the handlers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 #14135.
Motivation and Context
Implement high-level BiDi logging API.
The core logic did not take time, but trying to avoid the RemoteWebDriver casting took a lot of time to figure out but after spending sufficient time could not find a way to avoid circular dependency issues. Hence this PR took time. We could restructure it in a later release like Selenium 6, when everything uses BiDi. The current goal is to focus on getting the high-level APIs implemented.
Types of changes
Checklist
PR Type
Enhancement, Tests
Description
RemoteScriptclass to implement high-level BiDi logging API.RemoteScriptwithRemoteWebDriverto handle console messages and JavaScript errors.Scriptinterface defining methods for logging handlers.RemoteScriptfunctionality, including handler addition, removal, and multiple handler scenarios.Changes walkthrough 📝
RemoteScript.java
Add `RemoteScript` class for BiDi logging API integrationjava/src/org/openqa/selenium/remote/RemoteScript.java
RemoteScriptclass implementingScriptinterface.errors.
handlers.
RemoteWebDriver.java
Integrate `RemoteScript` with `RemoteWebDriver`java/src/org/openqa/selenium/remote/RemoteWebDriver.java
remoteScriptfield toRemoteWebDriver.script()method to initialize and returnRemoteScript.Script.java
Define `Script` interface for logging handlersjava/src/org/openqa/selenium/remote/Script.java
Scriptinterface with methods for handling console messagesand JavaScript errors.
WebScriptTest.java
Add tests for `RemoteScript` logging functionalityjava/test/org/openqa/selenium/WebScriptTest.java
RemoteScriptfunctionality.handlers.
BUILD.bazel
Update Bazel build file for BiDi logging dependenciesjava/src/org/openqa/selenium/remote/BUILD.bazel