例如,假设我们有一个按钮,其id为"testButton",API的URL为"http://testapi.com",如下所示:
const button = await page.$('#testButton');
await button.click();
await page.waitForResponse('http://testapi.com');
// 如果API没有响应,这里会抛出错误
例如,假设我们有一个按钮,其id为"testButton",API的URL为"http://testapi.com",如下所示:
await page.route('http://testapi.com', route => {
route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ success: true }),
});
});
const button = await page.$('#testButton');
await button.click();
// 等待API响应
const response = await page.waitForResponse('http://testapi.com');
const responseBody = await response.json();
// 检查响应内容是否符合预期
if (responseBody.success !== true) {
throw new Error('API响应结果不符合预期');
}