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

[js] Ensure parity in the locators used by methods #13902

Merged
merged 2 commits into from May 8, 2024

Conversation

pujagani
Copy link
Contributor

@pujagani pujagani commented May 2, 2024

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

Replaced locators and added error handling in this PR.

Motivation and Context

Ensure all select class methods have parity amongst all languages implementations.

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.

Type

Bug fix, Enhancement


Description

  • Introduced error handling in the constructor to ensure the element is not null and is a valid <select> element.
  • Replaced CSS selectors with more robust XPath queries in selectByValue and selectByText methods to improve reliability.
  • Simplified the text normalization and selection process in the selectByText method, enhancing readability and performance.
  • Added error handling to throw an exception if no options match the provided value in selectByValue, enhancing robustness.

Changes walkthrough

Relevant files
Enhancement
select.js
Enhance Select Class with Error Handling and XPath Selectors

javascript/node/selenium-webdriver/lib/select.js

  • Added error handling to throw an exception if the select element or
    option is null.
  • Replaced CSS selectors with XPath for finding options by value and
    text.
  • Simplified and enhanced the text normalization process in selectByText
    method.
  • Added error handling to throw an error if no options are found with
    the specified value.
  • +13/-29 

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

    Copy link

    PR Description updated to latest commit (bf4a4de)

    Copy link

    PR Review

    ⏱️ Estimated effort to review [1-5]

    3, because the PR involves changes in error handling and selector strategies which are crucial for functionality but are not overly complex. Reviewing these changes requires a good understanding of the existing implementation and the impact of these changes on it.

    🧪 Relevant tests

    No

    🔍 Possible issues

    Possible Bug: The use of escapeQuotes function is assumed but not defined or imported in the provided diff. This might lead to runtime errors if not properly handled.

    Error Handling: The error message "Cannot locate option with value: ${value}" uses template literals, but it's not clear if the surrounding function supports template string interpolation as expected in JavaScript.

    🔒 Security concerns

    No


    ✨ Review tool usage guide:

    Overview:
    The review tool scans the PR code changes, and generates a PR review which includes several types of feedbacks, such as possible PR issues, security threats and relevant test in the PR. More feedbacks can be added by configuring the tool.

    The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on any PR.

    • When commenting, to edit configurations related to the review tool (pr_reviewer section), use the following template:
    /review --pr_reviewer.some_config1=... --pr_reviewer.some_config2=...
    
    [pr_reviewer]
    some_config1=...
    some_config2=...
    

    See the review usage page for a comprehensive guide on using this tool.

    Copy link

    PR Code Suggestions

    CategorySuggestions                                                                                                                                                       
    Best practice
    Improve the safety and readability of XPath string construction.

    The string concatenation in the XPath expression might lead to incorrect queries or
    potential injection issues if not properly escaped. Use template literals for better
    readability and safety.

    javascript/node/selenium-webdriver/lib/select.js [227]

    -let options = await this.element.findElements(By.xpath('.//option[@value = ' + escapeQuotes(value) + ']'))
    +let options = await this.element.findElements(By.xpath(`.//option[@value = ${escapeQuotes(value)}]`))
     
    Simplify the check for empty options array to enhance readability.

    Instead of manually throwing an error for empty options, leverage JavaScript's built-in
    functionality or simplify the check to enhance code readability and performance.

    javascript/node/selenium-webdriver/lib/select.js [438-439]

    -if (options.length === 0) {
    +if (!options.length) {
       throw new Error(`Cannot locate option with value: ${value}`)
     }
     
    Enhancement
    Clarify the error message when a null element is provided to the constructor.

    To ensure the error message is consistent and clear, consider rephrasing it to specify
    that the element is expected to be a WebElement representing a tag. javascript/node/selenium-webdriver/lib/select.js [150] -throw new Error(`Element must not be null. Please provide a valid <select> element.`) +throw new Error(`Invalid element: The provided element is null. Please provide a WebElement representing a <select> tag.`)

    Ensure the element provided to the constructor is a valid WebElement.

    Add a check to ensure that the element provided to the constructor is indeed a WebElement,
    enhancing robustness and error handling of the constructor.

    javascript/node/selenium-webdriver/lib/select.js [153]

    +if (!(element instanceof WebElement)) {
    +  throw new TypeError('The provided element is not a valid WebElement.');
    +}
     this.element = element
     
    Security
    Use template literals for consistent and safe embedding of user input in XPath queries.

    Ensure the use of escapeQuotes function is consistent across all instances where user
    input is embedded in XPath queries to prevent injection attacks.

    javascript/node/selenium-webdriver/lib/select.js [379]

     const optionElement = await this.element.findElement(
    -  By.xpath('.//option[normalize-space(.) = ' + escapeQuotes(text) + ']'),
    +  By.xpath(`.//option[normalize-space(.) = ${escapeQuotes(text)}]`)
     )
     

    ✨ Improve tool usage guide:

    Overview:
    The improve tool scans the PR code changes, and automatically generates suggestions for improving the PR code. The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on a PR.

    • When commenting, to edit configurations related to the improve tool (pr_code_suggestions section), use the following template:
    /improve --pr_code_suggestions.some_config1=... --pr_code_suggestions.some_config2=...
    
    [pr_code_suggestions]
    some_config1=...
    some_config2=...
    

    See the improve usage page for a comprehensive guide on using this tool.

    @diemol diemol added this to the 4.21 milestone May 7, 2024
    @pujagani pujagani merged commit 67ba005 into SeleniumHQ:trunk May 8, 2024
    14 of 15 checks passed
    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.

    None yet

    2 participants