SeleniumHQ / selenium

A browser automation framework and ecosystem.
https://selenium.dev
Apache License 2.0
30.23k stars 8.12k forks source link

[πŸ› Bug]: [dotnet] Unable to use wrapped IWebElements from EventFiringWebDriver within Actions #14513

Open ZachBergman opened 3 days ago

ZachBergman commented 3 days ago

What happened?

We are using EventFiringWebDriver, which returns EventFiringWebElements. In our source, we have an IWebElement wrapping class called WebElementWrapper, which implements IWebElement, IWrapsElement, ILocatable. When using our WebElementWrappers within an Action builder, we get the following exception: "Target element cannot be converted to IWebElementReference".

Our setup...

public interface IWebElementWrapper : IWebElement, IWrapsElement, ILocatable
{
    // Excluded for brevity
}

public class WebElementWrapper : IWebElementWrapper
{
    private IWebElement _webElement;
    public IWebElement WebElement
    {
        get
        {
            if (_webElement == null)
            {
                _webElement = GetWebElement();
            }
            return _webElement;
        }
    }

    public IWebElement WrappedElement => WebElement;

    // Excluded irrelevant content for brevity
}

[TestClass]
public class Tests
{
    [TestMethod]
    public void Test()
    {
        // WebDriver is an EventFiringWebDriver
        new Actions(WebDriver)
            .MoveToElement(WebElementWrapperClassInstance)
            .Click()
            .Build()
            .Perform();
    }
}

It seems the cause of this is the implementation here (shown below with my comments)

// OpenQA.Selenium.Interactions.PointerInputDevice.PointerMoveInteraction
private Dictionary<string, object> ConvertElement()
{
    // this.target is an IWebElement, which doesn't necessarily implement IWebDriverObjectReference
    IWebDriverObjectReference elementReference = this.target as IWebDriverObjectReference;
    if (elementReference == null)
    {
        // We are falling to backup IWrapsElement, however IWrapsElement also does not necessarily implement IWebDriverObjectReference
        IWrapsElement elementWrapper = this.target as IWrapsElement;
        if (elementWrapper != null)
        {
            // In our scenario, since our IWebElement originated from EventFiringWebDriver, it is actually an EventFiringWebElement
            // which does not implement IWebDriverObectReference so it ends up being null here.
            elementReference = elementWrapper.WrappedElement as IWebDriverObjectReference;
        }
    }

    if (elementReference == null)
    {
        throw new ArgumentException("Target element cannot be converted to IWebElementReference");
    }

    Dictionary<string, object> elementDictionary = elementReference.ToDictionary();
    return elementDictionary;
}

In combination with the fact that EventFiringWebDriver issues EventFiringWebElements, which do not implement IWebDriverObjectReference like their WebElement cousins EventFiringWebElement

private class EventFiringWebElement : ITakesScreenshot, IWebElement, IWrapsElement, IWrapsDriver

WebElement

public class WebElement : IWebElement, IFindsElement, IWrapsDriver, ILocatable, ITakesScreenshot, IWebDriverObjectReference

How can we reproduce the issue?

Use the combination of a WebElementWrapper class as shown, use a EventFiringWebDriver, and use the Actions class as shown.

Relevant log output

System.ArgumentException "Target element cannot be converted to IWebElementReference"

Operating System

Windows 10

Selenium version

4.18.0

What are the browser(s) and version(s) where you see this issue?

Any

What are the browser driver(s) and version(s) where you see this issue?

Any

Are you using Selenium Grid?

No

github-actions[bot] commented 3 days ago

@ZachBergman, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!