From 4d7dbae6d59b42431484f129dd56226b65733e79 Mon Sep 17 00:00:00 2001 From: Bruno Quaresma Date: Mon, 23 Jan 2023 23:50:26 +0000 Subject: [PATCH 1/2] Simplify playwright conifg --- site/e2e/constants.ts | 5 ++--- site/e2e/globalSetup.ts | 4 ++-- site/e2e/playwright.config.ts | 32 +++++++++++--------------------- 3 files changed, 15 insertions(+), 26 deletions(-) diff --git a/site/e2e/constants.ts b/site/e2e/constants.ts index a04fabce81ec6..a059cf31bca32 100644 --- a/site/e2e/constants.ts +++ b/site/e2e/constants.ts @@ -1,6 +1,5 @@ -// 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" diff --git a/site/e2e/globalSetup.ts b/site/e2e/globalSetup.ts index a396acbff3d98..6aca64226ad57 100644 --- a/site/e2e/globalSetup.ts +++ b/site/e2e/globalSetup.ts @@ -3,8 +3,8 @@ import { createFirstUser } from "../src/api/api" import * as constants from "./constants" const globalSetup = async (): Promise => { - 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, diff --git a/site/e2e/playwright.config.ts b/site/e2e/playwright.config.ts index 30d46b3835811..216a5a7243485 100644 --- a/site/e2e/playwright.config.ts +++ b/site/e2e/playwright.config.ts @@ -1,33 +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, - reuseExistingServer: false, + command: `go run -tags embed ${coderMain} server --global-config /tmp`, + port, }, } From 329674f1c3f552b6d1f470c59bc0552f1546ab75 Mon Sep 17 00:00:00 2001 From: Bruno Quaresma Date: Tue, 24 Jan 2023 00:51:54 +0000 Subject: [PATCH 2/2] Add helpers and setup --- .gitignore | 2 +- .prettierignore | 2 +- site/.eslintignore | 2 +- site/.prettierignore | 2 +- site/e2e/constants.ts | 4 ++-- site/e2e/globalSetup.ts | 19 +++++++++++++++++++ site/e2e/helpers.ts | 7 +++++++ site/e2e/playwright.config.ts | 3 ++- site/e2e/states/.gitkeep | 0 site/e2e/tests/listTemplates.spec.ts | 9 +++++++++ 10 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 site/e2e/states/.gitkeep create mode 100644 site/e2e/tests/listTemplates.spec.ts diff --git a/.gitignore b/.gitignore index 5e4733083388c..357434e5e37dd 100644 --- a/.gitignore +++ b/.gitignore @@ -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. diff --git a/.prettierignore b/.prettierignore index de248ccdd1095..cec0b0cd13da5 100644 --- a/.prettierignore +++ b/.prettierignore @@ -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. diff --git a/site/.eslintignore b/site/.eslintignore index e699b562e73d6..1cdf29f2f7b76 100644 --- a/site/.eslintignore +++ b/site/.eslintignore @@ -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. diff --git a/site/.prettierignore b/site/.prettierignore index e699b562e73d6..1cdf29f2f7b76 100644 --- a/site/.prettierignore +++ b/site/.prettierignore @@ -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. diff --git a/site/e2e/constants.ts b/site/e2e/constants.ts index a059cf31bca32..5227e6d50175e 100644 --- a/site/e2e/constants.ts +++ b/site/e2e/constants.ts @@ -1,7 +1,7 @@ // 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" diff --git a/site/e2e/globalSetup.ts b/site/e2e/globalSetup.ts index 6aca64226ad57..ad61cca6f0f3d 100644 --- a/site/e2e/globalSetup.ts +++ b/site/e2e/globalSetup.ts @@ -1,9 +1,12 @@ 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 => { axios.defaults.baseURL = `http://localhost:${constants.defaultPort}` + // Create first user await createFirstUser({ email: constants.email, @@ -11,6 +14,22 @@ const globalSetup = async (): Promise => { 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 diff --git a/site/e2e/helpers.ts b/site/e2e/helpers.ts index fa37dc999ad9d..1b8defa88c4e2 100644 --- a/site/e2e/helpers.ts +++ b/site/e2e/helpers.ts @@ -1,4 +1,5 @@ import { Page } from "@playwright/test" +import path from "path" export const buttons = { starterTemplates: "Starter templates", @@ -22,3 +23,9 @@ export const fillInput = async ( ): Promise => { await page.fill(`text=${label}`, value) } + +const statesDir = path.join(__dirname, "./states") + +export const getStatePath = (name: string): string => { + return path.join(statesDir, `${name}.json`) +} diff --git a/site/e2e/playwright.config.ts b/site/e2e/playwright.config.ts index 216a5a7243485..43c01871dddc0 100644 --- a/site/e2e/playwright.config.ts +++ b/site/e2e/playwright.config.ts @@ -16,8 +16,9 @@ const config: PlaywrightTestConfig = { video: "retain-on-failure", }, webServer: { - command: `go run -tags embed ${coderMain} server --global-config /tmp`, + command: `go run -tags embed ${coderMain} server --global-config $(mktemp -d -t e2e-XXXXXXXXXX)`, port, + reuseExistingServer: false, }, } diff --git a/site/e2e/states/.gitkeep b/site/e2e/states/.gitkeep new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/site/e2e/tests/listTemplates.spec.ts b/site/e2e/tests/listTemplates.spec.ts new file mode 100644 index 0000000000000..429ee7ba07db1 --- /dev/null +++ b/site/e2e/tests/listTemplates.spec.ts @@ -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") +})