-
Notifications
You must be signed in to change notification settings - Fork 291
/
test-util.ts
47 lines (44 loc) · 1.29 KB
/
test-util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* eslint-env jest */
import type { Page, Frame } from "puppeteer";
function waitForFrame(page: Page) {
let fulfill: (value: Page | Frame) => void;
const promise = new Promise<Page | Frame>((resolve) => {
fulfill = resolve;
});
function checkFrame() {
const frame = page.frames().find((f) => f.parentFrame() !== null);
if (frame) fulfill(frame);
else page.once(`frameattached`, checkFrame);
}
checkFrame();
return promise;
}
async function goToPage(
page: Page,
route: string,
isFrame: boolean,
cb: (arg0: { currentPage: Page | Frame }) => void,
) {
let currentPage: Page | Frame = page;
await page.goto(`http://localhost:${process.env.TEST_SERVER_PORT}/${route}`);
if (isFrame) {
currentPage = await waitForFrame(page);
}
cb({ currentPage });
}
export const setupPage = (
instanceType: string,
cb: (arg0: { currentPage: Page | Frame }) => void,
) => {
beforeEach(async () => {
if (instanceType === "Page") {
cb({ currentPage: page });
} else if (instanceType === "ShadowPage") {
await goToPage(page, "shadow.html", false, cb);
} else if (instanceType === "ShadowFrame") {
await goToPage(page, "shadowFrame.html", true, cb);
} else if (instanceType === "Frame") {
await goToPage(page, "frame.html", true, cb);
}
});
};