Testing AngularJS with Selenium

后端 未结 12 722
梦毁少年i
梦毁少年i 2020-11-27 04:56

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

12条回答
  •  -上瘾入骨i
    2020-11-27 05:39

    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(
                    (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;
    }
    /*");
    });*/
    

提交回复
热议问题