Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript angularjs中是否有类似jQuery.active的东西?_Javascript_Angularjs_Selenium Webdriver - Fatal编程技术网

Javascript angularjs中是否有类似jQuery.active的东西?

Javascript angularjs中是否有类似jQuery.active的东西?,javascript,angularjs,selenium-webdriver,Javascript,Angularjs,Selenium Webdriver,我正在使用selenium测试我的应用程序。我有很多使用$resource或$http的ajax调用。如果有一种方法可以在angular中轮询任何活动的ajax请求,这样selenium就可以等到这些请求完成 我想我可以在页面上放置一个元素(供selenium查找)并将其连接到某个标志上,该标志设置为成功,但这可能会变得非常混乱 在使用jQuery时,有一种非常有效的方法可以做到这一点 或者硒有一种我没有找到的方法来实现这一点 在文档中找不到任何东西?有什么建议吗?谢谢 编辑: Caleb Bo

我正在使用selenium测试我的应用程序。我有很多使用$resource或$http的ajax调用。如果有一种方法可以在angular中轮询任何活动的ajax请求,这样selenium就可以等到这些请求完成

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

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

或者硒有一种我没有找到的方法来实现这一点

在文档中找不到任何东西?有什么建议吗?谢谢

编辑: Caleb Boyd的回答是正确的,是在使用selenium web驱动程序时检测角度ajax调用问题的一个很好的解决方案。下面是我如何使用它的快速实现。实际上,我使用了caleb代码的一个变体,其中包括ajax错误。然而,本质上是一样的。谢谢你,凯勒

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

<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>
这是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);
            }
        }
}
在控制器方法的底部放一个Thread.Sleep(5000),尝试测试WaitForAjax扩展。
希望这对别人有帮助。再次感谢Caleb。

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

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');
});
在你的DOM里的某个地方。。。比如:

<span class="dontDisplayMe">{{httpStatus.arc}}</span>
{{httpStatus.arc}

我还没有使用web驱动程序,所以我不确定您对“轮询”做了什么。但是您几乎可以保证DOM将保持最新,因为
$http

是的,我相信您可以在angular上使用回调。我在一个使用Ruby的大型自动化测试项目中使用了它。下面是电话。我们正在等待30秒,直到挂起的Requests.length变为0。字符串“0”,因为返回值始终为字符串

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

以下是我如何在Ruby应用程序中使用Capybara和Capybara Webkit实现这一点:

def wait_for_ajax
  Timeout.timeout(Capybara.default_max_wait_time) do
    loop until finished_all_ajax_requests?
  end
end

def finished_all_ajax_requests?
  pending_requests = page.evaluate_script('angular.element(document.body).injector().get("$http").pendingRequests.length')
  pending_requests && pending_requests.zero?
end

谢谢你,凯勒。我在上面添加了一些代码,以使用您的方法演示“端到端”类型的场景。
def wait_for_ajax
  Timeout.timeout(Capybara.default_max_wait_time) do
    loop until finished_all_ajax_requests?
  end
end

def finished_all_ajax_requests?
  pending_requests = page.evaluate_script('angular.element(document.body).injector().get("$http").pendingRequests.length')
  pending_requests && pending_requests.zero?
end