-
Notifications
You must be signed in to change notification settings - Fork 6k
feat(testing): add e2e test for 'Go Home' button #2648
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
f0b5a57
feat: add playwright
jsjoeio c2f1a2d
feat: add test for login page
jsjoeio d7e41a3
fix: increase test timeout to 30000
jsjoeio 3033c8f
feat: add test to visit go home in app menu
jsjoeio 34c6ec4
feat: add globalSetup for testing
jsjoeio 9eba2bd
fix(ci): update test job to use bin
jsjoeio 236717e
fix: update modulePathIgnorePatterns for jest
jsjoeio ffdbf3a
feat: add test/videos & /screenshots to gitignore
jsjoeio 9e3c8bd
feat: add step to upload test videos
jsjoeio e077f2d
refactor: update test script to check env var
jsjoeio b02d2fb
feat: add cookie utils for e2e tests
jsjoeio 2dc56ad
refactor: manually add cookie goHome
jsjoeio d0eece3
refactor: add note to test.sh about --home
jsjoeio 06af8b3
refactor: update goHome location in test
jsjoeio 38d7718
refactor: use promises for goHome test
jsjoeio 3fa460c
refactor: create helpers.ts & add Cookie
jsjoeio 5857b25
chore: add todo regarding storage and cookies e2e
jsjoeio b0fd554
refactor: add constants.ts with PASSWORD, etc
jsjoeio d61bbc4
refactor(goHome): check url, remove timeout
jsjoeio 6d4f814
Close context before browser
code-asher ef7e727
Fix unreadable wtfnode output
code-asher 6685b3a
Move wtfnode setup to global setup
code-asher 47a05c9
Gate wtfnode behind WTF_NODE env var
code-asher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,6 @@ node-* | |
/lib/coder-cloud-agent | ||
.home | ||
coverage | ||
**/.DS_Store | ||
**/.DS_Store | ||
test/videos | ||
test/screenshots |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const CODE_SERVER_ADDRESS = process.env.CODE_SERVER_ADDRESS || "http://localhost:8080" | ||
export const PASSWORD = process.env.PASSWORD || "e45432jklfdsab" | ||
export const STORAGE = process.env.STORAGE || "" | ||
code-asher marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// 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 } from "playwright" | ||
import { CODE_SERVER_ADDRESS, PASSWORD } from "./constants" | ||
import * as wtfnode from "./wtfnode" | ||
|
||
module.exports = async () => { | ||
console.log("\n🚨 Running Global Setup for Jest Tests") | ||
console.log(" Please hang tight...") | ||
const browser = await chromium.launch() | ||
const context = await browser.newContext() | ||
const page = await context.newPage() | ||
|
||
if (process.env.WTF_NODE) { | ||
wtfnode.setup() | ||
} | ||
|
||
await page.goto(CODE_SERVER_ADDRESS, { waitUntil: "domcontentloaded" }) | ||
// Type in password | ||
await page.fill(".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() | ||
console.log("✅ Global Setup for Jest Tests is now complete.") | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
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" | ||
|
||
describe("go home", () => { | ||
let browser: Browser | ||
let page: Page | ||
let context: BrowserContext | ||
|
||
beforeAll(async () => { | ||
browser = await chromium.launch() | ||
// Create a new context with the saved storage state | ||
const storageState = JSON.parse(STORAGE) || {} | ||
|
||
const cookieToStore = { | ||
sameSite: "Lax" as const, | ||
name: "key", | ||
value: hash(PASSWORD), | ||
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 | ||
|
||
const maybeUpdatedCookies = createCookieIfDoesntExist(cookies, cookieToStore) | ||
jsjoeio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
context = await browser.newContext({ | ||
storageState: { cookies: maybeUpdatedCookies }, | ||
recordVideo: { dir: "./test/videos/" }, | ||
}) | ||
}) | ||
|
||
afterAll(async () => { | ||
// Remove password from local storage | ||
await context.clearCookies() | ||
|
||
await context.close() | ||
await browser.close() | ||
}) | ||
|
||
beforeEach(async () => { | ||
page = await context.newPage() | ||
}) | ||
|
||
// NOTE: this test will fail if you do not run code-server with --home $CODE_SERVER_ADDRESS/healthz | ||
it("should see a 'Go Home' button in the Application Menu that goes to /healthz", async () => { | ||
const GO_HOME_URL = `${CODE_SERVER_ADDRESS}/healthz` | ||
// 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()) | ||
|
||
// waitUntil: "domcontentloaded" | ||
// In case the page takes a long time to load | ||
await page.goto(CODE_SERVER_ADDRESS, { waitUntil: "domcontentloaded" }) | ||
|
||
// Click the Home menu | ||
await page.click(".home-bar ul[aria-label='Home'] li") | ||
// See the Go Home button | ||
const goHomeButton = "a.action-menu-item span[aria-label='Go Home']" | ||
expect(await page.isVisible(goHomeButton)) | ||
|
||
// 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(goHomeButton, { button: "middle" })]) | ||
expect(page.url()).toBe(GO_HOME_URL) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Borrowed from playwright | ||
export interface Cookie { | ||
name: string | ||
value: string | ||
domain: string | ||
path: string | ||
/** | ||
* Unix time in seconds. | ||
*/ | ||
expires: number | ||
httpOnly: boolean | ||
secure: boolean | ||
sameSite: "Strict" | "Lax" | "None" | ||
} | ||
|
||
/** | ||
* Checks if a cookie exists in array of cookies | ||
*/ | ||
export function checkForCookie(cookies: Array<Cookie>, key: string): boolean { | ||
// Check for a cookie where the name is equal to key | ||
return Boolean(cookies.find((cookie) => cookie.name === key)) | ||
} | ||
|
||
/** | ||
* Creates a login cookie if one doesn't already exist | ||
*/ | ||
export function createCookieIfDoesntExist(cookies: Array<Cookie>, cookieToStore: Cookie): Array<Cookie> { | ||
const cookieName = cookieToStore.name | ||
const doesCookieExist = checkForCookie(cookies, cookieName) | ||
if (!doesCookieExist) { | ||
const updatedCookies = [...cookies, cookieToStore] | ||
return updatedCookies | ||
} | ||
return cookies | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { chromium, Page, Browser, BrowserContext } from "playwright" | ||
import { CODE_SERVER_ADDRESS, PASSWORD } from "./constants" | ||
|
||
describe("login", () => { | ||
let browser: Browser | ||
let page: Page | ||
let context: BrowserContext | ||
|
||
beforeAll(async () => { | ||
browser = await chromium.launch() | ||
context = await browser.newContext() | ||
}) | ||
|
||
afterAll(async () => { | ||
await browser.close() | ||
}) | ||
|
||
beforeEach(async () => { | ||
page = await context.newPage() | ||
}) | ||
|
||
afterEach(async () => { | ||
await page.close() | ||
// Remove password from local storage | ||
await context.clearCookies() | ||
}) | ||
|
||
it("should be able to login", async () => { | ||
await page.goto(CODE_SERVER_ADDRESS) | ||
// Type in password | ||
await page.fill(".password", PASSWORD) | ||
// Click the submit button and login | ||
await page.click(".submit") | ||
// See the editor | ||
const codeServerEditor = await page.isVisible(".monaco-workbench") | ||
expect(codeServerEditor).toBeTruthy() | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.