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.AppiumDriver
1.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."}}"