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

[java][bidi]: implement bidi setCacheBehavior #15130

Merged
merged 5 commits into from
Jan 23, 2025

Conversation

navin772
Copy link
Contributor

@navin772 navin772 commented Jan 22, 2025

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

This PR adds the bidi method setCacheBehavior defined in the bidi specs - https://w3c.github.io/webdriver-bidi/#command-network-setCacheBehavior.

I have verified that this works with the latest stable Firefox 134 and Chrome 132.

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

  • Implemented the BiDi setCacheBehavior method for Java bindings.

  • Added CacheBehavior enum and SetCacheBehaviorParameters class.

  • Developed unit tests for setCacheBehavior functionality.

  • Enhanced network module to support cache behavior configuration.


Changes walkthrough 📝

Relevant files
Enhancement
Network.java
Add `setCacheBehavior` method to Network module                   

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

  • Added setCacheBehavior method to the Network class.
  • Integrated SetCacheBehaviorParameters for cache behavior
    configuration.
  • +5/-0     
    CacheBehavior.java
    Introduce `CacheBehavior` enum for cache settings               

    java/src/org/openqa/selenium/bidi/network/CacheBehavior.java

  • Created CacheBehavior enum to define cache behavior options.
  • Added DEFAULT and BYPASS cache behavior types.
  • +34/-0   
    SetCacheBehaviorParameters.java
    Add `SetCacheBehaviorParameters` for cache configuration 

    java/src/org/openqa/selenium/bidi/network/SetCacheBehaviorParameters.java

  • Added SetCacheBehaviorParameters class for cache behavior
    configuration.
  • Supported optional context IDs for targeted cache behavior.
  • Implemented toMap method for parameter serialization.
  • +46/-0   
    Tests
    NetworkCommandsTest.java
    Add tests for `setCacheBehavior` in Network module             

    java/test/org/openqa/selenium/bidi/network/NetworkCommandsTest.java

  • Added tests for setCacheBehavior functionality.
  • Tested BYPASS and DEFAULT cache behaviors with and without context
    IDs.
  • Verified exception handling for invalid context IDs.
  • +61/-0   

    Need help?
  • Type /help how to ... in the comments thread for any question about Qodo Merge usage.
  • Check out the documentation for more information.
  • Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Potential Bug

    The toMap() method creates a new Map instance for each condition, which could lead to inconsistent behavior. Consider using a mutable map builder pattern for better maintainability.

    public Map<String, Object> toMap() {
      Map<String, Object> map = Map.of("cacheBehavior", cacheBehavior.toString());
    
      if (contexts != null && !contexts.isEmpty()) {
        return Map.of("cacheBehavior", cacheBehavior.toString(), "contexts", contexts);
      }
    
      return map;
    }

    Copy link
    Contributor

    qodo-merge-pro bot commented Jan 22, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    General
    ✅ Optimize map creation efficiency
    Suggestion Impact:The commit directly implements the suggestion by replacing Map.of() calls with a single mutable HashMap and using put() operations

    code diff:

    -    Map<String, Object> map = Map.of("cacheBehavior", cacheBehavior.toString());
    +    Map<String, Object> map = new HashMap<>();
    +    map.put("cacheBehavior", cacheBehavior.toString());
     
         if (contexts != null && !contexts.isEmpty()) {
    -      return Map.of("cacheBehavior", cacheBehavior.toString(), "contexts", contexts);
    +      map.put("contexts", contexts);
         }

    The toMap() method creates new Map instances unnecessarily. Optimize by using a
    single mutable map and conditionally adding the contexts.

    java/src/org/openqa/selenium/bidi/network/SetCacheBehaviorParameters.java [37-45]

     public Map<String, Object> toMap() {
    -  Map<String, Object> map = Map.of("cacheBehavior", cacheBehavior.toString());
    -
    +  Map<String, Object> map = new HashMap<>();
    +  map.put("cacheBehavior", cacheBehavior.toString());
    +  
       if (contexts != null && !contexts.isEmpty()) {
    -    return Map.of("cacheBehavior", cacheBehavior.toString(), "contexts", contexts);
    +    map.put("contexts", contexts);
       }
    -
    +  
       return map;
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 6

    Why: The suggestion improves code efficiency by replacing immutable Map.of() calls with a single mutable HashMap, reducing object creation overhead and making the code more maintainable. While valid, this is a minor optimization with moderate impact.

    6

    @navin772 navin772 requested a review from pujagani January 22, 2025 10:54
    Copy link
    Contributor

    @pujagani pujagani left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Thank you! @navin772 LGTM

    @navin772
    Copy link
    Contributor Author

    The CI failures are not relevant to the changes done in this PR.

    @pujagani pujagani merged commit 535f96f into SeleniumHQ:trunk Jan 23, 2025
    32 of 34 checks passed
    @navin772 navin772 deleted the java-bidi-setCacheBehavior branch January 23, 2025 05:56
    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.

    3 participants