From c146231b311582d62cf8bd21eff17f0c10e70d52 Mon Sep 17 00:00:00 2001 From: Alex Johansson Date: Sun, 17 Oct 2021 02:11:37 +0200 Subject: [PATCH] speed up e2e tests by reusing cookies (#976) --- jest.playwright.config.js | 2 +- playwright/lib/loginProvider.ts | 35 ++++++++++++++++++++------------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/jest.playwright.config.js b/jest.playwright.config.js index 9eee8e93ef..0962dba014 100644 --- a/jest.playwright.config.js +++ b/jest.playwright.config.js @@ -1,8 +1,8 @@ const opts = { // launch headless on CI, in browser locally headless: !!process.env.CI || !!process.env.PLAYWRIGHT_HEADLESS, - executablePath: process.env.PLAYWRIGHT_CHROME_EXECUTABLE_PATH, collectCoverage: !!process.env.PLAYWRIGHT_HEADLESS, + executablePath: process.env.PLAYWRIGHT_CHROME_EXECUTABLE_PATH, }; console.log("⚙️ Playwright options:", opts); diff --git a/playwright/lib/loginProvider.ts b/playwright/lib/loginProvider.ts index 31164d4ff0..63a5c5b269 100644 --- a/playwright/lib/loginProvider.ts +++ b/playwright/lib/loginProvider.ts @@ -1,6 +1,6 @@ /* eslint-disable @typescript-eslint/ban-types */ import { provider, Provider } from "kont"; -import { Page } from "playwright"; +import { Page, Cookie } from "playwright"; /** * Context data that Login provder needs. @@ -21,6 +21,7 @@ export type Contributes = { page: Page; }; +const cookieCache = new Map(); /** * Creates a new context / "incognito tab" and logs in the specified user */ @@ -40,20 +41,26 @@ export function loginProvider(opts: { .before(async () => { const context = await browser.newContext(); 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"); + // Click input[name="email"] + await page.click('input[name="email"]'); + // Fill input[name="email"] + await page.fill('input[name="email"]', `${opts.user}@example.com`); + // Press Tab + await page.press('input[name="email"]', "Tab"); + // Fill input[name="password"] + await page.fill('input[name="password"]', opts.user); + // Press Enter + await page.press('input[name="password"]', "Enter"); - await page.goto("http://localhost:3000/event-types"); - // Click input[name="email"] - await page.click('input[name="email"]'); - // Fill input[name="email"] - await page.fill('input[name="email"]', `${opts.user}@example.com`); - // Press Tab - await page.press('input[name="email"]', "Tab"); - // Fill input[name="password"] - await page.fill('input[name="password"]', opts.user); - // Press 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) { await page.goto(`http://localhost:3000${opts.path}`);