Skip to content

Instantly share code, notes, and snippets.

@pblzd
Created December 19, 2022 11:05
Show Gist options
  • Save pblzd/f005a4ee1d0039b360862c76b6c0b12d to your computer and use it in GitHub Desktop.
Save pblzd/f005a4ee1d0039b360862c76b6c0b12d to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<style>
.hide {
position: absolute;
top: -100px;
left: -100px;
}
</style>
</head>
<body>
<p><a href="#1">item</a></p>
</body>
</html>
<!-- Serve this file on http://localhost:8080/ to run the tests -->
import * as assert from "https://deno.land/std@0.158.0/testing/asserts.ts";
import { Client } from "https://deno.land/x/sinco@v4.1.0/src/client.ts";
const url = new URL("http://localhost:8080/test-sinco-140.html");
const createClient = () => Client.create(
[
"/usr/bin/chromium",
"--remote-debugging-port=9292",
"--headless",
"--disable-gpu",
"--no-sandbox",
"--disable-dev-shm-usage", // fix issues with lack of memory inside containers
"--window-size=1440,1000",
],
{
hostname: "localhost",
port: 9292,
},
"chrome",
undefined,
);
Deno.test({
name: "sinco",
fn: async (t) => {
await t.step({
name: "can click elem",
fn: async () => {
const { browser, page } = await createClient();
try {
await page.location(url.href);
const elem = await page.querySelector("a");
await elem.click();
const newUrl = await page.location();
assert.assertEquals(newUrl, url + "#1");
} finally {
// Always close all resources.
await browser.close();
}
},
});
await t.step({
name: "raises appropriate error if trying to click an element that was removed",
fn: async () => {
const { browser, page } = await createClient();
try {
await page.location(url.href);
const elem = await page.querySelector("a");
await page.evaluate(() => document.querySelector("p").remove());
let error;
try {
await elem.click();
} catch(exc) {
error = exc;
}
assert.assertIsError(error);
console.error(error);
assert.assertNotMatch(error.message, /scrollIntoView/);
} finally {
// Always close all resources.
await browser.close();
}
},
});
await t.step({
name: "raises appropriate error if trying to click an element that's not visible anymore",
fn: async () => {
const { browser, page } = await createClient();
try {
await page.location(url);
const elem = await page.querySelector("a");
await page.evaluate(() => document.querySelector("p").classList.add("hide"));
let error;
try {
await elem.click();
} catch(exc) {
error = exc;
}
assert.assertIsError(error);
console.error(error);
assert.assertNotMatch(error.message, /quad/);
} finally {
// Always close all resources.
await browser.close();
}
},
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment