1

I am new to automation and I am trying to automate the WPF application using WinAppDriver with C#. I am able to load the application but getting the error like {"An element could not be located on the page using the given search parameters."} while trying to find the element with Name/AccessibilityId even after keeping the wait time.

See below:

POST /session/09551C9F-CF20-4C2B-A900-F17D2483F9D8/element HTTP/1.1
Accept: application/json, image/png
Content-Length: 45
Content-Type: application/json;charset=utf-8
Host: 127.0.0.1:4723

{"using":"accessibility id","value":"TxtPwd"}
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."}}

I don't know what is happening. Any suggestions?

I did like - check for the elements and automation-id/name of element through inspect tool - set developer mode active - wait time before finding the element

 var aDesiredCapabilities = new DesiredCapabilities();
             aDesiredCapabilities.SetCapability("app", @"PathToApplication");
             aDesiredCapabilities.SetCapability("deviceName", "Windows 10");

             var aWindow = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), aDesiredCapabilities);
             aWindow.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);

             aWindow.FindElementByAccessibilityId("TxtPwd").SendKeys("qwerty");
             aWindow.FindElementByAccessibilityId("TxtUser").SendKeys("123456");
             aWindow.FindElementByAccessibilityId("Clear").Click();

             aWindow.FindElementByAccessibilityId("TxtPwd").SendKeys("qwerty");
             aWindow.FindElementByAccessibilityId("TxtUser").SendKeys("123456");
             aWindow.FindElementByAccessibilityId("Login");
GBouffard
  • 1,125
  • 4
  • 11
  • 24
Anil
  • 21
  • 1
  • 6
  • Have you tried these different approaches? https://github.com/microsoft/WinAppDriver/wiki/Authoring-Your-Own-Test-Script#supported-locators-to-find-ui-elements – popsiporkkanaa Oct 16 '19 at 18:16
  • I tried with FindElementByAccessibilityId and FindElementByName. Others I didn't. I expect it should work at least with FindElementByAccessibilityId. – Anil Oct 17 '19 at 03:26
  • instead of sendkeys, try clicking the element by using click() function. See first your element is interactable. element.enabled == true, element.displayed == true. another thing you need to focus is you are working on right window. – kishor sharma Oct 17 '19 at 06:12
  • I used click function of elements as well like aWindow.FindElementByAccessibilityId("Login").Click(); but this statement(aWindow.FindElementByAccessibilityId("Login");) itself is throwing exception. And the buttons are clickable. – Anil Oct 17 '19 at 06:34

3 Answers3

1

Is this user name password field showing in a pop-up?

Once you launch the application, put a short sleep before trying to access application UI elements. I suggest the following.

System.Threading.Thread.Sleep(TimeSpan.FromSeconds(10));

A better way would be to use an instance of WebDriverWait class to wait until the element is loaded.

WebDriverWait wdv = new WebDriverWait(sessionAppWinForms, TimeSpan.FromSeconds(10));
var txtPwd = aWindow.FindElementByAccessibilityId("TxtPwd");
wdv.Until(x => txtPwd.Displayed);

Update: I suggest inspecting UI controls with WinAppDriver UI Recorder. The latest version didn't work on my PC that's why I recommend using version 1.0. The download link is given below. https://github.com/microsoft/WinAppDriver/releases/tag/UiR_v1.0-RC

WinAppDriver is just a helper program, you can create automation scripts without using it. Sometimes the application takes a little longer to launch, in such cases you might use the WebDriverWait class to wait for certain conditions to be true. For example, wait for a certain label or textbox to be present on the screen. You might use the following line of code to wait unconditionally for a few seconds.

System.Threading.Thread.Sleep(5000);

I teach a Udemy course about test automaton with WinAppDriver in C# .Net. These concepts are covered in detail. You may see it here.

Naeem A. Malik
  • 995
  • 4
  • 19
  • I tried keeping Thread.Sleep(TimeSpan.FromSeconds(10)); but it didn't work. And I also tried aWindow.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5); instead of webdriverwait. No luck. – Anil Oct 18 '19 at 05:34
  • Anil, is this user name/password field showing in a pop up box? If yes then try creating a desktop session with "app", "Root". Find the window which contains the credentials fields, and performs operations on that window. – Naeem A. Malik Oct 18 '19 at 08:26
  • No Naeem, those username and password are two controls on normal WPF page. Its actually a simple WPF application which has those controls. And also, I already tried this desktop session with "app", "Root" but it didn't work. – Anil Nov 04 '19 at 06:03
  • Okay, what control properties are shown by the WinAppDriver UI Recorder? Have you tried the XPath generated by the UI Recorder with the method "FindElementByXPath". – Naeem A. Malik Nov 05 '19 at 16:08
  • I am not using UI Recorder. I am just using the WinAppDriver and getting the properties of controls with inspect tool. – Anil Nov 06 '19 at 04:54
  • The same thing is happening with Calculator application also. Not able to automate the button clicks. – Anil Nov 06 '19 at 05:03
  • Anil, try using WinAppDriverUIRecorder. use version 1.0 instead of the latest one. It is very lightweight. I'll add the download link to my main answer. – Naeem A. Malik Nov 06 '19 at 14:35
  • Naeem, why do we need to use UI Recorder? Can't we do without that? When I tried with some sample WPF application and Winforms application I was able to automate the button clicks. But when I tried with calculator application, not able to automate the clicks. session.FindElementByName("Seven").Click(); this is throwing an exception. I would like to automate the calculator application clicks. Any ideas? – Anil Nov 18 '19 at 11:21
  • Anil, the WAD UIR is just a helping aid. You can work without it. This code should work correctly I believe. You can wait for a few seconds after the application launch so that the UI is displayed completely. Use the method call System.Threading.Thread.Sleep(4000); Let me add the same to the main answer as well. – Naeem A. Malik Nov 19 '19 at 10:39
  • I tried keeping wait time also like aWindow.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5); aWindow.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10); But it didn't work. – Anil Nov 20 '19 at 10:31
  • I tried UI Recorder and generated the XPath of the element and tried finding the element with XPath. But still WinAppDriver not able find the element. – Anil Nov 21 '19 at 11:29
0

The application may be opened in another window out of your aWindow scope.

You can try to create a desktop driver session and start your process using Process.Start() method.

shatulsky
  • 306
  • 2
  • 10
0

If your application is running as administrator, then both WinAppDriver & Inspect.exe must run as administrator as well.

omar Enayet
  • 61
  • 1
  • 3