菜单
开源

clear([options])

清除文本框和输入字段(inputtextareacontenteditable 元素)中的任何现有值。

参数类型默认值描述
options对象null
options.force布尔值false设置为 true 将绕过可操作性检查(visible, stable, enabled)。
options.noWaitAfter布尔值false如果设置为 true 且执行此操作导致发生导航,则不会等待导航完成。
options.timeout数字30000最大超时时间(毫秒)。传入 0 禁用超时。默认值会被 BrowserContextPage 上的 setDefaultTimeout 选项覆盖。

返回值

类型描述
Promise<void>当 clear 操作完成时fulfilled的 Promise。

示例

JavaScript
import { browser } from 'k6/browser';
import { check } from "https://jslib.k6.io/k6-utils/1.5.0/index.js";

export const options = {
  scenarios: {
    ui: {
      executor: 'shared-iterations',
      options: {
        browser: {
          type: 'chromium',
        },
      },
    },
  },
};

export default async function () {
  const context = await browser.newContext();
  const page = await context.newPage();

  await page.goto("https://test.k6.io/my_messages.php", {
    waitUntil: 'networkidle',
  });

  // Fill an input element with some text that we will later clear
  const login = page.locator('input[name="login"]');
  await login.type('admin');

  // Check that the element has been filled with text
  await check(login, {
    'not empty': async lo => await lo.inputValue() != '',
  });

  // Now clear the text from the element
  await login.clear();

  // Check that the element is now empty
  await check(login, {
    empty: async lo => await lo.inputValue() == '',
  });

  await page.close();
}