htmlunit-driver icon indicating copy to clipboard operation
htmlunit-driver copied to clipboard

Use Capabilities API to set options

Open sbabcoc opened this issue 1 year ago • 3 comments

User description

Instead of using the importOptions method to duplicate every option, set the individual options directly via the Capabilities API.


PR Type

Enhancement


Description

  • Replaced the use of WebClientOptions with HtmlUnitDriverOptions for setting up the WebDriver.
  • Set individual options directly via the HtmlUnitOption capabilities API.
  • Removed the importOptions method to streamline option setting.

Changes walkthrough 📝

Relevant files
Enhancement
WebDriverTestCase.java
Use Capabilities API for setting HtmlUnitDriver options   

src/test/java/org/openqa/selenium/htmlunit/WebDriverTestCase.java

  • Replaced WebClientOptions with HtmlUnitDriverOptions.
  • Set individual options directly using HtmlUnitOption capabilities.
  • Removed the use of importOptions method.
  • +8/-10   

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

    sbabcoc avatar Jun 30 '24 18:06 sbabcoc

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review [1-5] 2
    🧪 Relevant tests No
    🔒 Security concerns No
    ⚡ Key issues to review Possible Bug:
    The method getWebClientTimeout() is called twice in the new implementation. Consider storing its result in a variable to avoid multiple calls, which could be inefficient or error-prone if the method's behavior changes.

    qodo-code-review[bot] avatar Jun 30 '24 18:06 qodo-code-review[bot]

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Possible bug
    Verify that HtmlUnitDriverOptions constructor correctly initializes settings

    Ensure that the HtmlUnitDriverOptions constructor is properly handling the
    getBrowserVersion() parameter, especially since the previous implementation used
    importOptions which might have included additional handling or defaults that are now
    missing.

    src/test/java/org/openqa/selenium/htmlunit/WebDriverTestCase.java [545]

    +// Ensure that all necessary settings and defaults are properly initialized in the HtmlUnitDriverOptions constructor
     final HtmlUnitDriverOptions driverOptions = new HtmlUnitDriverOptions(getBrowserVersion());
     
    
    • [ ] Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: Ensuring that the constructor correctly handles the getBrowserVersion() parameter is crucial, as it might affect the behavior of the driver. This suggestion addresses a potential bug.

    9
    Enhancement
    Use direct methods for setting options instead of setCapability

    Replace the use of setCapability with direct method calls for setting options if
    HtmlUnitDriverOptions provides such methods. Direct method calls (like setHistorySizeLimit
    and setTimeout) are usually more readable and less error-prone compared to generic
    setCapability methods.

    src/test/java/org/openqa/selenium/htmlunit/WebDriverTestCase.java [548-552]

    -driverOptions.setCapability(HtmlUnitOption.optHistorySizeLimit, 0);
    -driverOptions.setCapability(HtmlUnitOption.optTimeout, getWebClientTimeout());
    +driverOptions.setHistorySizeLimit(0);
    +driverOptions.setTimeout(getWebClientTimeout());
     
    
    • [ ] Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Using direct methods for setting options improves readability and reduces the chance of errors, making this a valuable enhancement.

    8
    Best practice
    Add exception handling around capability setting to ensure robustness

    Consider handling exceptions or errors that might occur during the setting of
    capabilities. Since capabilities are critical for driver configuration, any failure in
    setting them should be appropriately handled to avoid runtime issues.

    src/test/java/org/openqa/selenium/htmlunit/WebDriverTestCase.java [548-552]

    -driverOptions.setCapability(HtmlUnitOption.optHistorySizeLimit, 0);
    -driverOptions.setCapability(HtmlUnitOption.optTimeout, getWebClientTimeout());
    +try {
    +    driverOptions.setCapability(HtmlUnitOption.optHistorySizeLimit, 0);
    +    driverOptions.setCapability(HtmlUnitOption.optTimeout, getWebClientTimeout());
    +} catch (Exception e) {
    +    // Handle exception, possibly logging or rethrowing with more context
    +}
     
    
    • [ ] Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Adding exception handling around critical configuration steps is a best practice that enhances the robustness and reliability of the code.

    8
    Possible issue
    Verify and correct the import statement for HtmlUnitOption

    It seems that the import statement for HtmlUnitOption might be incorrect or unnecessary.
    The class HtmlUnitDriverOptions is used to set capabilities, and typically such options
    classes provide all necessary methods and constants. Verify if HtmlUnitOption is a
    required import, or if the capabilities can be set directly using HtmlUnitDriverOptions.

    src/test/java/org/openqa/selenium/htmlunit/WebDriverTestCase.java [111]

    -import org.openqa.selenium.htmlunit.options.HtmlUnitOption;
    +// If HtmlUnitOption is not needed, remove the import
    +// Otherwise, ensure that HtmlUnitOption is correctly implemented and used
     
    
    • [ ] Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: The suggestion to verify the necessity of the HtmlUnitOption import is valid, as unnecessary imports can clutter the code. However, it does not address a critical issue.

    7

    qodo-code-review[bot] avatar Jun 30 '24 18:06 qodo-code-review[bot]

    The codiumai-pr-agent-pro suggestions are all entirely off the mark.

    sbabcoc avatar Jun 30 '24 20:06 sbabcoc

    I think you'll notice a pronounced performance improvement by using the Capabilities API instead of copying all of the WebClientOptions properties en masse with the importOptions method.

    sbabcoc avatar Jul 04 '24 23:07 sbabcoc

    Thanks, as always

    rbri avatar Jul 12 '24 15:07 rbri

    @rbri Absolutely! Were those unit tests running faster with these revisions? Also, do you have a timeline for releasing HtmlUnit 4.4.0 with that hidden element fix?

    sbabcoc avatar Jul 13 '24 05:07 sbabcoc

    Were those unit tests running faster with these revisions?

    have applied the fix to HtmlUnit itself but no real performance change - for me this is expected, the performance is lost at many other places.

    rbri avatar Jul 14 '24 13:07 rbri

    Also, do you have a timeline for releasing HtmlUnit 4.4.0 with that hidden element fix?

    not really, usually i make a release every 4 weeks. At the moment i like to wait for the new Edge/Chrome version and i have some strange issues with htmx 2.0 that i like to fix. Made some good progress during the last days but still there are some hard riddles to solve.

    But if it helps, i can do a release maybe at the next weekend....

    rbri avatar Jul 14 '24 13:07 rbri