Skip to content

Commit

Permalink
Catch if an element is not clickable (#141)
Browse files Browse the repository at this point in the history
* Catch if an element is not clickable

* rm old todo
  • Loading branch information
ebebbington authored Apr 29, 2024
1 parent 4208bb5 commit 185b471
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,19 @@ export class Element {
if (!options.button) options.button = "left";

// Scroll into view
await this.#page.evaluate(
`${this.#method}('${this.#selector}').scrollIntoView({
block: 'center',
inline: 'center',
behavior: 'instant'
})`,
);
try {
await this.#page.evaluate(
`${this.#method}('${this.#selector}').scrollIntoView({
block: 'center',
inline: 'center',
behavior: 'instant'
})`,
);
} catch (_e) {
await this.#page.client.close(
`The given element ("${this.#selector}") is no longer present in the DOM`,
);
}

// Get details we need for dispatching input events on the element
const result = await this.#protocol.send<
Expand All @@ -279,6 +285,12 @@ export class Element {
null,
ProtocolTypes.Page.GetLayoutMetricsResponse
>("Page.getLayoutMetrics");
if (!result || !result.quads.length) {
await this.#page.client.close(
`Node is either not clickable or not an HTMLElement`,
);
}

// Ignoring because cssLayoutMetrics is present on chrome, but not firefox
// deno-lint-ignore ban-ts-comment
// @ts-ignore
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/element_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,32 @@ const serverAdd = `http://${
for (const browserItem of browserList) {
Deno.test(browserItem.name, async (t) => {
await t.step("click()", async (t) => {
await t.step(
"It should fail if the element is no longer present in the DOM",
async () => {
const { page } = await buildFor(browserItem.name, {
remote,
});
await page.location("https://drash.land");
// Need to make the element either not clickable or not a HTMLElement
const selector = 'a[href="https://discord.gg/RFsCSaHRWK"]';
const elem = await page.querySelector(
selector,
);
await page.location("https://google.com");
let errMsg = "";
try {
await elem.click();
} catch (e) {
errMsg = e.message;
}
assertEquals(
errMsg,
`The given element ("${selector}") is no longer present in the DOM`,
);
},
);

await t.step(
"It should allow clicking of elements and update location",
async () => {
Expand Down

0 comments on commit 185b471

Please sign in to comment.