AngularJS&量角器 - 使http请求持续更长时间

时间:2015-06-30 07:18:46

标签: javascript angularjs protractor angularjs-e2e end-to-end

我正在为我的angularjs应用程序进行测试,当页面加载时,会进行一些http调用。当进行任何呼叫时,出现加载圈并且当收到响应时,加载圈被隐藏。

如何让loding圈可见10秒?

2 个答案:

答案 0 :(得分:2)

您可以拦截http请求并延迟它们:

网络-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);
    })
};

<强>用法

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() {

});

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

答案 1 :(得分:0)

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