Search code examples
c#appiumwinappdriver

Why am i getting a Cannot implicitly convert type error for AppiumWebElement on a WindowsElement


I'm in the process of learning Appium with WinAppDriver. I am attemption to connect to an application already launched on the desktop e.g. 'Notepad' and then click the Maximise button.

However, the segment of code

WindowsElement maximizeButton = notepad.FindElementByName("Maximize");

Is giving me this error:

Cannot implicitly convert type 'OpenQA.Selenium.Appium.AppiumWebElement' to 
'OpenQA.Selenium.Appium.Windows.WindowsElement'. An explicit conversion exists (are you missing a cast?)

I don't know why this is happening as nodepad was declared as a WindowsElement and maximizeButton is a WindowsElement. I don't get this error if i declare it as a var.

But why does WindowsElement not work?

    [TestMethod]
    public void AttachToAnExistingAppWindow()
    {
        // https://github.com/Microsoft/WinAppDriver/wiki/Frequently-Asked-Questions/a8c02cfac47b4bf0c12c571b6010c403dcfe5e7f#when-and-how-to-attach-to-an-existing-app-window
        DesiredCapabilities appCapabilities = new DesiredCapabilities();
        appCapabilities.SetCapability("app", "Root");
        WindowsDriver<WindowsElement> DesktopSession = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appCapabilities);

        Assert.IsNotNull(DesktopSession);

        WindowsElement notepad = DesktopSession.FindElementByName("Untitled - Notepad");
        notepad.Click();

        WindowsElement maximizeButton = notepad.FindElementByName("Maximize");
        if (!maximizeButton.Text.Contains("Maximize"))
        {
             maximizeButton.Click();
        }
    }

Solution

  • Thanks, I found this to work..

    WindowsDriver<WindowsElement> session;
    
    session.FindElementByName("Maximize").Click();
    session.FindElementByName("Restore").Click();
    
    // Or using Maximize and restore via xpath
    session.FindElementByXPath($"//Button[starts-with(@Name, \"Maximize\")]").Click();
    session.FindElementByXPath($"//Button[starts-with(@Name, \"Restore\")]").Click();