clear([options])
清除文本框和输入字段(input
、textarea
或 contenteditable
元素)中的任何现有值。
参数 | 类型 | 默认值 | 描述 |
---|---|---|---|
options | 对象 | null | |
options.force | 布尔值 | false | 设置为 true 将绕过可操作性检查(visible , stable , enabled )。 |
options.noWaitAfter | 布尔值 | false | 如果设置为 true 且执行此操作导致发生导航,则不会等待导航完成。 |
options.timeout | 数字 | 30000 | 最大超时时间(毫秒)。传入 0 禁用超时。默认值会被 BrowserContext 或 Page 上的 setDefaultTimeout 选项覆盖。 |
返回值
类型 | 描述 |
---|---|
Promise<void> | 当 clear 操作完成时fulfilled的 Promise。 |
示例
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();
}