setOffline(offline)
切换 浏览器上下文 的连接状态(开/关)。
参数 | 类型 | 默认值 | 说明 |
---|---|---|---|
offline | boolean | false | 是否模拟 浏览器上下文 断开连接 (true ) 或连接 (false )。 |
返回值
类型 | 说明 |
---|---|
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 context = await browser.newContext();
await context.setOffline(true);
const page = await context.newPage();
try {
// Will not be able to load the page
await page.goto('https://test.k6.io/browser.php');
} finally {
await page.close();
}
}