Skip to content

chore(site): Improve the e2e setup #5840

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 2 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ site/coverage/
site/storybook-static/
site/test-results/*
site/e2e/test-results/*
site/e2e/storageState.json
site/e2e/states/*.json
site/playwright-report/*

# Make target for updating golden files.
Expand Down
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ site/coverage/
site/storybook-static/
site/test-results/*
site/e2e/test-results/*
site/e2e/storageState.json
site/e2e/states/*.json
site/playwright-report/*

# Make target for updating golden files.
Expand Down
2 changes: 1 addition & 1 deletion site/.eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ coverage/
storybook-static/
test-results/*
e2e/test-results/*
e2e/storageState.json
e2e/states/*.json
playwright-report/*

# Make target for updating golden files.
Expand Down
2 changes: 1 addition & 1 deletion site/.prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ coverage/
storybook-static/
test-results/*
e2e/test-results/*
e2e/storageState.json
e2e/states/*.json
playwright-report/*

# Make target for updating golden files.
Expand Down
9 changes: 4 additions & 5 deletions site/e2e/constants.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Our base port. It's important to run on 3000,
// which matches our api server
export const basePort = 3000
// Default port from the server
export const defaultPort = 3000

// Credentials for the default user when running in dev mode.
export const username = "developer"
// Credentials for the first user
export const username = "admin"
export const password = "password"
export const email = "admin@coder.com"
23 changes: 21 additions & 2 deletions site/e2e/globalSetup.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
import axios from "axios"
import { request } from "playwright"
import { createFirstUser } from "../src/api/api"
import * as constants from "./constants"
import { getStatePath } from "./helpers"

const globalSetup = async (): Promise<void> => {
axios.defaults.baseURL = `http://localhost:${constants.basePort}`
// Create a user
axios.defaults.baseURL = `http://localhost:${constants.defaultPort}`

// Create first user
await createFirstUser({
email: constants.email,
username: constants.username,
password: constants.password,
trial: false,
})

// Authenticated storage
const authenticatedRequestContext = await request.newContext()
await authenticatedRequestContext.post(
`http://localhost:${constants.defaultPort}/api/v2/users/login`,
{
data: {
email: constants.email,
password: constants.password,
},
},
)
await authenticatedRequestContext.storageState({
path: getStatePath("authState"),
})
await authenticatedRequestContext.dispose()
}

export default globalSetup
7 changes: 7 additions & 0 deletions site/e2e/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Page } from "@playwright/test"
import path from "path"

export const buttons = {
starterTemplates: "Starter templates",
Expand All @@ -22,3 +23,9 @@ export const fillInput = async (
): Promise<void> => {
await page.fill(`text=${label}`, value)
}

const statesDir = path.join(__dirname, "./states")

export const getStatePath = (name: string): string => {
return path.join(statesDir, `${name}.json`)
}
31 changes: 11 additions & 20 deletions site/e2e/playwright.config.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,23 @@
import { PlaywrightTestConfig } from "@playwright/test"
import * as path from "path"
import { basePort } from "./constants"
import path from "path"
import { defaultPort } from "./constants"

const port = process.env.CODER_E2E_PORT
? Number(process.env.CODER_E2E_PORT)
: defaultPort

const coderMain = path.join(__dirname, "../../enterprise/cmd/coder/main.go")

const config: PlaywrightTestConfig = {
testDir: "tests",
globalSetup: require.resolve("./globalSetup"),

// Create junit report file for upload to DataDog
reporter: [["junit", { outputFile: "test-results/junit.xml" }]],

// NOTE: if Playwright complains about the port being taken
// do not change the basePort (it must match our api server).
// Instead, simply run the test suite without running our local server.
use: {
baseURL: `http://localhost:${basePort}`,
baseURL: `http://localhost:${port}`,
video: "retain-on-failure",
},

// `webServer` tells Playwright to launch a test server - more details here:
// https://playwright.dev/docs/test-advanced#launching-a-development-web-server-during-the-tests
webServer: {
// Run the coder daemon directly.
command: `go run -tags embed ${path.join(
__dirname,
"../../enterprise/cmd/coder/main.go",
)} server --in-memory --access-url http://127.0.0.1:${basePort}`,
port: basePort,
timeout: 120 * 10000,
command: `go run -tags embed ${coderMain} server --global-config $(mktemp -d -t e2e-XXXXXXXXXX)`,
port,
reuseExistingServer: false,
},
}
Expand Down
Empty file added site/e2e/states/.gitkeep
Empty file.
9 changes: 9 additions & 0 deletions site/e2e/tests/listTemplates.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { test, expect } from "@playwright/test"
import { getStatePath } from "../helpers"

test.use({ storageState: getStatePath("authState") })

test("list templates", async ({ page, baseURL }) => {
await page.goto(`${baseURL}/templates`, { waitUntil: "networkidle" })
await expect(page).toHaveTitle("Templates – Coder")
})