SeleniumHQ / selenium

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

Refactored JSON parsing logic. #14164

Open manuelsblanco opened 1 week ago

manuelsblanco commented 1 week ago

User description

Refactored JSON parsing logic into a reusable method parseRemoteValues() to consolidate and eliminate code duplication across locateNodes() and related methods in the BrowsingContext class.

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 In this change, I've introduced the parseRemoteValues function to streamline JSON parsing within the BrowsingContext class. This function efficiently reads JSON input, extracts the "nodes" entry, converts it to JSON, and deserializes it into a list of RemoteValue objects using Gson's TypeToken. By encapsulating this logic, we reduce redundancy and enhance maintainability across JSON parsing operations in BrowsingContext.

Motivation and Context The implementation of parseRemoteValues addresses the need to centralize JSON parsing logic in BrowsingContext. Previously, JSON parsing was dispersed, leading to code duplication and reduced readability. By consolidating this logic into a reusable method, we improve code clarity, promote consistency, and simplify future modifications.

Types of changes

Checklist


PR Type

Enhancement


Description


Changes walkthrough 📝

Relevant files
Enhancement
BrowsingContext.java
Refactor JSON parsing logic in BrowsingContext class         

java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java
  • Introduced parseRemoteValues method for JSON parsing.
  • Refactored locateNodes, locateNode, and locateElement methods to use
    parseRemoteValues.
  • Reduced code duplication by centralizing JSON parsing logic.
  • +26/-42 

    💡 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 1 week ago

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review [1-5] 2
    🧪 Relevant tests No
    🔒 Security concerns No
    ⚡ Key issues to review Error Handling:
    The new method parseRemoteValues lacks explicit error handling for JSON parsing and type conversion. Consider adding error handling to manage cases where the JSON structure might not meet expectations or when type conversion fails.
    Resource Management:
    Ensure that resources like StringReader and JsonInput are properly closed or managed to avoid resource leaks, especially in error scenarios.
    codiumai-pr-agent-pro[bot] commented 1 week ago

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Possible bug
    Add a null check for result.get("nodes") to prevent potential NullPointerException ___ **Consider adding a null check for result.get("nodes") before passing it to
    JSON.toJson(result.get("nodes")) to avoid potential NullPointerException.** [java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java [372-376]](https://github.com/SeleniumHQ/selenium/pull/14164/files#diff-14648f4359d170cc8f4c7f9f7c573da0cbf220e7340cd8391d6081284b982ea1R372-R376) ```diff Map result = jsonInput.read(Map.class); -try (StringReader reader = new StringReader(JSON.toJson(result.get("nodes"))); +Object nodes = result.get("nodes"); +if (nodes == null) { + return Collections.emptyList(); +} +try (StringReader reader = new StringReader(JSON.toJson(nodes)); JsonInput input = JSON.newInput(reader)) { return input.read(new TypeToken>() {}.getType()); } ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 8 Why: The suggestion correctly identifies a potential bug where `result.get("nodes")` could be null, leading to a `NullPointerException`. Adding a null check enhances robustness and prevents runtime exceptions.
    8
    Return null if remoteValues is empty to avoid potential IndexOutOfBoundsException ___ **Consider using Collections.emptyList() instead of returning null to avoid potential
    NullPointerException when the list is used.** [java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java [405]](https://github.com/SeleniumHQ/selenium/pull/14164/files#diff-14648f4359d170cc8f4c7f9f7c573da0cbf220e7340cd8391d6081284b982ea1R405-R405) ```diff -return remoteValues.get(0); +return remoteValues.isEmpty() ? null : remoteValues.get(0); ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 7 Why: The suggestion correctly addresses the risk of `IndexOutOfBoundsException` by checking if `remoteValues` is empty before accessing its first element. This is a valid and important check to prevent runtime errors.
    7
    Add a null check for remoteValues to prevent potential NullPointerException ___ **Consider adding a null check for remoteValues before calling remoteValues.get(0) to avoid
    potential NullPointerException.** [java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java [405]](https://github.com/SeleniumHQ/selenium/pull/14164/files#diff-14648f4359d170cc8f4c7f9f7c573da0cbf220e7340cd8391d6081284b982ea1R405-R405) ```diff +if (remoteValues == null || remoteValues.isEmpty()) { + return null; +} return remoteValues.get(0); ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 7 Why: This suggestion is similar to suggestion 2 but focuses on `NullPointerException`. It's a valid safety check ensuring that the code does not attempt to access elements of a null or empty list, thus preventing runtime exceptions.
    7
    Maintainability
    Extract common logic for sending commands and parsing remote values into a separate method to reduce code duplication ___ **Consider extracting the common logic of sending commands and parsing remote values into a
    separate private method to reduce code duplication.** [java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java [383-386]](https://github.com/SeleniumHQ/selenium/pull/14164/files#diff-14648f4359d170cc8f4c7f9f7c573da0cbf220e7340cd8391d6081284b982ea1R383-R386) ```diff -new Command<>( - "browsingContext.locateNodes", - params, - this::parseRemoteValues)); +private List sendLocateNodesCommand(Map params) { + return this.bidi.send(new Command<>("browsingContext.locateNodes", params, this::parseRemoteValues)); +} +// Usage +return sendLocateNodesCommand(params); + ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 6 Why: The suggestion to extract repeated command sending logic into a separate method is a good practice for maintainability and reducing code duplication. However, the impact on the overall system is moderate compared to the other suggestions that prevent runtime errors.
    6