emulateMedia([options])
此方法通过 media
参数更改 CSS @media
类型,和/或使用 colorScheme
参数更改 `'prefers-colors-scheme'` 媒体特性。
参数 | 类型 | 默认值 | 描述 |
---|---|---|---|
options | 对象 | null | |
options.colorScheme | 字符串 | '' | 模拟 `'prefers-colors-scheme'` 媒体特性,支持的值为 `'light'`、`'dark'` 和 `'no-preference'`。 |
options.media | 字符串 | '' | 更改页面的 CSS 媒体类型。唯一允许的值为 `'screen'` 和 `'print'`。 |
options.reducedMotion | 字符串 | '' | 模拟 `'prefers-reduced-motion'` 媒体特性,支持的值为 `'reduce'`、`'no-preference'`。 |
返回值
类型 | 描述 |
---|---|
Promise<void> | 媒体模拟完成后解析的 Promise。 |
示例
import { browser } from 'k6/browser';
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');
console.log(await page.evaluate(() => matchMedia('screen').matches)); // true
}