feat: add globalSetup for testing

This commit is contained in:
Joe Previte 2021-02-01 14:38:53 -07:00
parent 3033c8f9a2
commit 34c6ec4c07
No known key found for this signature in database
GPG Key ID: 2C91590C6B742C24
3 changed files with 47 additions and 17 deletions

View File

@ -146,6 +146,7 @@
"modulePathIgnorePatterns": [
"<rootDir>/release"
],
"testTimeout": 30000
"testTimeout": 30000,
"globalSetup": "<rootDir>/test/globalSetup.ts"
}
}

25
test/globalSetup.ts Normal file
View File

@ -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()
}

View File

@ -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()
})
})