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] Start adding nullable reference type annotations to the Support package #14779

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

Conversation

RenderMichael
Copy link
Contributor

@RenderMichael RenderMichael commented Nov 20, 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

This implements nullable reference types and the requisite null checks (alongside some modernization) on the first half of the support package, as well as types in the support namespace but in the regular package.

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

  • Added nullable reference type annotations across multiple files to improve null safety.
  • Introduced exception handling for null arguments in constructors and methods.
  • Simplified method logic and improved type checking in several classes.
  • Updated project configuration to enable nullable annotations.
  • Set default values for mock objects in tests to ensure consistent behavior.

Changes walkthrough 📝

Relevant files
Enhancement
18 files
FindElementEventArgs.cs
Add nullability and exception handling to FindElementEventArgs

dotnet/src/support/Events/FindElementEventArgs.cs

  • Added nullability annotations to parameters and properties.
  • Introduced exception handling for null arguments.
  • Removed private fields and used auto-properties.
  • +12/-23 
    GetShadowRootEventArgs.cs
    Add nullability and exception handling to GetShadowRootEventArgs

    dotnet/src/support/Events/GetShadowRootEventArgs.cs

  • Added nullability annotations to parameters and properties.
  • Introduced exception handling for null arguments.
  • Removed private fields and used auto-properties.
  • +5/-13   
    WebDriverExceptionEventArgs.cs
    Add nullability and exception handling to WebDriverExceptionEventArgs

    dotnet/src/support/Events/WebDriverExceptionEventArgs.cs

  • Added nullability annotations to parameters and properties.
  • Introduced exception handling for null arguments.
  • Removed private fields and used auto-properties.
  • +6/-14   
    WebDriverNavigationEventArgs.cs
    Add nullability and exception handling to WebDriverNavigationEventArgs

    dotnet/src/support/Events/WebDriverNavigationEventArgs.cs

  • Added nullability annotations to parameters and properties.
  • Introduced exception handling for null arguments.
  • Removed private fields and used auto-properties.
  • +9/-16   
    WebDriverScriptEventArgs.cs
    Add nullability and exception handling to WebDriverScriptEventArgs

    dotnet/src/support/Events/WebDriverScriptEventArgs.cs

  • Added nullability annotations to parameters and properties.
  • Introduced exception handling for null arguments.
  • Removed private fields and used auto-properties.
  • +5/-13   
    WebElementEventArgs.cs
    Add nullability and exception handling to WebElementEventArgs

    dotnet/src/support/Events/WebElementEventArgs.cs

  • Added nullability annotations to parameters and properties.
  • Introduced exception handling for null arguments.
  • Removed private fields and used auto-properties.
  • +5/-13   
    WebElementValueEventArgs.cs
    Add nullability and exception handling to WebElementValueEventArgs

    dotnet/src/support/Events/WebElementValueEventArgs.cs

  • Added nullability annotations to parameters and properties.
  • Introduced exception handling for null arguments.
  • +6/-3     
    WebDriverExtensions.cs
    Enhance WebDriverExtensions with nullability and improved logic

    dotnet/src/support/Extensions/WebDriverExtensions.cs

  • Added nullability annotations to method parameters.
  • Improved exception handling and type checking.
  • Simplified method logic.
  • +32/-40 
    LoadableComponentException.cs
    Add nullability annotations to LoadableComponentException

    dotnet/src/support/UI/LoadableComponentException.cs

    • Added nullability annotations to constructor parameters.
    +2/-3     
    LoadableComponent{T}.cs
    Add nullability and improve documentation in LoadableComponent

    dotnet/src/support/UI/LoadableComponent{T}.cs

  • Added nullability annotations to properties.
  • Improved documentation comments.
  • +10/-20 
    PopupWindowFinder.cs
    Enhance PopupWindowFinder with nullability and simplified logic

    dotnet/src/support/UI/PopupWindowFinder.cs

  • Added nullability annotations to parameters.
  • Simplified method logic.
  • +7/-13   
    SlowLoadableComponent{T}.cs
    Add nullability and improve exception handling in
    SlowLoadableComponent

    dotnet/src/support/UI/SlowLoadableComponent{T}.cs

  • Added nullability annotations to properties.
  • Improved exception handling.
  • +17/-29 
    UnexpectedTagNameException.cs
    Add nullability annotations to UnexpectedTagNameException

    dotnet/src/support/UI/UnexpectedTagNameException.cs

    • Added nullability annotations to constructor parameters.
    +2/-3     
    DefaultWait{T}.cs
    Enhance DefaultWait with nullability and improved logic   

    dotnet/src/webdriver/Support/DefaultWait{T}.cs

  • Added nullability annotations to methods and properties.
  • Improved exception handling and type checking.
  • +26/-51 
    IClock.cs
    Enable nullable reference types in IClock                               

    dotnet/src/webdriver/Support/IClock.cs

    • Enabled nullable reference types.
    +2/-0     
    IWait{T}.cs
    Add nullability annotations to IWait interface                     

    dotnet/src/webdriver/Support/IWait{T}.cs

    • Added nullability annotations to method signatures.
    +5/-1     
    SystemClock.cs
    Enable nullable reference types and simplify SystemClock 

    dotnet/src/webdriver/Support/SystemClock.cs

    • Enabled nullable reference types.
    • Simplified method logic.
    +5/-12   
    WebDriverWait.cs
    Enable nullable reference types and simplify WebDriverWait

    dotnet/src/webdriver/Support/WebDriverWait.cs

    • Enabled nullable reference types.
    • Simplified method logic.
    +4/-4     
    Tests
    1 files
    EventFiringWebDriverTest.cs
    Set default values for mocks in EventFiringWebDriverTest 

    dotnet/test/support/Events/EventFiringWebDriverTest.cs

    • Set default values for mock objects.
    +8/-2     
    Configuration changes
    1 files
    WebDriver.Support.csproj
    Enable nullable annotations in project file                           

    dotnet/src/support/WebDriver.Support.csproj

    • Updated project file to enable nullable annotations.
    +2/-0     

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

    @@ -40,8 +40,14 @@ public class EventFiringWebDriverTest
    public void Setup()
    {
    mockDriver = new Mock<IWebDriver>();
    mockElement = new Mock<IWebElement>();
    mockShadowRoot = new Mock<ISearchContext>();
    mockElement = new Mock<IWebElement>()
    Copy link
    Contributor Author

    Choose a reason for hiding this comment

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

    The null checks found a hole in the mocking, these changes are necessary to make those tests pass.

    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

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

    Possible Bug
    The Until method may throw NullReferenceException when condition returns null for non-nullable value types

    Code Smell
    The Until method has complex branching logic that could be simplified and made more maintainable

    Code Smell
    The constructor allows null URL parameter but does not document this in XML comments

    @RenderMichael RenderMichael changed the title Null support 1 [dotnet] Start adding nullable reference type annotations to the Support package Nov 20, 2024
    Copy link
    Contributor

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Fix boolean result type checking to prevent potential infinite loops in wait conditions

    The boolean result check in the Until method is incorrect. The current code only
    returns when result is true, but fails to handle false values properly, which could
    lead to infinite loops. The check should verify if the result is actually a boolean
    value before evaluating it.

    dotnet/src/webdriver/Support/DefaultWait{T}.cs [163-166]

    -if (result is true)
    +if (result is bool boolResult)
     {
    -    return result;
    +    if (boolResult)
    +    {
    +        return result;
    +    }
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: The suggestion addresses a critical issue that could cause infinite loops in wait conditions. The current code only checks for true values but doesn't properly handle boolean type verification, which could lead to runtime errors or unexpected behavior.

    9
    Ensure consistency between method signature nullability annotations to prevent potential null reference exceptions

    The return value is marked as non-null with [return: NotNull] but the input
    parameter allows null values (TResult?). This could lead to runtime errors if null
    is returned. Either remove NotNull attribute or ensure the condition function cannot
    return null.

    dotnet/src/webdriver/Support/IWait{T}.cs [63-64]

    -[return: NotNull]
    -TResult Until<TResult>(Func<T, TResult?> condition);
    +TResult Until<TResult>(Func<T, TResult> condition) where TResult : notnull;
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: The suggestion identifies a critical inconsistency in null handling that could lead to runtime exceptions. The current implementation has contradicting nullability annotations which is a significant issue for type safety.

    9
    General
    Enable full null reference type checking to catch potential null reference exceptions at compile time

    Setting Nullable to 'annotations' only enables nullable warning annotations but does
    not enforce null checks. For full null safety, use 'enable' instead.

    dotnet/src/webdriver/Support/WebDriver.Support.csproj [9]

    -<Nullable>annotations</Nullable>
    +<Nullable>enable</Nullable>
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: This is a valuable suggestion as enabling full null checking would provide stronger compile-time guarantees against null reference exceptions, which aligns with the PR's focus on improving null safety.

    8
    Improve error message clarity by including the actual type received in exception messages

    The null check for responseObject should include a more specific error message that
    includes the actual type received, to help with debugging unexpected response
    formats.

    dotnet/src/support/Extensions/WebDriverExtensions.cs [54-57]

     if (responseObject is not Response screenshotResponse)
     {
    -    throw new WebDriverException($"Unexpected failure getting screenshot; response was not in the proper format: {responseObject}");
    +    throw new WebDriverException($"Unexpected failure getting screenshot; response was not in the proper format. Expected Response type but got: {responseObject?.GetType().FullName ?? "null"}");
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 4

    Why: The suggestion improves error message diagnostics by including the actual type of the unexpected response, which would help with debugging. While useful, this is a minor enhancement to error reporting rather than a critical functionality fix.

    4

    💡 Need additional feedback ? start a PR chat

    @nvborisenko
    Copy link
    Member

    Even don't spend your time on Support package, it is kind of unsupported.

    @RenderMichael
    Copy link
    Contributor Author

    @nvborisenko I get it 😁

    But it's so small, and so simple to get it working. This PR does most of the work.

    The EventFiringWebDriver is the single remaining file, which I left out of this PR so it doesn't get too big. I can include it in this one, if that simplifies things.

    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