Appium desktop app test throws element could not be located exception, but passes when the app is launched before the test runs

1

I'm trying to learn Appium and using it for WPF application testing. Tests against Calculator or Notepad are running fine, but recently I've encountered a problem when trying to test custom WPF application.

Appium desktop app tests throw "An element could not be located on the page using the given search parameters" exception, but pass without problems when app is launched before test run. So I guess that my Setup/Init phase is somehow incorrect but I don't know why.

Error occurs when the test is run without the app launched first (so when the SetUp phase has to launch the app). Test passes when the app is started before test run, or even when it is left open from previous failed test run.

App launch takes about 10 to 15 second, during which at first slash screen shows up, and then the main window of the application.

Appium.WebDriver nuget packege is used in the project, version 3.0.0.2

I've tried Thread.Sleep for 30 seconds, but it doesn't resolve the issue.

[TestFixture]
public class DesktopAppSession
{
    protected WindowsDriver<WindowsElement> _session;
    protected const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723";
    protected const string AppId = @"<path to app.exe>";

    [SetUp]
    public void TestInit()
    {
        if (_session != null)
            return;

        var appCapabilities = new DesiredCapabilities();
        appCapabilities.SetCapability("app", AppId);
        appCapabilities.SetCapability("deviceName", "WindowsPC");
        _session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appCapabilities);

        Assert.IsNotNull(_session);

        Thread.Sleep(TimeSpan.FromSeconds(30));

        _session.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
    }

    [TearDown]
    public void TestCleanup()
    {
        if (_session != null)
        {
            _session.Quit();
            _session = null;
        }
    }

    [Test]
    public void UserInfoModalShowsUp()
    {
        var userInfoButtonAName = "UserInfoButtonAName";
        var userInfoButtonId = "UserInfoButtonAID";

        var userInfoButton = _session.FindElementByAccessibilityId(userInfoButtonId);

        Assert.True(userInfoButton != null);

        userInfoButton.Click();

        var userDetailsTitleLabel = _session.FindElementByName("User details");

        userDetailsTitleLabel?.Click();

        Assert.True(true);
    }
}

Exception message:

System.InvalidOperationException HResult=0x80131509 Message=An element could not be located on the page using the given search parameters. Source=WebDriver StackTrace: at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse) at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary2 parameters) at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value) at OpenQA.Selenium.Appium.AppiumDriver1.FindElement(String by, String value)

Log from WinAppDriver:

"POST /session/23293B57-F396-47CC-83EF-FCA491E269B0/element HTTP/1.1 Accept: application/json, image/png Content-Length: 56 Content-Type: application/json;charset=utf-8 Host: 127.0.0.1:4723

{"using":"accessibility id","value":"UserInfoButtonAID"} HTTP/1.1 404 Not Found Content-Length: 139 Content-Type: application/json

{"status":7,"value":{"error":"no such element","message":"An element could not be located on the page using the given search parameters."}}"

c#
appium-desktop
winappdriver
asked on Stack Overflow May 29, 2019 by WhisperWind • edited May 29, 2019 by WhisperWind

1 Answer

0

You will most likely need to switch the window handle to the correct one.

Winappdriver uses window handles for top level windows. Most likely, your splashscreen will be a top level window, and your actual application will also be a top level window. So winappdriver will have at least 2 window handles. Your driver (_session in your case) has a property called WindowHandles which contains a list of window handles. These handles get added chronologically, so the most recent handle (the one from your application) should be the last window handle.

This example shows you how to switch window handles:

if (_session.CurrentWindowHandle != _session.WindowHandles.Last())
{
    _session.SwitchTo().Window(_session.WindowHandles.Last());
}

You can also verify if you have the correct window handle by checking the page source property from your driver like this: _session.PageSource;. The page source is a xml representation of the current selected window handle. You can put it in the Visual Studio watch and copy over the xml to a xml formatter utility program for readability.

Additional information about the splash screen problem and alternative ways to fix it, can be found here. Be sure to check the answers from user timotiusmargo.

answered on Stack Overflow May 31, 2019 by PixelPlex • edited May 31, 2019 by PixelPlex

User contributions licensed under CC BY-SA 3.0