TestCafe RequestLogger-如何在对每个请求对象进行断言之前等待所有响应返回 [英] TestCafe RequestLogger - How to wait for all responses to return before doing an assertion on each request object
问题描述
请参阅随附的屏幕截图.我有一个RequestLogger,它捕获对/api/dynamic_reporting/filters/*/find
端点的所有请求.
See the attached screenshot. I have a RequestLogger that captures all the requests to /api/dynamic_reporting/filters/*/find
endpoint.
问题是当我执行断言时,等待t.expect(dynamicReportingFindRequest.requests [1] .response.statusCode).eql(200,'Did not get 200 OK response'),我收到错误无法读取未定义的属性'statusCode'
,因为xhr仍在等待 requests [1],requests [2] ...
且尚未完成
The issue is that when I do an assertion await t.expect(dynamicReportingFindRequest.requests[1].response.statusCode).eql(200,'Did not get 200 OK response')
, I get an error Cannot read property 'statusCode' of undefined
because the xhr is still pending for requests[1], requests[2]...
and hasn't completed yet.
在对请求进行断言之前,我如何等待所有响应返回?
How do I wait for all responses to return before doing an assertion on the requests?
推荐答案
我会根据这是简单的node.js服务器:
Here is simple node.js server:
var http = require('http');
http.createServer(function (req, res) {
if (req.url.indexOf('test') > -1) {
setTimeout(() => {
res.writeHead(200, {'Content-Type': 'application/json'});
res.write('rest');
res.end();
}, 10000);
}
else {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(`<button onclick="fetch('http://localhost:8082/test'); setTimeout(() => { fetch('http://localhost:8082/test'); }, 100);setTimeout(() => { fetch('http://localhost:8082/test'); }, 200)">button</button>`);
res.end();
}
}).listen(8082);
单击按钮将发送3个请求.
Click on the button sends 3 requests.
测试代码如下:
import { RequestLogger } from 'testcafe';
const logger = RequestLogger(/test/);
fixture `Getting Started`
.page `http://localhost:8082`;
test.requestHooks(logger)('My first test', async t => {
await t.click('button');
while (logger.requests.find(r => !r.response || r.response.statusCode !== 200))
await t.wait(1000);
console.log(logger.requests.length);
console.log(logger.requests.filter(r => r.response.statusCode === 200).length);
});
这篇关于TestCafe RequestLogger-如何在对每个请求对象进行断言之前等待所有响应返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!