feat: add globalSetup for testing
This commit is contained in:
parent
3033c8f9a2
commit
34c6ec4c07
@ -146,6 +146,7 @@
|
|||||||
"modulePathIgnorePatterns": [
|
"modulePathIgnorePatterns": [
|
||||||
"<rootDir>/release"
|
"<rootDir>/release"
|
||||||
],
|
],
|
||||||
"testTimeout": 30000
|
"testTimeout": 30000,
|
||||||
|
"globalSetup": "<rootDir>/test/globalSetup.ts"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
25
test/globalSetup.ts
Normal file
25
test/globalSetup.ts
Normal 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()
|
||||||
|
}
|
@ -7,10 +7,15 @@ describe("login", () => {
|
|||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
browser = await chromium.launch()
|
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 () => {
|
afterAll(async () => {
|
||||||
|
// Remove password from local storage
|
||||||
|
await context.clearCookies()
|
||||||
|
|
||||||
await browser.close()
|
await browser.close()
|
||||||
await context.close()
|
await context.close()
|
||||||
})
|
})
|
||||||
@ -19,12 +24,6 @@ describe("login", () => {
|
|||||||
page = await context.newPage()
|
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 () => {
|
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`
|
const GO_HOME_URL = `${process.env.CODE_SERVER_ADDRESS}/healthz`
|
||||||
let requestedGoHomeUrl = false
|
let requestedGoHomeUrl = false
|
||||||
@ -35,15 +34,13 @@ describe("login", () => {
|
|||||||
// only that it was made
|
// only that it was made
|
||||||
if (request.url() === GO_HOME_URL) {
|
if (request.url() === GO_HOME_URL) {
|
||||||
requestedGoHomeUrl = true
|
requestedGoHomeUrl = true
|
||||||
|
console.log("woooo =>>>", requestedGoHomeUrl)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
// waitUntil: "networkidle"
|
|
||||||
|
// waitUntil: "domcontentloaded"
|
||||||
// In case the page takes a long time to load
|
// In case the page takes a long time to load
|
||||||
await page.goto(process.env.CODE_SERVER_ADDRESS || "http://localhost:8080", { waitUntil: "networkidle" })
|
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")
|
|
||||||
// Click the Application menu
|
// Click the Application menu
|
||||||
await page.click(".menubar-menu-button[title='Application Menu']")
|
await page.click(".menubar-menu-button[title='Application Menu']")
|
||||||
// See the Go Home button
|
// See the Go Home button
|
||||||
@ -56,10 +53,17 @@ describe("login", () => {
|
|||||||
|
|
||||||
// If there are unsaved changes it will show a dialog
|
// If there are unsaved changes it will show a dialog
|
||||||
// asking if you're sure you want to leave
|
// 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
|
// If it takes longer than 3 seconds to navigate, something is wrong
|
||||||
await page.waitForRequest(GO_HOME_URL)
|
await page.waitForRequest(GO_HOME_URL, { timeout: 10000 })
|
||||||
expect(requestedGoHomeUrl).toBeTruthy()
|
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()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user