Skip to content

Instantly share code, notes, and snippets.

@emilhein
Last active June 8, 2022 08:00
Show Gist options
  • Save emilhein/868095995924c7a84dbf21510e6323de to your computer and use it in GitHub Desktop.
Save emilhein/868095995924c7a84dbf21510e6323de to your computer and use it in GitHub Desktop.
/**
* @jest-environment jsdom
*/
import app from "../server/app";
import puppeteer from "puppeteer";
import { opts } from "./helpers/util";
const PORT = 3032;
jest.setTimeout(60000);
let browser = null;
let instance = null
describe("Test container", function () {
beforeAll(async () => {
instance = app.listen(PORT);
browser = await puppeteer.launch(opts);
});
afterAll(async () => {
await browser.close();
instance.close(() => { })
});
describe("E2E test", function () {
it("Output script is being loaded and setting some_global_variable to 4",
async function () {
// Arrange
const page = await browser.newPage();
let pageToLoad = `http://localhost:${PORT}/yourpage.html`
// Act
await page.goto(pageToLoad, { waitUntil: "networkidle0" });
await page.waitForTimeout(2000);
await scroll(page);
await page.waitForTimeout(5000);
let pageContextVariable = await page.evaluate(() => globalThis.some_global_variable);
// Assert
expect(pageContextVariable).toBe(4);
await page.close();
});
});
})
async function scroll(page) {
await page.evaluate(() => {
return new Promise((resolve, reject) => {
var totalHeight = parseInt(0);
var timer = setInterval(() => {
let distance = 500
var scrollHeight = document.body.scrollHeight;
window.scrollBy(parseInt(0), distance);
totalHeight += distance;
if (totalHeight >= scrollHeight) {
clearInterval(timer);
resolve();
}
}, 200);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment