在我的 Protractor 测试中,我还想验证是否进行了正确的 ajax 调用。例如,如果我导航到 /chapters
我想确保有一个对 /chapters/documents?page=1
可以用 Protractor 做到这一点吗?
我找到了this发布了一些有趣的解决方案,但我仍然不知道如何使用它,以便在我的 it(...)
中我可以实际测试一些东西。例如。 browser.addMockModule
听起来很有希望,但同样,我应该在 beforeEach
中做什么以及如何验证任何内容。帮助将不胜感激!
最佳答案
您可以尝试如下操作。在您的测试中添加模拟模块:
browser.addMockModule('httpInterceptor', function () {
angular.module('httpInterceptor', []).factory('httpRequestInterceptor',function ($q) {
return {
request: function (request) {
window.handledRequests = window.handledRequests || [];
window.handledRequests.push({
url: request.url,
method: request.method,
params: request.params,
data: JSON.stringify(request.data)
});
return request || $q.when(request);
}
};
}).config(function ($httpProvider) {
$httpProvider.interceptors.push('httpInterceptor');
});
});
然后您可以在测试中编写断言,如下所示:
browser.controlFlow().execute(function () {
browser.executeScript(function () {
return window.handledRequests;
}).then(function (handledRequests) {
// assert content of handledRequests
});
});
关于javascript - Protractor:测试特定的ajax调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36410402/