简体   繁体   中英

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. 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 ?

You can intercept the http requests and delay them:

network-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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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