gpt4 book ai didi

javascript - AngularJS 和 Protractor - 使 http 请求持续更长时间

转载 作者:行者123 更新时间:2023-11-30 16:46:40 24 4
gpt4 key购买 nike

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

我怎样才能使定位圆可见 10 秒?

最佳答案

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

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

用法

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

关于javascript - AngularJS 和 Protractor - 使 http 请求持续更长时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31131949/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com