使用Selenium测试AngularJS [英] Testing AngularJS with Selenium

查看:2795
本文介绍了使用Selenium测试AngularJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有SPA应用上堆栈ASP MVC + AngularJS,我想测试UI。
对于现在我想与PhantomJS和WebKit司机硒。

I have SPA application on on stack ASP MVC + AngularJS and i'd like to test UI. For a now i'm trying for Selenium with PhantomJS and WebKit drivers.

测试页 - 查看与单个元素 - 列表<立GT; 它通过动态角从服务器和绑定加载

Testing page - view with single element - the list of <li> which loads dynamically from server and bind by Angular.

<div id="items">
    <li>text</li>
    <li>text2</li>
</div>

我想通过测试

_driver.FindElements(By.TagName('li'))

问题是,在这一刻没有装载元素,_driver.PageSource不包含的元素了。

The problem is that at this moment there is no elements loaded, and _driver.PageSource doesn't contain elements too.

我如何可以等待加载的项目?请不要建议Thread.sleep代码()

How can i wait for items loaded? Please do not suggest Thread.Sleep()

推荐答案

这将等待页面加载/ jquery.ajax(如present)和$ HTTP调用,以及随行的消化/渲染循环,把它扔到效用函数,并等待了。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆