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

[dotnet] Handle nullability on WebElement #15225

Merged
merged 1 commit into from
Feb 3, 2025

Conversation

RenderMichael
Copy link
Contributor

@RenderMichael RenderMichael commented Feb 3, 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.

Annotate nullability on helper interfaces on WebElement. Refactor WebElement to minimize diffs when nullability is fully annotated on the type.

Motivation and Context

Contributes to #14640

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, Bug fix


Description

  • Enabled nullability annotations for WebElement and related interfaces.

  • Refactored WebElement class to improve null safety and readability.

  • Simplified and optimized code in WebElementFactory.

  • Replaced redundant methods and improved exception handling.


Changes walkthrough 📝

Relevant files
Enhancement
ILocatable.cs
Add nullability annotations to `ILocatable`                           

dotnet/src/webdriver/ILocatable.cs

  • Enabled nullability annotations.
  • No functional changes, only metadata updates.
  • +2/-0     
    ITakesScreenshot.cs
    Add nullability annotations to `ITakesScreenshot`               

    dotnet/src/webdriver/ITakesScreenshot.cs

  • Enabled nullability annotations.
  • No functional changes, only metadata updates.
  • +2/-0     
    WebElement.cs
    Refactor `WebElement` and enable nullability                         

    dotnet/src/webdriver/WebElement.cs

  • Enabled nullability annotations.
  • Refactored WebElement to use Id property instead of elementId field.
  • Simplified methods by removing redundant checks and improving
    readability.
  • Improved exception handling and code consistency.
  • +71/-100
    WebElementFactory.cs
    Refactor `WebElementFactory` and enable nullability           

    dotnet/src/webdriver/WebElementFactory.cs

  • Enabled nullability annotations.
  • Simplified GetElementId method and removed redundant
    TryGetElementPropertyName.
  • Improved exception handling for null or invalid inputs.
  • +22/-30 

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

    qodo-merge-pro bot commented Feb 3, 2025

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

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

    Null Safety

    The GetDomAttribute and GetDomProperty methods return ToString() on potentially null values without proper null checks, which could cause NullReferenceException.

    return commandResponse.Value?.ToString();
    Error Handling

    The ContainsElementReference method throws ArgumentNullException but returns false would be more appropriate for a boolean check method.

        if (elementDictionary == null)
        {
            throw new ArgumentNullException(nameof(elementDictionary), "The dictionary containing the element reference cannot be null");
        }
    
        return elementDictionary.ContainsKey(WebElement.ElementReferencePropertyName);
    }

    Copy link
    Contributor

    qodo-merge-pro bot commented Feb 3, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Prevent null reference exception

    Add null check for commandResponse.Value before calling ToString() to prevent
    potential NullReferenceException.

    dotnet/src/webdriver/WebElement.cs [78-79]

     Response commandResponse = this.Execute(DriverCommand.GetElementTagName, parameters);
    -return commandResponse.Value.ToString();
    +return commandResponse.Value?.ToString() ?? string.Empty;
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: The suggestion adds important null checking to prevent NullReferenceException when accessing commandResponse.Value, which could be null. This is a critical defensive programming practice.

    8
    Handle invalid response format

    Add null check for scriptResponse before casting to Dictionary to prevent
    potential InvalidCastException.

    dotnet/src/webdriver/WebElement.cs [210-211]

     object scriptResponse = this.driver.ExecuteScript("var rect = arguments[0].getBoundingClientRect(); return {'x': rect.left, 'y': rect.top};", this);
    -Dictionary<string, object> rawLocation = (Dictionary<string, object>)scriptResponse;
    +if (scriptResponse is not Dictionary<string, object> rawLocation)
    +{
    +    throw new WebDriverException("Failed to get element location - invalid response format");
    +}
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: The suggestion improves error handling by properly checking the response type and providing a clear error message when the format is invalid, preventing potential runtime exceptions.

    8

    @RenderMichael
    Copy link
    Contributor Author

    Failures are not related to changes in this PR:

    //java/test/org/openqa/selenium/mobile:NetworkConnectionTest
    //java/test/org/openqa/selenium/remote:RemoteWebDriverScreenshotTest
    //java/test/org/openqa/selenium/remote:RemoteWebDriverScreenshotTest-firefox-beta

    @RenderMichael RenderMichael merged commit 480d3c7 into SeleniumHQ:trunk Feb 3, 2025
    9 of 10 checks passed
    @RenderMichael RenderMichael deleted the webelement branch February 3, 2025 19:19
    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.

    2 participants