Stevik
Stevik

Reputation: 1152

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 ?

Upvotes: 1

Views: 472

Answers (2)

Delian Mitankin
Delian Mitankin

Reputation: 3691

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

Upvotes: 2

Dinesh
Dinesh

Reputation: 12

You can make use of setTimeout method in javascript or alternatively $timeout function in angular js.

Upvotes: 0

Related Questions