playwright-fluent
Version:
Fluent API around playwright
42 lines (41 loc) • 1.77 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.clearText = exports.defaultClearTextOptions = void 0;
const utils_1 = require("../../../utils");
exports.defaultClearTextOptions = {
delay: 50,
};
async function clearText(page, options) {
if (!page) {
throw new Error(`Cannot clear text because no browser has been launched`);
}
const focusedElement = await page.evaluateHandle(() => window.document.activeElement);
if (focusedElement === null) {
throw new Error(`You must first click on an editable element before clearing text.`);
}
const handle = focusedElement.asElement();
if (handle === null) {
throw new Error(`You must first click on an editable element before clearing text.`);
}
const currentTagName = await handle.evaluate((node) => node.tagName);
const isContentEditable = await handle.evaluate((node) => node.isContentEditable);
if (currentTagName === 'BODY') {
throw new Error(`You must first click on an editable element before clearing text.`);
}
if (currentTagName === 'P' && !isContentEditable) {
throw new Error(`You must first click on an editable element before clearing text.`);
}
if (currentTagName === 'DIV' && !isContentEditable) {
throw new Error(`You must first click on an editable element before clearing text.`);
}
const tripleClickOptions = {
...utils_1.defaultWaitUntilOptions,
button: 'left',
delay: options.delay,
clickCount: 3,
};
await handle.click(tripleClickOptions);
await (0, utils_1.sleep)(options.delay * 5);
await (0, utils_1.toPage)(page).keyboard.press('Backspace', { delay: options.delay });
}
exports.clearText = clearText;
;