菜单
开源软件

setOffline(offline)

切换 浏览器上下文 的连接状态(开/关)。

参数类型默认值说明
offlinebooleanfalse是否模拟 浏览器上下文 断开连接 (true) 或连接 (false)。

返回值

类型说明
Promise<void>一个 Promise,当连接状态设置完成后完成。

示例

JavaScript
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();
  }
}