I'm trying to select text within a textarea
using Cypress, but running into some issues. The Cypress api doesn't have an out of the box way to do it that I can find, so I tried implementing it myself.
I found this stackoverflow answer about selecting text and decided to follow its example. What I've arrived at is this:
selectTextWithin = (selector: string) => {
cy.document().then((doc: Document) => {
cy.window().then((win: Window) => {
cy.get(selector).then((textElement: JQuery<HTMLElement>) => {
if (win.getSelection) {
const selection = win.getSelection();
const range = doc.createRange();
range.selectNodeContents(textElement.get(0));
selection.removeAllRanges();
selection.addRange(range);
} else {
throw new Error("Can't select text.")
}
})
})
})
}
It executes without error, but it doesn't appear to be selecting any text.