speed up e2e tests by reusing cookies (#976)

This commit is contained in:
Alex Johansson 2021-10-17 02:11:37 +02:00 committed by GitHub
parent f08a2271fe
commit c146231b31
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 15 deletions

View file

@ -1,8 +1,8 @@
const opts = { const opts = {
// launch headless on CI, in browser locally // launch headless on CI, in browser locally
headless: !!process.env.CI || !!process.env.PLAYWRIGHT_HEADLESS, headless: !!process.env.CI || !!process.env.PLAYWRIGHT_HEADLESS,
executablePath: process.env.PLAYWRIGHT_CHROME_EXECUTABLE_PATH,
collectCoverage: !!process.env.PLAYWRIGHT_HEADLESS, collectCoverage: !!process.env.PLAYWRIGHT_HEADLESS,
executablePath: process.env.PLAYWRIGHT_CHROME_EXECUTABLE_PATH,
}; };
console.log("⚙️ Playwright options:", opts); console.log("⚙️ Playwright options:", opts);

View file

@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/ban-types */ /* eslint-disable @typescript-eslint/ban-types */
import { provider, Provider } from "kont"; import { provider, Provider } from "kont";
import { Page } from "playwright"; import { Page, Cookie } from "playwright";
/** /**
* Context data that Login provder needs. * Context data that Login provder needs.
@ -21,6 +21,7 @@ export type Contributes = {
page: Page; page: Page;
}; };
const cookieCache = new Map<string, Cookie[]>();
/** /**
* Creates a new context / "incognito tab" and logs in the specified user * Creates a new context / "incognito tab" and logs in the specified user
*/ */
@ -40,7 +41,10 @@ export function loginProvider(opts: {
.before(async () => { .before(async () => {
const context = await browser.newContext(); const context = await browser.newContext();
const page = await context.newPage(); const page = await context.newPage();
const cachedCookies = cookieCache.get(opts.user);
if (cachedCookies) {
await context.addCookies(cachedCookies);
} else {
await page.goto("http://localhost:3000/event-types"); await page.goto("http://localhost:3000/event-types");
// Click input[name="email"] // Click input[name="email"]
await page.click('input[name="email"]'); await page.click('input[name="email"]');
@ -54,6 +58,9 @@ export function loginProvider(opts: {
await page.press('input[name="password"]', "Enter"); await page.press('input[name="password"]', "Enter");
await page.waitForSelector("[data-testid=event-types]"); await page.waitForSelector("[data-testid=event-types]");
const cookies = await context.cookies();
cookieCache.set(opts.user, cookies);
}
if (opts.path) { if (opts.path) {
await page.goto(`http://localhost:3000${opts.path}`); await page.goto(`http://localhost:3000${opts.path}`);