code-server/test/helpAbout.test.ts

93 lines
3.3 KiB
TypeScript
Raw Normal View History

2021-02-05 05:54:08 +08:00
import { chromium, Page, Browser, BrowserContext, Cookie } from "playwright"
import { hash } from "../src/node/util"
import { CODE_SERVER_ADDRESS, PASSWORD, STORAGE } from "./constants"
import { createCookieIfDoesntExist } from "./helpers"
2021-01-29 02:48:57 +08:00
describe("Open Help > About", () => {
2021-01-29 02:48:57 +08:00
let browser: Browser
let page: Page
let context: BrowserContext
2021-02-12 02:18:15 +08:00
beforeAll(async () => {
browser = await chromium.launch({ headless: false })
2021-02-02 05:38:53 +08:00
// Create a new context with the saved storage state
const storageState = JSON.parse(STORAGE) || {}
2021-02-05 05:54:08 +08:00
const cookieToStore = {
sameSite: "Lax" as const,
name: "key",
value: hash(PASSWORD),
2021-02-05 05:54:08 +08:00
domain: "localhost",
path: "/",
expires: -1,
httpOnly: false,
secure: false,
}
// For some odd reason, the login method used in globalSetup.ts doesn't always work
// I don't know if it's on playwright clearing our cookies by accident
// or if it's our cookies disappearing.
// This means we need an additional check to make sure we're logged in.
// We do this by manually adding the cookie to the browser environment
// if it's not there at the time the test starts
const cookies: Cookie[] = storageState.cookies || []
// If the cookie exists in cookies then
// this will return the cookies with no changes
// otherwise if it doesn't exist, it will create it
// hence the name maybeUpdatedCookies
//
// TODO(@jsjoeio)
// The playwright storage thing sometimes works and sometimes doesn't. We should investigate this further
// at some point.
// See discussion: https://github.com/cdr/code-server/pull/2648#discussion_r575434946
2021-02-05 05:54:08 +08:00
const maybeUpdatedCookies = createCookieIfDoesntExist(cookies, cookieToStore)
context = await browser.newContext({
storageState: { cookies: maybeUpdatedCookies },
recordVideo: { dir: "./test/videos/" },
})
2021-01-29 02:48:57 +08:00
})
2021-02-12 02:18:15 +08:00
afterAll(async () => {
2021-02-02 05:38:53 +08:00
// Remove password from local storage
await context.clearCookies()
await context.close()
await browser.close()
2021-01-29 02:48:57 +08:00
})
2021-02-12 02:18:15 +08:00
beforeEach(async () => {
2021-01-29 02:48:57 +08:00
page = await context.newPage()
})
it("should see a 'Help' then 'About' button in the Application Menu that opens a dialog", async () => {
// Sometimes a dialog shows up when you navigate
// asking if you're sure you want to leave
// so we listen if it comes, we accept it
// page.on("dialog", (dialog) => dialog.accept())
2021-02-02 05:38:53 +08:00
// waitUntil: "domcontentloaded"
// In case the page takes a long time to load
await page.goto(CODE_SERVER_ADDRESS, { waitUntil: "domcontentloaded" })
2021-03-06 02:58:17 +08:00
// Make sure the editor actually loaded
expect(await page.isVisible("div.monaco-workbench"))
// Click the Application menu
2021-03-06 02:58:17 +08:00
await page.click("[aria-label='Application Menu']")
// See the Help button
const helpButton = "a.action-menu-item span[aria-label='Help']"
expect(await page.isVisible(helpButton))
// Click it and navigate to /healthz
// NOTE: ran into issues of it failing intermittently
// without having button: "middle"
await Promise.all([page.waitForNavigation(), page.click(helpButton, { button: "middle" })])
// see the About button
const aboutButton = "a.action-menu-item span[aria-label='About']"
expect(await page.isVisible(aboutButton))
2021-01-29 02:48:57 +08:00
})
})