我正在運行一個名為 create admin 的測驗。測驗會先創建admin,然后檢查admin是否創建成功。
在腳本中,我有一部分代碼要等待 3 秒才能繼續,因為無論何時單擊提交按鈕,網站都需要在導航完成后用 3 秒時間重繪 管理表(用戶資訊串列) . 有關更多資訊,此重繪 不是導航,因此,我的“waitForNavigation()”不起作用。
因此,該程序將類似于:“填寫表單”>“單擊提交按鈕”>“等待導航”>“重新加載用戶表(3 秒)。
如果我不等待3s讓表重繪 ,測驗會拋出錯誤,因為在表中找不到注冊用戶(我有其他腳本可以找到用戶)。
這是單擊“保存按鈕”時導航的樣子:

之后,該表需要 3s 重繪 ,如下所示:

這就是“創建”函式的樣子:

uj5u.com熱心網友回復:
使用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();
uj5u.com熱心網友回復:
我創建了一個名為“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;
};
這可能是什么問題?
uj5u.com熱心網友回復:
您可以將其包裝setTimeout為Promise并在異步函式中使用它:
const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
where ms- 你想等待的毫秒延遲。
在您的代碼中的用法:
...
await page.click('button :text-is("Save")');
await delay(3000); // <-- here we wait 3s
return username;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/348009.html
標籤:javascript 测试 功能测试 剧作家 阿瓦
上一篇:打字稿/如何期待固定日期?
下一篇:如何在基于特性的測驗中提出特性
