microsoft / WinAppDriver

Windows Application Driver
MIT License
3.7k stars 1.4k forks source link

WinAppDriver cannot find visible objects #1592

Open Alyaan24 opened 3 years ago

Alyaan24 commented 3 years ago

I have been facing a really weird issue, I've seen other people face it as well but haven't seen a clear fix, my tests sometimes pass, and sometimes randomly fail with the error saying it couldnt find an element with those search parameters. I've ran the same tests together and all 16 of them passed and then ran them again the next morning and they all failed. sometimes they barely pass and sometimes they all do, I've been stuck on this for a long while and would appreciate any help. This is the error log on a simple test to check if a toggle button is turned on, it detects the settings button usually and presses it but then fails to recognize the toggle button.

Windows Application Driver listening for requests at: http://127.0.0.1:4723/ Press ENTER to exit.

========================================== POST /session HTTP/1.1 Accept: application/json, image/png Connection: Keep-Alive Content-Length: 247 Content-Type: application/json;charset=utf-8 Host: 127.0.0.1:4723 User-Agent: selenium/3.141.0 (.net windows)

HTTP/1.1 200 OK Content-Length: 163 Content-Type: application/json

{"sessionId":"BFA5FB1C-D162-4F21-90E6-5D6715D40BC0","status":0,"value":{"app":"App pathway","ms:waitForAppLaunch":5,"platformName":"Windows"}}

========================================== POST /session/BFA5FB1C-D162-4F21-90E6-5D6715D40BC0/timeouts HTTP/1.1 Accept: application/json, image/png Content-Length: 31 Content-Type: application/json;charset=utf-8 Host: 127.0.0.1:4723 User-Agent: selenium/3.141.0 (.net windows)

{"type":"implicit","ms":5000.0} HTTP/1.1 200 OK Content-Length: 63 Content-Type: application/json

{"sessionId":"BFA5FB1C-D162-4F21-90E6-5D6715D40BC0","status":0}

========================================== POST /session HTTP/1.1 Accept: application/json, image/png Content-Length: 170 Content-Type: application/json;charset=utf-8 Host: 127.0.0.1:4723 User-Agent: selenium/3.141.0 (.net windows)

{"desiredCapabilities":{"app":"Root","deviceName":"WindowsPC","newCommandTimeout":90,"platformName":"Windows"},"capabilities":{"firstMatch":[{"platformName":"Windows"}]}} HTTP/1.1 200 OK Content-Length: 111 Content-Type: application/json

{"sessionId":"A65EA81E-B0E5-4DD0-9F20-231510190D05","status":0,"value":{"app":"Root","platformName":"Windows"}}

========================================== POST /session/BFA5FB1C-D162-4F21-90E6-5D6715D40BC0/element HTTP/1.1 Accept: application/json, image/png Content-Length: 35 Content-Type: application/json;charset=utf-8 Host: 127.0.0.1:4723 User-Agent: selenium/3.141.0 (.net windows)

{"using":"name","value":"Settings"} HTTP/1.1 200 OK Content-Length: 101 Content-Type: application/json

{"sessionId":"BFA5FB1C-D162-4F21-90E6-5D6715D40BC0","status":0,"value":{"ELEMENT":"42.788248.4.428"}}

==========================================

POST /session/BFA5FB1C-D162-4F21-90E6-5D6715D40BC0/element/42.788248.4.428/click HTTP/1.1 Accept: application/json, image/png Content-Length: 2 Content-Type: application/json;charset=utf-8 Host: 127.0.0.1:4723 User-Agent: selenium/3.141.0 (.net windows)

{} HTTP/1.1 200 OK Content-Length: 63 Content-Type: application/json

{"sessionId":"BFA5FB1C-D162-4F21-90E6-5D6715D40BC0","status":0}

========================================== POST /session/BFA5FB1C-D162-4F21-90E6-5D6715D40BC0/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 User-Agent: selenium/3.141.0 (.net windows)

{"using":"class name","value":"ToggleSwitch"} 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."}}

Let me know if you need any more info

anunay1 commented 3 years ago

"They all failed" because of the same issue "An element could not be located on the page using the given search parameters"

Alyaan24 commented 3 years ago

"They all failed" because of the same issue "An element could not be located on the page using the given search parameters"

Yes but clearly the elements i'm trying to find are on the screen and the names are correct

anunay1 commented 3 years ago

Can you get the page source when they all failed?

Alyaan24 commented 3 years ago

Can you get the page source when they all failed?

Im sorry, I'm pretty new to this, what do you mean by page source?

Shakevg commented 3 years ago

Can you get the page source when they all failed?

Im sorry, I'm pretty new to this, what do you mean by page source?

@Alyaan24 This is content of the page in XML format that extracted by WinAppDriver and used to search items https://github.com/microsoft/WinAppDriver/search?q=pagesource

Tree55Topz commented 3 years ago

@Alyaan24 do you still have issues with this? This is a pretty common thing with UI Automation, and with just about any UI testing framework. You need to make sure your methods are set up in a way that they are stable. This meaning you are catching exceptions that are thrown when an element may not be ready. Intermittent failures usually indicate this

Ill share with you an approach I have in my solution (C#). Its a WaitForObject method that will wait for an element to be visible and interactable before interacting with it. Remember... Stability > Speed when you are building your solution out. Its much easier to make tests faster than it is to make them stable, especially when you have a suite with many tests.

In this example, this method will use a Timer class to wait for an element to be accessible. I prefer this over threads.

// Wait for an Object to be accessible, use a custom timeout
        public WindowsElement WaitForObject(Func<WindowsElement> element, int timeout)
        {

            WindowsElement waitElement = null;

            try
            {
                var wait = new DefaultWait<WindowsDriver<WindowsElement>>(_session)
                {
                    Timeout = TimeSpan.FromSeconds(timeout),
                    PollingInterval = TimeSpan.FromSeconds(1)
                };

                wait.IgnoreExceptionTypes(typeof(WebDriverException));
                wait.IgnoreExceptionTypes(typeof(InvalidOperationException));
                wait.IgnoreExceptionTypes(typeof(StaleElementReferenceException));
                wait.IgnoreExceptionTypes(typeof(NoSuchElementException));
                wait.IgnoreExceptionTypes(typeof(NotFoundException));
                wait.IgnoreExceptionTypes(typeof(WebException));
                wait.IgnoreExceptionTypes(typeof(WebDriverTimeoutException));

                wait.Until(driver =>
                {
                    waitElement = element();

                    return waitElement != null && waitElement.Enabled && waitElement.Displayed;
                });

                return waitElement;

            } catch (Exception e)
            {
                Console.WriteLine(e);

                testResult.AssertAreEqual(false, true, "Failed to WaitForObject.. Check screenshots");

                return waitElement;
            }
        }

you can do the same sort of thing with a while loop that is using Thread.Sleep and a counter to wait for a max number of attempts before giving up.