Yul*_*lia 6 javascript automated-tests web-testing e2e-testing testcafe
我正在尝试编写一个测试下载作品,该作品需要检查xhr响应的状态是否为READY。我使用诺言在TestCafe中创建了一个客户端函数,但是在递归的情况下失败了。
我应该如何修复我的代码来处理这种情况?
PS为新手问题道歉,我刚刚开始自动化测试之旅。
fixture`Download report works`
test
.requestHooks(logger)//connected a request hook, will wait for logger request
('I should be able to download PDF report from header of the page', async t => {
//recursively check if response status is READY, and then go to assertions
const waitForDownloadResponseStatus = ClientFunction((log) => {
return new Promise((resolve,rejects)=>{
const waitForStatus=()=>{
const arrayFromResponse = JSON.parse(log.response.body);
const responseStatus = arrayFromResponse.status;
if (responseStatus == 'READY')
{
resolve(responseStatus);
}
else {
waitForStatus();
}
}
waitForStatus();
})
});
//page objects
const reportTableRaw = Selector('div.contentcontainer').find('a').withText('April 2019').nth(0);
const downloadPdfButton = Selector('a.sr-button.sr-methodbutton.btn-export').withText('PDF');
//actions.
await t
.navigateTo(url)
.useRole(admin)
.click(reportTableRaw)//went to customise your report layout
.click(downloadPdfButton)
.expect(logger.contains(record => record.response.statusCode === 200))
.ok();//checked if there is something in logger
const logResponse = logger.requests[0];
// const arrayFromResponse = JSON.parse(logResponse.response.body);
// const responseStatus = arrayFromResponse.status;
console.log(logger.requests);
await waitForDownloadResponseStatus(logResponse).then((resp)=>{
console.log(resp);
t.expect(resp).eql('READY');
});
});
Run Code Online (Sandbox Code Playgroud)
当您将对象作为参数或依赖项传递给客户端函数时,它将收到所传递对象的副本。因此,它将无法检测到外部代码所做的任何更改。在这种特殊情况下,该waitForStatus函数将无法达到终止条件,因为它无法检测到log外部请求挂钩对对象所做的更改。这意味着此功能将无限期运行,直到它耗尽所有可用的堆栈内存。之后,它将失败并显示堆栈溢出错误。
为避免这种情况,READY如果更改contains函数的谓词参数,则可以检查响应的状态。看下面的代码:
.expect(logger.contains(record => record.response.statusCode === 200 &&
JSON.parse(record.response.body).status === 'READY'))
.ok({ timeout: 5000 });
Run Code Online (Sandbox Code Playgroud)
另外,您可以使用该timeout选项。在测试失败之前,断言可以通过的时间(以毫秒为单位)。