waitForFunction(pageFunction, arg[, options])
当 pageFunction
返回一个 truthy 值时返回。
参数 | 类型 | 默认值 | 描述 |
---|---|---|---|
pageFunction | function | 在页面上下文中执行的函数。 | |
arg | string | '' | 传递给 pageFunction 的可选参数 |
options | object | null | |
options.polling | number 或 raf | raf | 如果 polling 是 'raf' ,则 pageFunction 会在 requestAnimationFrame 回调中不断执行。如果 polling 是一个数字,则它被视为函数执行的时间间隔(毫秒)。 |
options.timeout | number | 30000 | 最大时间(毫秒)。传入 0 可禁用超时。默认值会被 BrowserContext 或 Page 上的 setDefaultTimeout 选项覆盖。 |
返回值
类型 | 描述 |
---|---|
Promise< JSHandle> | 与页面关联的 JSHandle 实例。 |
示例
import { browser } from 'k6/browser';
import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js';
export const options = {
scenarios: {
browser: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
};
export default async function () {
const page = await browser.newPage();
try {
await page.evaluate(() => {
setTimeout(() => {
const el = document.createElement('h1');
el.innerHTML = 'Hello';
document.body.appendChild(el);
}, 1000);
});
const ok = await page.waitForFunction("document.querySelector('h1')", {
polling: 'mutation',
timeout: 2000,
});
await check(ok, {
'waitForFunction successfully resolved': async (ok) =>
await ok.innerHTML() == 'Hello'
});
} finally {
await page.close();
}
}