From 34c6ec4c071a8a1470684fd32dd88d4e7ca584f4 Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Mon, 1 Feb 2021 14:38:53 -0700 Subject: [PATCH] feat: add globalSetup for testing --- package.json | 3 ++- test/globalSetup.ts | 25 +++++++++++++++++++++++++ test/goHome.test.ts | 36 ++++++++++++++++++++---------------- 3 files changed, 47 insertions(+), 17 deletions(-) create mode 100644 test/globalSetup.ts diff --git a/package.json b/package.json index e739a5f9c..28ff7f668 100644 --- a/package.json +++ b/package.json @@ -146,6 +146,7 @@ "modulePathIgnorePatterns": [ "/release" ], - "testTimeout": 30000 + "testTimeout": 30000, + "globalSetup": "/test/globalSetup.ts" } } diff --git a/test/globalSetup.ts b/test/globalSetup.ts new file mode 100644 index 000000000..4904c0c3d --- /dev/null +++ b/test/globalSetup.ts @@ -0,0 +1,25 @@ +// This setup runs before our e2e tests +// so that it authenticates us into code-server +// ensuring that we're logged in before we run any tests +import { chromium, Page, Browser, BrowserContext } from "playwright" + +module.exports = async () => { + const browser: Browser = await chromium.launch() + const context: BrowserContext = await browser.newContext() + const page: Page = await context.newPage() + + await page.goto(process.env.CODE_SERVER_ADDRESS || "http://localhost:8080", { waitUntil: "domcontentloaded" }) + // Type in password + await page.fill(".password", process.env.PASSWORD || "password") + // Click the submit button and login + await page.click(".submit") + + // Save storage state and store as an env variable + // More info: https://playwright.dev/docs/auth?_highlight=authe#reuse-authentication-state + const storage = await context.storageState() + process.env.STORAGE = JSON.stringify(storage) + + await page.close() + await browser.close() + await context.close() +} diff --git a/test/goHome.test.ts b/test/goHome.test.ts index b7993dd8d..3618254bb 100644 --- a/test/goHome.test.ts +++ b/test/goHome.test.ts @@ -7,10 +7,15 @@ describe("login", () => { beforeAll(async () => { browser = await chromium.launch() - context = await browser.newContext() + // Create a new context with the saved storage state + const storageState = JSON.parse(process.env.STORAGE || "") + context = await browser.newContext({ storageState }) }) afterAll(async () => { + // Remove password from local storage + await context.clearCookies() + await browser.close() await context.close() }) @@ -19,12 +24,6 @@ describe("login", () => { page = await context.newPage() }) - afterEach(async () => { - await page.close() - // Remove password from local storage - await context.clearCookies() - }) - it("should see a 'Go Home' button in the Application Menu that goes to coder.com", async () => { const GO_HOME_URL = `${process.env.CODE_SERVER_ADDRESS}/healthz` let requestedGoHomeUrl = false @@ -35,15 +34,13 @@ describe("login", () => { // only that it was made if (request.url() === GO_HOME_URL) { requestedGoHomeUrl = true + console.log("woooo =>>>", requestedGoHomeUrl) } }) - // waitUntil: "networkidle" + + // waitUntil: "domcontentloaded" // In case the page takes a long time to load - await page.goto(process.env.CODE_SERVER_ADDRESS || "http://localhost:8080", { waitUntil: "networkidle" }) - // Type in password - await page.fill(".password", process.env.PASSWORD || "password") - // Click the submit button and login - await page.click(".submit") + await page.goto(process.env.CODE_SERVER_ADDRESS || "http://localhost:8080", { waitUntil: "domcontentloaded" }) // Click the Application menu await page.click(".menubar-menu-button[title='Application Menu']") // See the Go Home button @@ -56,10 +53,17 @@ describe("login", () => { // If there are unsaved changes it will show a dialog // asking if you're sure you want to leave - page.on("dialog", (dialog) => dialog.accept()) + await page.on("dialog", (dialog) => dialog.accept()) - // We make sure to wait on a request to the GO_HOME_URL - await page.waitForRequest(GO_HOME_URL) + // If it takes longer than 3 seconds to navigate, something is wrong + await page.waitForRequest(GO_HOME_URL, { timeout: 10000 }) expect(requestedGoHomeUrl).toBeTruthy() + + // // Make sure the response for GO_HOME_URL was successful + // const response = await page.waitForResponse( + // (response) => response.url() === GO_HOME_URL && response.status() === 200, + // ) + // We make sure a request was made to the GO_HOME_URL + // expect(response.ok()).toBeTruthy() }) })