Skip to content

Commit 10d7698

Browse files
mtojekpull[bot]
authored andcommitted
test(site): improve E2E framework (#9438)
1 parent 1ae3396 commit 10d7698

12 files changed

+132
-11
lines changed

.github/workflows/ci.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ jobs:
554554
- run: pnpm playwright:install
555555
working-directory: site
556556

557-
- run: pnpm playwright:test
557+
- run: pnpm playwright:test --workers 1
558558
env:
559559
DEBUG: pw:api
560560
working-directory: site

site/e2e/hooks.ts

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { Page } from "@playwright/test"
2+
3+
export const beforeCoderTest = async (page: Page) => {
4+
// eslint-disable-next-line no-console -- Show everything that was printed with console.log()
5+
page.on("console", (msg) => console.log("[onConsole] " + msg.text()))
6+
7+
page.on("request", (request) => {
8+
if (!isApiCall(request.url())) {
9+
return
10+
}
11+
12+
// eslint-disable-next-line no-console -- Log HTTP requests for debugging purposes
13+
console.log(
14+
`[onRequest] method=${request.method()} url=${request.url()} postData=${
15+
request.postData() ? request.postData() : ""
16+
}`,
17+
)
18+
})
19+
page.on("response", async (response) => {
20+
if (!isApiCall(response.url())) {
21+
return
22+
}
23+
24+
const shouldLogResponse =
25+
!response.url().endsWith("/api/v2/deployment/config") &&
26+
!response.url().endsWith("/api/v2/debug/health")
27+
28+
let responseText = ""
29+
try {
30+
if (shouldLogResponse) {
31+
const buffer = await response.body()
32+
responseText = buffer.toString("utf-8")
33+
responseText = responseText.replace(/\n$/g, "")
34+
} else {
35+
responseText = "skipped..."
36+
}
37+
} catch (error) {
38+
responseText = "not_available"
39+
}
40+
41+
// eslint-disable-next-line no-console -- Log HTTP requests for debugging purposes
42+
console.log(
43+
`[onResponse] url=${response.url()} status=${response.status()} body=${responseText}`,
44+
)
45+
})
46+
}
47+
48+
const isApiCall = (urlString: string): boolean => {
49+
const url = new URL(urlString)
50+
const apiPath = "/api/v2"
51+
52+
return url.pathname.startsWith(apiPath)
53+
}

site/e2e/playwright.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export default defineConfig({
3030
timeout: 60000,
3131
},
3232
],
33+
reporter: [["./reporter.ts"]],
3334
use: {
3435
baseURL: `http://localhost:${port}`,
3536
video: "retain-on-failure",

site/e2e/reporter.ts

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import type {
2+
FullConfig,
3+
Suite,
4+
TestCase,
5+
TestResult,
6+
FullResult,
7+
Reporter,
8+
} from "@playwright/test/reporter"
9+
10+
class CoderReporter implements Reporter {
11+
onBegin(config: FullConfig, suite: Suite) {
12+
// eslint-disable-next-line no-console -- Helpful for debugging
13+
console.log(`Starting the run with ${suite.allTests().length} tests`)
14+
}
15+
16+
onTestBegin(test: TestCase) {
17+
// eslint-disable-next-line no-console -- Helpful for debugging
18+
console.log(`Starting test ${test.title}`)
19+
}
20+
21+
onStdOut(chunk: string, test: TestCase, _: TestResult): void {
22+
// eslint-disable-next-line no-console -- Helpful for debugging
23+
console.log(
24+
`[stdout] [${test ? test.title : "unknown"}]: ${chunk.replace(
25+
/\n$/g,
26+
"",
27+
)}`,
28+
)
29+
}
30+
31+
onStdErr(chunk: string, test: TestCase, _: TestResult): void {
32+
// eslint-disable-next-line no-console -- Helpful for debugging
33+
console.log(
34+
`[stderr] [${test ? test.title : "unknown"}]: ${chunk.replace(
35+
/\n$/g,
36+
"",
37+
)}`,
38+
)
39+
}
40+
41+
onTestEnd(test: TestCase, result: TestResult) {
42+
// eslint-disable-next-line no-console -- Helpful for debugging
43+
console.log(`Finished test ${test.title}: ${result.status}`)
44+
}
45+
46+
onEnd(result: FullResult) {
47+
// eslint-disable-next-line no-console -- Helpful for debugging
48+
console.log(`Finished the run: ${result.status}`)
49+
}
50+
}
51+
52+
// eslint-disable-next-line no-unused-vars -- Playwright config uses it
53+
export default CoderReporter

site/e2e/tests/app.spec.ts

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { test } from "@playwright/test"
22
import { randomUUID } from "crypto"
33
import * as http from "http"
44
import { createTemplate, createWorkspace, startAgent } from "../helpers"
5+
import { beforeCoderTest } from "../hooks"
6+
7+
test.beforeEach(async ({ page }) => await beforeCoderTest(page))
58

69
test("app", async ({ context, page }) => {
710
const appContent = "Hello World"

site/e2e/tests/createWorkspace.spec.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { test, Page } from "@playwright/test"
1+
import { test } from "@playwright/test"
22
import {
33
createTemplate,
44
createWorkspace,
@@ -16,11 +16,9 @@ import {
1616
sixthParameter,
1717
} from "../parameters"
1818
import { RichParameter } from "../provisionerGenerated"
19+
import { beforeCoderTest } from "../hooks"
1920

20-
test.beforeEach(async ({ page }: { page: Page }) => {
21-
// eslint-disable-next-line no-console -- For debugging purposes
22-
page.on("console", (msg) => console.log("Console: " + msg.text()))
23-
})
21+
test.beforeEach(async ({ page }) => await beforeCoderTest(page))
2422

2523
test("create workspace", async ({ page }) => {
2624
const template = await createTemplate(page, {

site/e2e/tests/gitAuth.spec.ts

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { gitAuth } from "../constants"
33
import { Endpoints } from "@octokit/types"
44
import { GitAuthDevice } from "api/typesGenerated"
55
import { Awaiter, createServer } from "../helpers"
6+
import { beforeCoderTest } from "../hooks"
7+
8+
test.beforeEach(async ({ page }) => await beforeCoderTest(page))
69

710
// Ensures that a Git auth provider with the device flow functions and completes!
811
test("git auth device", async ({ page }) => {

site/e2e/tests/outdatedAgent.spec.ts

+3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ import {
77
sshIntoWorkspace,
88
startAgentWithCommand,
99
} from "../helpers"
10+
import { beforeCoderTest } from "../hooks"
1011

1112
const agentVersion = "v0.14.0"
1213

14+
test.beforeEach(async ({ page }) => await beforeCoderTest(page))
15+
1316
test("ssh with agent " + agentVersion, async ({ page }) => {
1417
const token = randomUUID()
1518
const template = await createTemplate(page, {

site/e2e/tests/outdatedCLI.spec.ts

+3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ import {
77
sshIntoWorkspace,
88
startAgent,
99
} from "../helpers"
10+
import { beforeCoderTest } from "../hooks"
1011

1112
const clientVersion = "v0.14.0"
1213

14+
test.beforeEach(async ({ page }) => await beforeCoderTest(page))
15+
1316
test("ssh with client " + clientVersion, async ({ page }) => {
1417
const token = randomUUID()
1518
const template = await createTemplate(page, {

site/e2e/tests/restartWorkspace.spec.ts

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import {
99

1010
import { firstBuildOption, secondBuildOption } from "../parameters"
1111
import { RichParameter } from "../provisionerGenerated"
12+
import { beforeCoderTest } from "../hooks"
13+
14+
test.beforeEach(async ({ page }) => await beforeCoderTest(page))
1215

1316
test("restart workspace with ephemeral parameters", async ({ page }) => {
1417
const richParameters: RichParameter[] = [firstBuildOption, secondBuildOption]

site/e2e/tests/updateWorkspace.spec.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { test, Page } from "@playwright/test"
1+
import { test } from "@playwright/test"
22

33
import {
44
createTemplate,
@@ -18,11 +18,9 @@ import {
1818
secondBuildOption,
1919
} from "../parameters"
2020
import { RichParameter } from "../provisionerGenerated"
21+
import { beforeCoderTest } from "../hooks"
2122

22-
test.beforeEach(async ({ page }: { page: Page }) => {
23-
// eslint-disable-next-line no-console -- For debugging purposes
24-
page.on("console", (msg) => console.log("Console: " + msg.text()))
25-
})
23+
test.beforeEach(async ({ page }) => await beforeCoderTest(page))
2624

2725
test("update workspace, new optional, immutable parameter added", async ({
2826
page,

site/e2e/tests/webTerminal.spec.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { test } from "@playwright/test"
22
import { createTemplate, createWorkspace, startAgent } from "../helpers"
33
import { randomUUID } from "crypto"
4+
import { beforeCoderTest } from "../hooks"
5+
6+
test.beforeEach(async ({ page }) => await beforeCoderTest(page))
47

58
test("web terminal", async ({ context, page }) => {
69
const token = randomUUID()

0 commit comments

Comments
 (0)