2gis / Winium.StoreApps.CodedUi

Prototype of Winium.StoreApps driver using CodedUI. Implements JsonWireProtocol for automation of Windows Phone applications
10 stars 8 forks source link

GetPageSource sometimes throws ElementNotAvailableException #6

Closed NickAb closed 9 years ago

NickAb commented 9 years ago

Some times GetPageSource method throws ElementNotAvailableException

ERROR: test_click_button (__main__.TestApp2)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:/Users/*/Desktop/fbautomation.py", line 158, in test_click_button
    print(self.driver.page_source)
  File "C:\Python27\lib\site-packages\selenium-2.45.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 438, in page_source
    return self.execute(Command.GET_PAGE_SOURCE)['value']
  File "C:\Python27\lib\site-packages\selenium-2.45.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 175, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium-2.45.0-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 136, in check_response
    raise exception_class(value)
WebDriverException: Message: System.Windows.Automation.ElementNotAvailableException: Element not available ---> System.Runtime.InteropServices.COMException: Operation timed out. (Exception from HRESULT: 0x80131505)

   at UIAutomationClient.IUIAutomationElement.FindAllBuildCache(TreeScope scope, IUIAutomationCondition condition, IUIAutomationCacheRequest cacheRequest)

   at System.Windows.Automation.AutomationElement.FindAll(TreeScope scope, Condition condition)

   --- End of inner exception stack trace ---

   at System.Windows.Automation.AutomationElement.FindAll(TreeScope scope, Condition condition)

   at Winium.StoreApps.CodedUITestProject.WiniumElement.GetChildrens()

   at Winium.StoreApps.CodedUITestProject.WiniumElement.AsXml()

   at Winium.StoreApps.CodedUITestProject.CommandExecutors.GetPageSourceExecutor.DoImpl()

   at Winium.StoreApps.CodedUITestProject.CommandExecutors.CommandExecutorBase.Do()
NickAb commented 9 years ago

Both FindAll and FindFirst are prone to this exception, which is raised when trying to find elements in WebView that has loaded its data recently, exception is thrown when methods try to build cache, so it might be due to trying to iterate visual tree while it is being rebuild by another thread.

Replacing FindAll with

private IEnumerable<WiniumElement> GetChildrens()
        {
            var elementNode = TreeWalker.ControlViewWalker.GetFirstChild(this.AutomationElement);

            while (elementNode != null)
            {
                yield return new WiniumElement(elementNode);
                elementNode = TreeWalker.ControlViewWalker.GetNextSibling(elementNode);
            }
        }

Solves the problem. Needs further investigation. Most likely we will have to replace both FindFirst and FindAll calls with custom TreeWalker iteration method.

This will solve the problem with not being able to get page source for views with webviews and not being able to find element in webview due to same error.