From 7ea6d22b3eec86aaa510eade8be871e3d7aabf00 Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Tue, 13 Apr 2021 17:15:07 -0700 Subject: [PATCH] refactor: add login to config.js for e2e tests --- test/config.ts | 62 +++++++++++++++++++++++++++++++++--- test/e2e/globalSetup.test.ts | 13 +++++--- 2 files changed, 67 insertions(+), 8 deletions(-) diff --git a/test/config.ts b/test/config.ts index 60339b41d..3ab5dfd30 100644 --- a/test/config.ts +++ b/test/config.ts @@ -1,10 +1,64 @@ -import { ChromiumEnv, FirefoxEnv, WebKitEnv, test, setConfig, PlaywrightOptions } from "@playwright/test" +import { + ChromiumEnv, + FirefoxEnv, + WebKitEnv, + test, + setConfig, + PlaywrightOptions, + Config, + globalSetup, +} from "@playwright/test" +import * as crypto from "crypto" +import path from "path" +import { PASSWORD } from "./utils/constants" +import * as wtfnode from "./utils/wtfnode" -setConfig({ - testDir: "e2e", // Search for tests in this directory. - timeout: 30000, // Each test is given 30 seconds. +// Playwright doesn't like that ../src/node/util has an enum in it +// so I had to copy hash in separately +const hash = (str: string): string => { + return crypto.createHash("sha256").update(str).digest("hex") +} + +const cookieToStore = { + sameSite: "Lax" as const, + name: "key", + value: hash(PASSWORD), + domain: "localhost", + path: "/", + expires: -1, + httpOnly: false, + secure: false, +} + +globalSetup(async () => { + console.log("\nšŸšØ Running Global Setup for Jest End-to-End Tests") + console.log("šŸ‘‹ Please hang tight...") + + if (process.env.WTF_NODE) { + wtfnode.setup() + } + + const storage = { + cookies: [cookieToStore], + } + + // Save storage state and store as an env variable + // More info: https://playwright.dev/docs/auth?_highlight=authe#reuse-authentication-state + process.env.STORAGE = JSON.stringify(storage) + console.log("āœ… Global Setup for Jest End-to-End Tests is now complete.") }) +const config: Config = { + testDir: path.join(__dirname, "e2e"), // Search for tests in this directory. + timeout: 30000, // Each test is given 30 seconds. +} + +if (process.env.CI) { + config.retries = 2 // Retry failing tests 2 times +} + +setConfig(config) + const options: PlaywrightOptions = { headless: true, // Run tests in headless browsers. video: "retain-on-failure", diff --git a/test/e2e/globalSetup.test.ts b/test/e2e/globalSetup.test.ts index 28bb59b5a..d0eb8ccc7 100644 --- a/test/e2e/globalSetup.test.ts +++ b/test/e2e/globalSetup.test.ts @@ -6,11 +6,16 @@ import { CODE_SERVER_ADDRESS, STORAGE } from "../utils/constants" test.describe("globalSetup", () => { // Create a new context with the saved storage state // so we don't have to logged in - const storageState = JSON.parse(STORAGE) || {} - const options = { - contextOptions: { + const options: any = {} + + // TODO@jsjoeio + // Fix this once https://github.com/microsoft/playwright-test/issues/240 + // is fixed + if (STORAGE) { + const storageState = JSON.parse(STORAGE) || {} + options.contextOptions = { storageState, - }, + } } test("should keep us logged in using the storageState", options, async ({ page }) => { await page.goto(CODE_SERVER_ADDRESS, { waitUntil: "networkidle" })