我有一个场景,其中API接收多个响应(一次一个)并在UI上呈现。API继续轮询DB,直到它接收到所有的响应。我需要我的脚本等待,直到API响应变成"完成"。我尝试了下面的代码,但它没有等到状态完成。
const response = await page.waitForResponse(response => response.url().includes('https://services/url') && response.status() === 200);
console.log('RESPONSE ' + (await response.body()));
下面是记录的响应
{
"transactionDetail": {
"transactionID":"866f357f-7541-4ff2-b879-61ca284513a7",
"transactionTimestamp":"2021-08-02T10:48:50.372207",
"inLanguage":"en-US",
"serviceVersion":"1"
},
"resultSummary":{
"inputCount":1,
"successCount":1,
"failureCount":0},
"inquiryDetail": {
"kaseIterationId":"8a7b11547af8835d017b067ad06e04ba"
},
},
"response":"InProgress"
}
我怎么能让我的脚本等待,直到"响应"成为"Completed"而不是"inprogress &;">
您可以通过向page.waitForResponse
提供如下异步谓词来实现:
async function isFinished(response) {
return response.url().includes('https://services/url') && response.status() === 200 && (await response.json()).response === 'Completed'
}
const response = await page.waitForResponse(async (response) => await isFinished(response));