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] Enable ShadowRoot to use By query mechanism #15336

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

Conversation

RenderMichael
Copy link
Contributor

@RenderMichael RenderMichael commented Feb 25, 2025

User description

Enables ShadowRoot to use the element query mechanism in By

Motivation and Context

Fixes #15335

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

Bug fix, Tests


Description

  • Enabled ShadowRoot to use By query mechanism for element searching.

  • Added FindElement and FindElements methods to ShadowRoot for consistency.

  • Introduced new tests to validate By methods in ShadowRoot.

  • Enhanced error handling for null By arguments in ShadowRoot.


Changes walkthrough 📝

Relevant files
Bug fix
ShadowRoot.cs
Add `By` query mechanism support to `ShadowRoot`                 

dotnet/src/webdriver/ShadowRoot.cs

  • Implemented IFindsElement interface in ShadowRoot.
  • Added FindElement and FindElements methods using By.
  • Introduced overloads for FindElement and FindElements with mechanism
    and value parameters.
  • Improved error handling for null By arguments.
  • +27/-5   
    Tests
    ShadowRootHandlingTest.cs
    Add tests for `ShadowRoot` `By` query mechanism                   

    dotnet/test/common/ShadowRootHandlingTest.cs

  • Added tests for ShadowRoot using By methods.
  • Validated FindElement and FindElements functionality in ShadowRoot.
  • Included test for empty ID search returning empty results.
  • +16/-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

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    🎫 Ticket compliance analysis 🔶

    15335 - Partially compliant

    Compliant requirements:

    • Implement By type special handling in ShadowRoot class
    • Make FindElements(By.Id("")) return an empty list instead of throwing
    • Ensure consistent behavior with WebDriver and WebElement

    Non-compliant requirements:

    • Support custom implementations through By.FindElementMethod and By.FindElementsMethod
    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Missing Implementation

    The PR does not implement support for custom By.FindElementMethod and By.FindElementsMethod overrides, which was part of the original requirements

    public IWebElement FindElement(By by)
    {
        if (by is null)
        {
            throw new ArgumentNullException(nameof(by), "by cannot be null");
        }
    
        return by.FindElement(this);
    }
    
    /// <summary>
    /// Finds a child element matching the given mechanism and value.
    /// </summary>
    /// <param name="mechanism">The mechanism by which to find the element.</param>
    /// <param name="value">The value to use to search for the element.</param>
    /// <returns>The first <see cref="IWebElement"/> matching the given criteria.</returns>
    public virtual IWebElement FindElement(string mechanism, string value)
    {
        Dictionary<string, object> parameters = new Dictionary<string, object>();
        parameters.Add("id", this.shadowRootId);
        parameters.Add("using", mechanism);
        parameters.Add("value", value);
    
        Response commandResponse = this.driver.InternalExecute(DriverCommand.FindShadowChildElement, parameters);
        return this.driver.GetElementFromResponse(commandResponse);
    }
    
    /// <summary>
    /// Finds all <see cref="IWebElement">IWebElements</see> within the current context
    /// using the given mechanism.
    /// </summary>
    /// <param name="by">The locating mechanism to use.</param>
    /// <returns>A <see cref="ReadOnlyCollection{T}"/> of all <see cref="IWebElement">WebElements</see>
    /// matching the current criteria, or an empty list if nothing matches.</returns>
    /// <exception cref="ArgumentNullException">If <paramref name="by"/> is <see langword="null"/>.</exception>
    public ReadOnlyCollection<IWebElement> FindElements(By by)
    {
        if (by is null)
        {
            throw new ArgumentNullException(nameof(by), "by cannot be null");
        }
    
        return by.FindElements(this);
    }
    
    /// <summary>
    /// Finds all child elements matching the given mechanism and value.
    /// </summary>
    /// <param name="mechanism">The mechanism by which to find the elements.</param>
    /// <param name="value">The value to use to search for the elements.</param>
    /// <returns>A collection of all of the <see cref="IWebElement">IWebElements</see> matching the given criteria.</returns>
    public virtual ReadOnlyCollection<IWebElement> FindElements(string mechanism, string value)
    {
        Dictionary<string, object> parameters = new Dictionary<string, object>();
        parameters.Add("id", this.shadowRootId);
        parameters.Add("using", mechanism);
        parameters.Add("value", value);
    
        Response commandResponse = this.driver.InternalExecute(DriverCommand.FindShadowChildElements, parameters);
        return this.driver.GetElementsFromResponse(commandResponse);
    }

    Copy link
    Contributor

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Add parameter validation checks

    Add null checks for mechanism and value parameters in FindElement and
    FindElements methods to prevent potential NullReferenceException

    dotnet/src/webdriver/ShadowRoot.cs [105-110]

     public virtual IWebElement FindElement(string mechanism, string value)
     {
    +    if (string.IsNullOrEmpty(mechanism))
    +    {
    +        throw new ArgumentNullException(nameof(mechanism), "mechanism cannot be null or empty");
    +    }
    +    if (value == null)
    +    {
    +        throw new ArgumentNullException(nameof(value), "value cannot be null");
    +    }
         Dictionary<string, object> parameters = new Dictionary<string, object>();
         parameters.Add("id", this.shadowRootId);
         parameters.Add("using", mechanism);
         parameters.Add("value", value);
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    __

    Why: The suggestion addresses an important defensive programming practice by adding null checks for method parameters. This prevents potential runtime errors and provides clear error messages, which is crucial for API reliability and debugging.

    Medium
    • More

    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.

    [🐛 Bug]: [dotnet] ShadowRoot element searching does not use By's method
    1 participant