简体   繁体   English

AngularJS和量角器-发出更长的http请求

[英]AngularJS & Protractor - Make a http request last longer

I'm working on tests for my angularjs app and when the page is loaded some http calls are made. 我正在为我的angularjs应用程序进行测试,并且在加载页面时进行了一些http调用。 When any call is made a loading circle appears and when a response is received the loading circle is hidden. 进行任何呼叫时,都会出现一个加载圆圈,而当收到响应时,该加载圆圈将被隐藏。

How can I make the loding circle visible for let's say 10 seconds ? 我如何在10秒钟内使可见的圆圈可见?

You can intercept the http requests and delay them: 您可以拦截http请求并延迟它们:

network-delay.js 网络delay.js

exports.module = function() {
    angular.module('networkDelayInterceptor', [])
    .config(function simulateNetworkLatency($httpProvider) {
        function httpDelay($timeout, $q) {
            var delayInMilliseconds = 1000;

            var responseOverride = function (reject) {
                return function (response) {
                    //Uncomment the lines below to filter out all the requests not needing delay
                    //if (response.config.url.indexOf('some-url-to-delay') === -1) {
                    //  return response;
                    //}

                    var deferred = $q.defer();
                    $timeout(
                        function() {
                            if (reject) {
                                deferred.reject(response);
                            } else {
                                deferred.resolve(response);
                            }
                        },
                        delayInMilliseconds,
                        false
                    );

                    return deferred.promise;
                };
            };

            return {
                response: responseOverride(false),
                responseError: responseOverride(true)
            };
        }

        $httpProvider.interceptors.push(httpDelay);
    })
};

Usage 用法

beforeAll(function() {
    var networkDelay = require('network-delay');

    // You can customize the module with a parameter for the url and the delay by adding them as a 3rd and 4th param, and modifying the module to use them
    browser.addMockModule('networkDelayInterceptor', networkDelay.module);
});

afterAll(function() {
    browser.removeMockModule('networkDelayInterceptor');
});

it('My-slowed-down-test', function() {

});

Source: http://www.bennadel.com/blog/2802-simulating-network-latency-in-angularjs-with-http-interceptors-and-timeout.htm 资料来源: http : //www.bennadel.com/blog/2802-simulating-network-latency-in-angularjs-with-http-interceptors-and-timeout.htm

您可以在javascript中使用setTimeout方法,或者在angular js中使用$ timeout函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM