在angularjs中有类似jQuery.active的东西吗?

tre*_*orc 4 javascript angularjs selenium-webdriver

我正在使用selenium来测试我的应用程序.我有很多使用$ resource或$ http的ajax调用.如果有一种方法可以以角度方式轮询任何活动的ajax请求,以便selenium可以等到这些请求完成后,那就太好了.

我想我可以在页面上放置一个元素(用于查找selenium)并将其连接到一些成功设置的标志,但这可能会变得非常混乱.

使用这里描述的jQuery时,有一种非常有效的方法可以做到这一点.

或者硒有没有办法做到这一点,我还没有找到?

在文档中找不到任何内容?有什么建议?谢谢.

编辑: Caleb Boyd的答案是正确的,并且是使用硒网络驱动程序检测角度ajax调用问题的一个很好的解决方案.这是我如何使用它的快速实现.我实际上使用了来自此链接的caleb代码的变体,其中包含ajax错误.然而,它基本上是一回事.谢谢Caleb.

将此脚本和元素添加到页面底部.在部署之前删除:

<html>
<head><!--My Angular Scripts--></head>
<body ng-app="MyApp">
<!--Your Html -->
<script>
            MyApp.config(function($httpProvider) {
                $httpProvider.interceptors.push('requestInterceptor');
            })
            .factory('requestInterceptor', function($q, $rootScope) {
                $rootScope.pendingRequests = 0;
                return {
                    'request': function(config) {
                        $rootScope.pendingRequests++;
                        return config || $q.when(config);
                    },
                    'requestError': function(rejection) {
                        $rootScope.pendingRequests--;
                        return $q.reject(rejection);
                    },
                    'response': function(response) {
                        $rootScope.pendingRequests--;
                        return response || $q.when(response);
                    },
                    'responseError': function(rejection) {
                        $rootScope.pendingRequests--;
                        return $q.reject(rejection);
                    }
                };
            });
    </script>
    <span id="http-status">{{pendingRequests}}</span>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我使用NUnit作为我的测试框架.

[TestFixture]
public class MyTestClass
{
  [Setup}
  public void Setup()
  {
    _webDriver = new ChromeDriver(@"...path to chromedriver.exe")
    //any other properties you need
   }

   [TearDown]
   public void TearDown()
   {
     if(_webDriver == null)
        return;
     _webDriver.Quit();
     _webDriver.Dispose();
    }

    [Test]
    public void Test_my_page_functionality()
    {
      var pageBtn = _webDriver.FindElement(By.Id("my-btn-id"));
      pageBtn.Click();
      _webDriver.WaitForAjax();//see extension below
      //test whatever result you want after ajax request has come back
     }
}
Run Code Online (Sandbox Code Playgroud)

这是WaitForAjax扩展

public static class WebDriverExtensions
{
  public static void WaitForAjax(this IWebDriver webDriver)
        {
            while (true)
            {
                //Note: FindElement is another extension that uses a timer to look for an element
                //It is NOT the one that selenium uses - the selenium extension throws exceptions on a null element
                var ajaxIsComplete = webDriver.FindElement(By.Id("http-status"), 5);
                if (ajaxIsComplete != null && ajaxIsComplete.Text.Equals("0"))
                {
                    //optional wait for angularjs digest or compile if data is returned in ajax call
                    Thread.Sleep(1000);
                    break;
                }
                Thread.Sleep(100);
            }
        }
}
Run Code Online (Sandbox Code Playgroud)

尝试通过在控制器方法的底部放置一个Thread.Sleep(5000)来测试WaitForAjax扩展.希望这有助于某人.再次感谢Caleb.

小智 10

是的我相信你可以在角度上使用回调.我在使用Ruby的Test Automation的大型项目中使用过它.以下是电话.我们等待30秒,直到挂起的Requests.length变为0.字符串"0",因为返回值始终是字符串.

Watir::Wait.until(30) {
@browser.execute_script("return angular.element(document.body).injector().get(\'$http\').pendingRequests.length;") == "0"
}
Run Code Online (Sandbox Code Playgroud)


cal*_*oyd 5

对此的一个解决方案是利用拦截器.也许是这样的:

angular.module('myApp',[])
.value('httpStatus',{arc:0})
.factory('activeHttpIntercepotrs',function(httpStatus,$q,$rootScope){
    //link up $rootScope and httpStatus
    $rootScope.httpStatus = httpStatus;
    return {
        'request': function(config){
            httpStatus.arc++; return config || $q.when(config);
        },
        'response': function(config){
            httpStatus.arc--; return config || $q.when(config);
        }
    }

})
.config(function($httpProvider){
    $httpProvider.interceptors.push('activeHttpIntercepotrs');
});
Run Code Online (Sandbox Code Playgroud)

并在你的DOM的某个地方...类似于:

<span class="dontDisplayMe">{{httpStatus.arc}}</span>
Run Code Online (Sandbox Code Playgroud)

我没有使用网络驱动程序,以便你做什么'民意调查',我不确定.但是你几乎可以保证,由于发生的摘要,DOM将保持最新状态$http