我有ASP应用程序堆栈ASP MVC AngularJS,我想测试UI.
现在我正在为PhantomJS和WebKit驱动程序尝试Selenium.
现在我正在为PhantomJS和WebKit驱动程序尝试Selenium.
测试页面 – 使用单个元素的视图 – < li>它从服务器动态加载并由Angular绑定.
<div id="items"> <li>text</li> <li>text2</li> </div>
我试图通过测试
_driver.FindElements(By.TagName('li'))
问题在于,目前没有加载元素,而_driver.PageSource也不包含元素.
我如何等待装载物品?请不要建议Thread.Sleep()
这将等待页面加载/ jquery.ajax(如果存在)和$http调用,以及任何伴随的摘要/渲染循环,将其放在效用函数中并等待.
/* C# Example var pageLoadWait = new WebDriverWait(WebDriver,TimeSpan.FromSeconds(timeout)); pageLoadWait.Until<bool>( (driver) => { return (bool)JS.ExecuteScript( @"*/ try { if (document.readyState !== 'complete') { return false; // Page not loaded yet } if (window.jQuery) { if (window.jQuery.active) { return false; } else if (window.jQuery.ajax && window.jQuery.ajax.active) { return false; } } if (window.angular) { if (!window.qa) { // Used to track the render cycle finish after loading is complete window.qa = { doneRendering: false }; } // Get the angular injector for this app (change element if necessary) var injector = window.angular.element('body').injector(); // Store providers to use for these checks var $rootScope = injector.get('$rootScope'); var $http = injector.get('$http'); var $timeout = injector.get('$timeout'); // Check if digest if ($rootScope.$$phase === '$apply' || $rootScope.$$phase === '$digest' || $http.pendingRequests.length !== 0) { window.qa.doneRendering = false; return false; // Angular digesting or loading data } if (!window.qa.doneRendering) { // Set timeout to mark angular rendering as finished $timeout(function() { window.qa.doneRendering = true; },0); return false; } } return true; } catch (ex) { return false; } /*"); });*/