使用 Selenium 测试 AngularJS [英] Testing AngularJS with Selenium
问题描述
我在堆栈 ASP MVC + AngularJS 上有一个 SPA 应用程序,我想测试 UI.现在我正在尝试使用 PhantomJS 和 WebKit 驱动程序的 Selenium.
这是一个示例测试页面 - 使用单个元素查看.列表项 从服务器动态加载并受 Angular 限制.
<li>文本</li><li>text2</li>
我正在尝试通过测试,但此行中有错误:
_driver.FindElements(By.TagName('li'))
此时没有加载的元素,_driver.PageSource 不包含元素.
如何等待物品加载?请不要建议 Thread.Sleep()
这将等待页面加载/jquery.ajax(如果存在)和 $http 调用,以及任何伴随的摘要/渲染循环,将其放入实用程序函数中等一下.
/* C# 示例var pageLoadWait = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(timeout));pageLoadWait.Until((司机) =>{返回(布尔)JS.ExecuteScript(@"*/尝试 {如果(document.readyState !=='完成'){返回假;//页面尚未加载}如果(窗口.jQuery){如果(window.jQuery.active){返回假;} else if (window.jQuery.ajax && window.jQuery.ajax.active) {返回假;}}如果(窗口.角度){如果(!window.qa){//用于跟踪加载完成后的渲染周期完成情况窗口.qa = {doneRendering: 假};}//获取此应用程序的角度注入器(如有必要,更改元素)var injector = window.angular.element('body').injector();//存储用于这些检查的提供程序var $rootScope = injector.get('$rootScope');var $http = injector.get('$http');var $timeout = injector.get('$timeout');//检查是否摘要if ($rootScope.$$phase === '$apply' || $rootScope.$$phase === '$digest' || $http.pendingRequests.length !== 0) {window.qa.doneRendering = false;返回假;//Angular 消化或加载数据}如果(!window.qa.doneRendering){//设置超时以将角度渲染标记为完成$超时(功能(){window.qa.doneRendering = true;}, 0);返回假;}}返回真;}赶上(前){返回假;}/*");});*/
I have a SPA application on stack ASP MVC + AngularJS and I'd like to test the UI. For now I'm trying Selenium with PhantomJS and WebKit drivers.
This is a sample testing page - view with single element. The list items <li>
load dynamically from server and are bounded by Angular.
<div id="items">
<li>text</li>
<li>text2</li>
</div>
I'm trying to pass a test and there is an error in this line:
_driver.FindElements(By.TagName('li'))
At this point there are no loaded elements and _driver.PageSource doesn't contain elements.
How can I wait for the items to load? Please do not suggest Thread.Sleep()
This will wait for page loads / jquery.ajax (if present) and $http calls, and any accompanying digest/render cycle, throw it in a utility function and wait away.
/* 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;
}
/*");
});*/
这篇关于使用 Selenium 测试 AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!