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

[rb] handled nil condition for Cookie Method in ruby #15099

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from

Conversation

pallavigitwork
Copy link
Member

@pallavigitwork pallavigitwork commented Jan 16, 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

implemented handling nil/empty for cookie methods - reference pr- #15044

Motivation and Context

it wasn't implemented in ruby

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.

implemented handling nil/empty for cookie methods - reference pr- #15044


PR Type

Bug fix, Enhancement


Description

  • Added validation for nil or empty cookie names in delete_cookie and cookie methods.

  • Raised Error::ArgumentError for invalid cookie names.

  • Improved error handling for cookie-related methods in Ruby WebDriver.


Changes walkthrough 📝

Relevant files
Bug fix
bridge.rb
Validate cookie name in Ruby WebDriver methods                     

rb/lib/selenium/webdriver/remote/bridge.rb

  • Added checks for nil or empty cookie names in delete_cookie and cookie
    methods.
  • Raised Error::ArgumentError with a descriptive message for invalid
    names.
  • +6/-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

    qodo-merge-pro bot commented Jan 16, 2025

    PR Reviewer Guide 🔍

    (Review updated until commit 10cc4d3)

    Here are some key observations to aid the review process:

    🎫 Ticket compliance analysis 🔶

    15044 - Partially compliant

    Compliant requirements:

    • Handle null/empty cookie name validation
    • Raise appropriate error for invalid cookie names

    Non-compliant requirements:

    • Improve cookie retrieval efficiency by using dedicated endpoint instead of fetching all cookies
    ⏱️ Estimated effort to review: 1 🔵⚪⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Code Style

    Extra parentheses around condition in if statement are unnecessary in Ruby and should be removed for better readability

    if ( name.nil? || name.empty?) then

    Copy link
    Contributor

    qodo-merge-pro bot commented Jan 16, 2025

    PR Code Suggestions ✨

    Latest suggestions up to 10cc4d3
    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Add type safety by ensuring string conversion before checking for emptiness

    Consider using String#to_s before checking empty? to handle non-string inputs
    gracefully, preventing potential NoMethodError.

    rb/lib/selenium/webdriver/remote/bridge.rb [388-390]

    -if ( name.nil? || name.empty?) then
    -  raise Error::ArgumentError, 'Cookie name cannot be null or empty'
    -end
    +raise Error::ArgumentError, 'Cookie name cannot be null or empty' if name.nil? || name.to_s.empty?
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: This is a valuable defensive programming suggestion that prevents potential runtime errors by safely handling non-string inputs, making the code more robust and preventing potential NoMethodError exceptions.

    8
    General
    Simplify conditional statement by following idiomatic Ruby style and using guard clause pattern

    Remove unnecessary parentheses around the condition and redundant 'then' keyword to
    follow Ruby style conventions. Also, consider using a guard clause pattern for
    better readability.

    rb/lib/selenium/webdriver/remote/bridge.rb [388-390]

    -if ( name.nil? || name.empty?) then
    -  raise Error::ArgumentError, 'Cookie name cannot be null or empty'
    -end
    +raise Error::ArgumentError, 'Cookie name cannot be null or empty' if name.nil? || name.empty?
    • Apply this suggestion
    Suggestion importance[1-10]: 5

    Why: The suggestion improves code readability and follows Ruby conventions by removing unnecessary parentheses and 'then' keyword, using a more idiomatic guard clause pattern. While valid, this is primarily a style improvement.

    5

    Previous suggestions

    ✅ Suggestions up to commit 3966bfe
    CategorySuggestion                                                                                                                                    Score
    General
    ✅ Improve error message accuracy to match the actual validation being performed
    Suggestion Impact:The commit implemented the suggested error message change in both delete_cookie and cookie methods, making the error messages more accurate by explicitly mentioning both null and empty conditions

    code diff:

               if ( name.nil? || name.empty?) then
    -            raise Error::ArgumentError, 'Cookie name cannot be null'
    +            raise Error::ArgumentError, 'Cookie name cannot be null or empty'
               end
               execute :delete_cookie, name: name
             end
     
             def cookie(name)
               if ( name.nil? || name.empty?) then
    -            raise Error::ArgumentError, 'Cookie name cannot be null'
    +            raise Error::ArgumentError, 'Cookie name cannot be null or empty'
               end

    Update error message to accurately reflect both nil and empty string conditions
    being checked.

    rb/lib/selenium/webdriver/remote/bridge.rb [389]

    -raise Error::ArgumentError, 'Cookie name cannot be null'
    +raise Error::ArgumentError, 'Cookie name cannot be null or empty'
    Suggestion importance[1-10]: 7

    Why: The suggestion improves the error message accuracy by explicitly mentioning both nil and empty conditions, which helps developers better understand the validation failure reason.

    7
    Improve code readability by following Ruby style conventions for conditional statements

    Remove unnecessary parentheses around the condition and remove redundant 'then'
    keyword to follow Ruby style conventions.

    rb/lib/selenium/webdriver/remote/bridge.rb [388-390]

    -if ( name.nil? || name.empty?) then
    +if name.nil? || name.empty?
       raise Error::ArgumentError, 'Cookie name cannot be null'
     end
    Suggestion importance[1-10]: 5

    Why: The suggestion correctly identifies Ruby style conventions by removing unnecessary parentheses and 'then' keyword, which improves code readability and maintainability.

    5

    @aguspe
    Copy link
    Contributor

    aguspe commented Jan 16, 2025

    Thank you so much for the help! this is already implemented, so I will close the PR, have a great day!

    @aguspe aguspe closed this Jan 16, 2025
    @nvborisenko
    Copy link
    Member

    @aguspe This adds validation of cookie name to be aligned with other bindings, I think the PR is still actual.

    @aguspe
    Copy link
    Contributor

    aguspe commented Jan 16, 2025

    @nvborisenko I was talking with Pallavi, and It feels strange for me to add this type of validation in Ruby with a mandatory parameter.

    But if the goal is to be aligned between all the bindings and return a consistent error across bindings and browsers then I can re-open this and help Pallavi with the testing

    We can update the guard clause instead to make it more Ruby-like

    @aguspe
    Copy link
    Contributor

    aguspe commented Jan 16, 2025

    After discussing with @nvborisenko I'm re-opening this PR and adding a review comment

    @aguspe aguspe reopened this Jan 16, 2025
    @@ -385,10 +385,16 @@ def add_cookie(cookie)
    end

    def delete_cookie(name)
    if ( name.nil? || name.empty?) then
    raise Error::ArgumentError, 'Cookie name cannot be null or empty'
    end
    Copy link
    Contributor

    @aguspe aguspe Jan 16, 2025

    Choose a reason for hiding this comment

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

    Could you use a guard clause here such as:

    raise Error::ArgumentError, 'Cookie name cannot be null or empty' unless name && !name.empty?

    @harsha509 harsha509 changed the title handled nil condition for Cookie Method in ruby [rb] handled nil condition for Cookie Method in ruby Jan 17, 2025
    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