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] Simplify and nullable annotate DriverFinder #15232

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from

Conversation

RenderMichael
Copy link
Contributor

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

Simplify and nullable annotate DriverFinder

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

  • Simplified and nullable-annotated DriverFinder class.

  • Introduced TryGetBrowserPath method for safer browser path retrieval.

  • Replaced dictionary-based paths with SeleniumManagerPaths record.

  • Improved error handling and nullability checks in driver and browser path logic.


Changes walkthrough 📝

Relevant files
Enhancement
ChromiumDriver.cs
Updated browser path handling in ChromiumDriver                   

dotnet/src/webdriver/Chromium/ChromiumDriver.cs

  • Replaced HasBrowserPath with TryGetBrowserPath for safer path
    retrieval.
  • Updated logic to handle nullable browser paths.
  • +2/-2     
    DriverFinder.cs
    Simplified and enhanced `DriverFinder` implementation       

    dotnet/src/webdriver/DriverFinder.cs

  • Added nullability annotations and checks.
  • Replaced dictionary with SeleniumManagerPaths for path storage.
  • Introduced TryGetBrowserPath method for safer path retrieval.
  • Improved error handling for invalid paths.
  • +43/-25 
    FirefoxDriver.cs
    Updated browser path handling in FirefoxDriver                     

    dotnet/src/webdriver/Firefox/FirefoxDriver.cs

  • Replaced HasBrowserPath with TryGetBrowserPath for safer path
    retrieval.
  • Updated logic to handle nullable browser paths.
  • +2/-2     
    SeleniumManager.cs
    Refactored SeleniumManager to use `SeleniumManagerPaths` 

    dotnet/src/webdriver/SeleniumManager.cs

  • Replaced dictionary-based paths with SeleniumManagerPaths record.
  • Improved logging and error handling for path retrieval.
  • Updated method signatures to use SeleniumManagerPaths.
  • +17/-17 

    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 4, 2025

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

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

    Error Handling

    The BinaryPaths method throws NoSuchDriverException for invalid browser path, but this may be too strict since browser path is optional in some cases. Consider making browser path validation conditional.

    if (!File.Exists(binaryPaths.BrowserPath))
    {
        throw new NoSuchDriverException($"The browser path is not a valid file: {binaryPaths.BrowserPath}");
    }

    Copy link
    Contributor

    qodo-merge-pro bot commented Feb 4, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Prevent null reference exception

    Add null check for binaryPaths.BrowserPath before validating file existence to
    prevent NullReferenceException. The browser path might be null in some cases.

    dotnet/src/webdriver/DriverFinder.cs [119-122]

    -if (!File.Exists(binaryPaths.BrowserPath))
    +if (!string.IsNullOrEmpty(binaryPaths.BrowserPath) && !File.Exists(binaryPaths.BrowserPath))
     {
         throw new NoSuchDriverException($"The browser path is not a valid file: {binaryPaths.BrowserPath}");
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: The suggestion addresses a critical null reference safety issue that could cause runtime crashes. The browser path validation should indeed check for null/empty values before attempting to verify file existence.

    8
    Handle null browser path safely

    Add null check for path before accessing it in GetBrowserPath() to prevent
    potential NullReferenceException when accessing BrowserPath property.

    dotnet/src/webdriver/DriverFinder.cs [56-59]

     public string GetBrowserPath()
     {
    -    return BinaryPaths().BrowserPath;
    +    var paths = BinaryPaths();
    +    return paths.BrowserPath ?? string.Empty;
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: The suggestion improves null safety by handling potential null browser paths gracefully, preventing runtime exceptions. This is particularly important as the code already shows signs of null-safety concerns with nullable annotations.

    7

    @nvborisenko
    Copy link
    Member

    Please let me postpone it, DriverFinder is strange for me (in terms of nullability).

    @RenderMichael
    Copy link
    Contributor Author

    @nvborisenko It took me some time to figure out, but I found out the reason: when no browser exists, Selenium Manager will return an empty string as the value.

    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