【问题标题】:How to pause the test script for 3 seconds before continue running it? Playwright如何在继续运行之前暂停测试脚本 3 秒?剧作家
【发布时间】:2021-12-16 17:45:27
【问题描述】:
我正在运行一个名为 create admin 的测试。测试会先创建admin,然后检查admin是否创建成功。
在脚本中,我有一段代码要等待 3 秒再继续,因为每当单击提交按钮时,网站需要花 3 秒时间来刷新管理表(用户信息列表)之后导航完成。有关更多信息,此刷新不是导航,因此我的“waitForNavigation()”不起作用。
因此,流程将是:'填写表单' > '点击提交按钮' > '等待导航' > '重新加载用户表(3s)。
如果我不等待3s刷新表,测试会抛出错误,因为在表中找不到注册用户(我有其他脚本可以找到用户)。
这是单击“保存按钮”时导航的样子:
之后,表格需要 3 秒刷新,它看起来像:
“创建”函数如下所示:
【问题讨论】:
标签:
javascript
testing
functional-testing
playwright
ava
【解决方案1】:
您可以将setTimeout 包装为Promise 并在异步函数中使用它:
const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
ms 的位置 - 您想等待的毫秒数延迟。
在您的代码中的用法:
...
await page.click('button :text-is("Save")');
await delay(3000); // <-- here we wait 3s
return username;
【解决方案2】:
我创建了一个名为“delayChecked()”的函数来将脚本延迟 3 秒。但它并没有像我预期的那样工作。
async function delayedCheck() {
// Refresh the table data required 3 seconds.
// Therefore, delay the scripts for 3 seconds.
let process_complete = false;
if (!process_complete) {
setTimeout(() => {
process_complete = true;
}, 3000);
}
}
export async function create(page: Page) {
await openUserTabOn(page);
const username = NAME_BASE + suffixify();
await page.click('text=New');
await page.fill('[placeholder="Name here..."]', 'testing123');
await page.fill('[placeholder="Username here..."]', username);
await page.fill('[placeholder="Password here..."]', 'xe123456');
await page.click('[role="listbox"]:has-text("admin")');
await page.click('[role="option"]:has-text("admin")');
await page.click('button :text-is("Save")');
await delayedCheck();
return username;
};
这可能是什么问题?
【解决方案3】:
使用setTimeout 来做到这一点。这是一个例子
function delayedCheck() {
const processComplete = true; // set the right boolean here
if ( processComplete ) {
// Do something
} else {
setTimeout(delayedCheck, 3000); // try again in 3 seconds
}
}
delayedCheck();