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] Annotate nullability on SafariOptions, error, and enums #15219

Merged
merged 2 commits 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 SafariOptions, error, and enums

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


Description

  • Enabled nullable reference types in multiple files for better null-safety.

  • Refactored SafariOptions properties to use auto-properties.

  • Improved WebDriverError error mapping logic with dictionary initialization.

  • Replaced List with HashSet for KnownSpecCompliantCapabilityNames for efficiency.


Changes walkthrough 📝

Relevant files
Enhancement
CapabilityType.cs
Refactored capability handling and enabled nullable types

dotnet/src/webdriver/CapabilityType.cs

  • Enabled nullable reference types.
  • Replaced List with HashSet for KnownSpecCompliantCapabilityNames.
  • Added null-check in IsSpecCompliantCapabilityName method.
  • +11/-2   
    IRotatable.cs
    Enabled nullable reference types in `IRotatable`                 

    dotnet/src/webdriver/IRotatable.cs

    • Enabled nullable reference types.
    +2/-0     
    SafariOptions.cs
    Refactored `SafariOptions` and enabled nullable types       

    dotnet/src/webdriver/Safari/SafariOptions.cs

  • Enabled nullable reference types.
  • Refactored properties to use auto-properties.
  • Simplified UseTechnologyPreview and related logic.
  • +9/-22   
    ScreenOrientation.cs
    Enabled nullable reference types in `ScreenOrientation`   

    dotnet/src/webdriver/ScreenOrientation.cs

    • Enabled nullable reference types.
    +2/-0     
    WebDriverError.cs
    Refactored `WebDriverError` error mapping logic                   

    dotnet/src/webdriver/WebDriverError.cs

  • Enabled nullable reference types.
  • Simplified error mapping initialization with dictionary initializer.
  • Improved error handling with TryGetValue.
  • +37/-36 
    WebDriverResult.cs
    Enabled nullable reference types in `WebDriverResult`       

    dotnet/src/webdriver/WebDriverResult.cs

    • Enabled nullable reference types.
    +2/-0     
    WindowType.cs
    Enabled nullable reference types in `WindowType`                 

    dotnet/src/webdriver/WindowType.cs

    • Enabled nullable reference types.
    +3/-0     

    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: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Error Handling

    The default case in ResultFromError() now returns UnsupportedOperation instead of using the error parameter. This could mask actual errors by converting them all to UnsupportedOperation.

    if (!resultMap.TryGetValue(error, out WebDriverResult result))
    {
        return WebDriverResult.UnsupportedOperation;
    }
    Property Initialization

    Multiple properties are initialized to false both in the constructor and via auto-property initialization. This redundancy could lead to confusion about the actual initial state.

        this.TechnologyPreview = false;
        this.AddKnownCapabilityName(SafariOptions.EnableAutomaticInspectionSafariOption, "EnableAutomaticInspection property");
        this.AddKnownCapabilityName(SafariOptions.EnableAutomaticProfilingSafariOption, "EnableAutomaticProfiling property");
    }
    
    /// <summary>
    /// Allows the Options class to be used with a Safari Technology Preview driver
    /// </summary>
    public void UseTechnologyPreview()
    {
        this.TechnologyPreview = true;
        this.BrowserName = "Safari Technology Preview";
    }
    
    /// <summary>
    /// Gets or sets a value indicating whether to have the driver preload the
    /// Web Inspector and JavaScript debugger in the background.
    /// </summary>
    public bool TechnologyPreview { get; private set; } = false;
    
    /// <summary>
    /// Gets or sets a value indicating whether to have the driver preload the
    /// Web Inspector and JavaScript debugger in the background.
    /// </summary>
    public bool EnableAutomaticInspection { get; set; } = false;
    
    /// <summary>
    /// Gets or sets a value indicating whether to have the driver preload the
    /// Web Inspector and start a timeline recording in the background.
    /// </summary>
    public bool EnableAutomaticProfiling { get; set; } = false;

    Copy link
    Contributor

    qodo-merge-pro bot commented Feb 3, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Add null check for input parameter

    The ResultFromError method should validate the input parameter error for null
    before attempting to use it with TryGetValue. Add a null check to prevent
    potential NullReferenceException.

    dotnet/src/webdriver/WebDriverError.cs [211-219]

     public static WebDriverResult ResultFromError(string error)
     {
    +    if (error == null)
    +    {
    +        return WebDriverResult.UnsupportedOperation;
    +    }
         if (!resultMap.TryGetValue(error, out WebDriverResult result))
         {
             return WebDriverResult.UnsupportedOperation;
         }
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: The suggestion addresses a potential null reference exception by adding a null check, which is particularly important given the newly enabled nullable reference types. This is a critical defensive programming practice.

    8
    General
    Validate state before changing technology preview

    The UseTechnologyPreview() method should be called before setting any other
    options to ensure proper initialization. Add validation to prevent setting
    options after technology preview is enabled.

    dotnet/src/webdriver/Safari/SafariOptions.cs [65-69]

     public void UseTechnologyPreview()
     {
    +    if (this.EnableAutomaticInspection || this.EnableAutomaticProfiling)
    +    {
    +        throw new InvalidOperationException("Cannot enable Technology Preview after setting other options");
    +    }
         this.TechnologyPreview = true;
         this.BrowserName = "Safari Technology Preview";
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: The suggestion adds important state validation to prevent inconsistent configuration when enabling technology preview mode, which could lead to runtime issues. This improves the robustness of the SafariOptions class.

    7

    @RenderMichael
    Copy link
    Contributor Author

    Failed tests are not related to the changes:

    //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 d458c6e into SeleniumHQ:trunk Feb 3, 2025
    9 of 10 checks passed
    @RenderMichael RenderMichael deleted the nullable branch February 3, 2025 18:18
    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