evaluate(pageFunction[, arg])
返回 pageFunction 调用的值。
参数 | 类型 | 默认值 | 描述 |
---|---|---|---|
pageFunction | 函数或字符串 | 将在页面上下文中评估的函数。这也可以是一个字符串。 | |
arg | 字符串 | '' | 要传递给 pageFunction 的可选参数 |
返回值
类型 | 描述 |
---|---|
Promise<any> | pageFunction 调用的值。 |
示例
import { browser } from 'k6/browser';
import { check } from 'k6/http';
export const options = {
scenarios: {
browser: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
};
export default async function () {
const page = await browser.newPage();
await page.goto('https://test.k6.io/browser.php');
const dimensions = await page.evaluate(() => {
const obj = {
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight,
deviceScaleFactor: window.devicePixelRatio,
};
return obj;
});
check(dimensions, {
width: (d) => d.width === 1280,
height: (d) => d.height === 720,
scale: (d) => d.deviceScaleFactor === 1,
});
}