export async function setText(
xpath: string,
text: string,
simulateTyping: boolean,
waitTime = 200,
typeSpeed = 0,
): Promise<HTMLInputElement> {
const element = getInput(xpath)
if (element) {
await setElementText(element, text, simulateTyping, waitTime, typeSpeed)
}
return element
}
async function setElementText(
element: HTMLInputElement,
text: string,
simulateTyping: boolean,
waitTime = 500,
typeSpeed: number,
): Promise<void> {
element.focus()
element.select()
document.execCommand("delete", false)
element.focus()
if (simulateTyping) {
for (const char of text) {
await typingTimeout(typeSpeed)
//document.execCommand("insertText", false, char)
console.log(char)
}
} else {
if (!document.execCommand("insertText", false, text)) {
// Fallback for Firefox: just replace the value
element.value = text
}
}
element.click()
await randomTimeout(waitTime)
}