Skip to content

[dotnet] Finish nullability annotations on Support package #15089

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

Merged
merged 1 commit into from
Jan 15, 2025

Conversation

RenderMichael
Copy link
Contributor

@RenderMichael RenderMichael commented Jan 15, 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

This completes nullability annotations on the Support package, and marks the project as nullable-enabled in the csproj and in bazel.

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

  • Completed nullability annotations for the Support package.

  • Updated Nullable settings to enable in project and Bazel files.

  • Improved event handling and exception safety in EventFiringWebDriver.

  • Refactored code for better null safety and modern C# practices.


Changes walkthrough 📝

Relevant files
Enhancement
EventFiringWebDriver.cs
Refactor `EventFiringWebDriver` for null safety and modern practices

dotnet/src/support/Events/EventFiringWebDriver.cs

  • Replaced driver field with WrappedDriver property.
  • Added nullability annotations to event handlers and method parameters.
  • Refactored methods for null safety and modern C# practices.
  • Improved exception handling and logging for driver operations.
  • +184/-219
    WebDriverExtensions.cs
    Add null safety to `WebDriverExtensions`                                 

    dotnet/src/support/Extensions/WebDriverExtensions.cs

  • Updated method signatures to include nullable annotations.
  • Improved null safety in screenshot handling logic.
  • +2/-2     
    PopupWindowFinder.cs
    Enhance null safety in `PopupWindowFinder`                             

    dotnet/src/support/UI/PopupWindowFinder.cs

  • Changed IList to ReadOnlyCollection for window handles.
  • Improved null safety in popup handling logic.
  • +5/-4     
    SelectElement.cs
    Refactor `SelectElement` for null safety and better practices

    dotnet/src/support/UI/SelectElement.cs

  • Replaced IList with ReadOnlyCollection for options handling.
  • Refactored to use WrappedElement property for null safety.
  • Improved exception handling in selection methods.
  • +12/-16 
    Configuration changes
    BUILD.bazel
    Enable nullable reference types in Bazel build                     

    dotnet/src/support/BUILD.bazel

    • Changed nullable setting from annotations to enable.
    +2/-2     
    WebDriver.Support.csproj
    Enable nullable reference types in project file                   

    dotnet/src/support/WebDriver.Support.csproj

    • Updated Nullable setting to enable in project file.
    +1/-1     

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

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

    Exception Safety

    The new null checks and exception handling could potentially swallow or transform important exceptions. Need to validate that exceptions are properly propagated and that error states are handled correctly.

    /// <exception cref="ArgumentNullException">If <paramref name="parentDriver"/> is <see langword="null"/>.</exception>
    public EventFiringWebDriver(IWebDriver parentDriver)
    {
        this.WrappedDriver = parentDriver ?? throw new ArgumentNullException(nameof(parentDriver));
    }
    Null Reference

    The removal of explicit null initialization could lead to null reference exceptions if the wrapped element becomes null. The null safety needs thorough validation.

    if (string.IsNullOrEmpty(tagName) || !string.Equals(tagName, "select", StringComparison.OrdinalIgnoreCase))
    {
        throw new UnexpectedTagNameException("select", tagName);
    }
    
    this.WrappedElement = element;

    Copy link
    Contributor

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Add proper parameter validation and type checking before unsafe type casting

    Add null check for frameElement parameter in Frame method before casting to
    IWrapsElement to prevent potential InvalidCastException.

    dotnet/src/support/Events/EventFiringWebDriver.cs [1113-1121]

     public IWebDriver Frame(IWebElement frameElement)
     {
    +    ArgumentNullException.ThrowIfNull(frameElement);
    +    if (frameElement is not IWrapsElement wrapper)
    +    {
    +        throw new ArgumentException("Frame element must implement IWrapsElement", nameof(frameElement));
    +    }
    +    
         IWebDriver driver;
         try
         {
    -        IWrapsElement wrapper = (IWrapsElement)frameElement;
             driver = this.wrappedLocator.Frame(wrapper.WrappedElement);
         }
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: The suggestion addresses a potential runtime error by adding proper null and type validation before casting. This is a significant improvement for robustness and error handling, preventing hard-to-debug InvalidCastExceptions.

    8
    General
    Use standard ArgumentNullException pattern for parameter validation instead of custom exception

    Add null check for args parameter in UnwrapElementArguments method before accessing
    its Length property to prevent potential NullReferenceException.

    dotnet/src/support/Events/EventFiringWebDriver.cs [802-810]

     private static object?[] UnwrapElementArguments(object?[] args)
     {
    -    if (args is null)
    -    {
    -        throw new InvalidOperationException("Cannot unwrap null args");
    -    }
    +    ArgumentNullException.ThrowIfNull(args);
     
         // Walk the args: the various drivers expect unwrapped versions of the elements
         List<object?> unwrappedArgs = new List<object?>(args.Length);
    • Apply this suggestion
    Suggestion importance[1-10]: 5

    Why: Using ArgumentNullException.ThrowIfNull() is more idiomatic in modern C# and provides a consistent pattern for null checks. While both approaches work, the suggested change improves code consistency and readability.

    5

    @RenderMichael RenderMichael merged commit cfcf372 into SeleniumHQ:trunk Jan 15, 2025
    10 checks passed
    @RenderMichael RenderMichael deleted the nullable-support-2 branch January 15, 2025 15:01
    sandeepsuryaprasad pushed a commit to sandeepsuryaprasad/selenium that referenced this pull request Mar 23, 2025
    gryznar pushed a commit to gryznar/selenium that referenced this pull request May 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.

    1 participant