Skip to content

Commit b0ecff3

Browse files
committed
refactor: globalSetup to use CodeServer model
1 parent cb65590 commit b0ecff3

8 files changed

+129
-60
lines changed

test/e2e/browser.test.ts

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
import { test, expect } from "@playwright/test"
2-
import { CODE_SERVER_ADDRESS } from "../utils/constants"
2+
import { CodeServer } from "./models/CodeServer"
33

44
// This is a "gut-check" test to make sure playwright is working as expected
5-
test("browser should display correct userAgent", async ({ page, browserName }) => {
6-
const displayNames = {
7-
chromium: "Chrome",
8-
firefox: "Firefox",
9-
webkit: "Safari",
10-
}
11-
await page.goto(CODE_SERVER_ADDRESS, { waitUntil: "networkidle" })
12-
const userAgent = await page.evaluate("navigator.userAgent")
13-
14-
expect(userAgent).toContain(displayNames[browserName])
5+
test.describe("browser", () => {
6+
let codeServer: CodeServer
7+
8+
test.beforeEach(async ({ page }) => {
9+
codeServer = new CodeServer(page)
10+
await codeServer.navigate()
11+
})
12+
13+
test("browser should display correct userAgent", async ({ page, browserName }) => {
14+
const displayNames = {
15+
chromium: "Chrome",
16+
firefox: "Firefox",
17+
webkit: "Safari",
18+
}
19+
const userAgent = await page.evaluate("navigator.userAgent")
20+
21+
expect(userAgent).toContain(displayNames[browserName])
22+
})
1523
})

test/e2e/codeServer.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ test.describe("CodeServer", () => {
2020

2121
test.beforeEach(async ({ page }) => {
2222
codeServer = new CodeServer(page)
23-
await codeServer.navigate()
23+
await codeServer.setup()
2424
})
2525

26-
test("should navigate to the CODE_SERVER_ADDRESS", options, async ({ page }) => {
26+
test(`should navigate to ${CODE_SERVER_ADDRESS}`, options, async ({ page }) => {
2727
// We navigate codeServer before each test
2828
// and we start the test with a storage state
2929
// which means we should be logged in

test/e2e/globalSetup.test.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { test, expect } from "@playwright/test"
2-
import { CODE_SERVER_ADDRESS, STORAGE } from "../utils/constants"
2+
import { STORAGE } from "../utils/constants"
3+
import { CodeServer } from "./models/CodeServer"
34

45
// This test is to make sure the globalSetup works as expected
56
// meaning globalSetup ran and stored the storageState in STORAGE
67
test.describe("globalSetup", () => {
78
// Create a new context with the saved storage state
89
// so we don't have to logged in
910
const options: any = {}
11+
let codeServer: CodeServer
1012

1113
// TODO@jsjoeio
1214
// Fix this once https://github.com/microsoft/playwright-test/issues/240
@@ -17,9 +19,12 @@ test.describe("globalSetup", () => {
1719
storageState,
1820
}
1921
}
22+
test.beforeEach(async ({ page }) => {
23+
codeServer = new CodeServer(page)
24+
await codeServer.setup()
25+
})
2026
test("should keep us logged in using the storageState", options, async ({ page }) => {
21-
await page.goto(CODE_SERVER_ADDRESS, { waitUntil: "networkidle" })
2227
// Make sure the editor actually loaded
23-
expect(await page.isVisible("div.monaco-workbench")).toBe(true)
28+
expect(await codeServer.isEditorVisible()).toBe(true)
2429
})
2530
})

test/e2e/login.test.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { test, expect } from "@playwright/test"
2-
import { CODE_SERVER_ADDRESS, PASSWORD } from "../utils/constants"
2+
import { PASSWORD } from "../utils/constants"
3+
import { CodeServer } from "./models/CodeServer"
34

45
test.describe("login", () => {
56
// Reset the browser so no cookies are persisted
@@ -9,26 +10,32 @@ test.describe("login", () => {
910
storageState: {},
1011
},
1112
}
13+
let codeServer: CodeServer
14+
15+
test.beforeEach(async ({ page }) => {
16+
codeServer = new CodeServer(page)
17+
await codeServer.navigate()
18+
})
1219

1320
test("should see the login page", options, async ({ page }) => {
14-
await page.goto(CODE_SERVER_ADDRESS, { waitUntil: "networkidle" })
1521
// It should send us to the login page
1622
expect(await page.title()).toBe("code-server login")
1723
})
1824

1925
test("should be able to login", options, async ({ page }) => {
20-
await page.goto(CODE_SERVER_ADDRESS, { waitUntil: "networkidle" })
2126
// Type in password
2227
await page.fill(".password", PASSWORD)
2328
// Click the submit button and login
2429
await page.click(".submit")
2530
await page.waitForLoadState("networkidle")
31+
// We do this because occassionally code-server doesn't load on Firefox
32+
// but loads if you reload once or twice
33+
await codeServer.reloadUntilEditorIsVisible()
2634
// Make sure the editor actually loaded
27-
expect(await page.isVisible("div.monaco-workbench")).toBe(true)
35+
expect(await codeServer.isEditorVisible()).toBe(true)
2836
})
2937

3038
test("should see an error message for missing password", options, async ({ page }) => {
31-
await page.goto(CODE_SERVER_ADDRESS, { waitUntil: "networkidle" })
3239
// Skip entering password
3340
// Click the submit button and login
3441
await page.click(".submit")
@@ -37,7 +44,6 @@ test.describe("login", () => {
3744
})
3845

3946
test("should see an error message for incorrect password", options, async ({ page }) => {
40-
await page.goto(CODE_SERVER_ADDRESS, { waitUntil: "networkidle" })
4147
// Type in password
4248
await page.fill(".password", "password123")
4349
// Click the submit button and login
@@ -47,7 +53,6 @@ test.describe("login", () => {
4753
})
4854

4955
test("should hit the rate limiter for too many unsuccessful logins", options, async ({ page }) => {
50-
await page.goto(CODE_SERVER_ADDRESS, { waitUntil: "networkidle" })
5156
// Type in password
5257
await page.fill(".password", "password123")
5358
// Click the submit button and login

test/e2e/logout.test.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { test, expect } from "@playwright/test"
22
import { CODE_SERVER_ADDRESS, PASSWORD } from "../utils/constants"
3+
import { CodeServer } from "./models/CodeServer"
34

45
test.describe("logout", () => {
56
// Reset the browser so no cookies are persisted
@@ -9,15 +10,24 @@ test.describe("logout", () => {
910
storageState: {},
1011
},
1112
}
13+
let codeServer: CodeServer
14+
15+
test.beforeEach(async ({ page }) => {
16+
codeServer = new CodeServer(page)
17+
await codeServer.navigate()
18+
})
19+
1220
test("should be able login and logout", options, async ({ page }) => {
13-
await page.goto(CODE_SERVER_ADDRESS, { waitUntil: "networkidle" })
1421
// Type in password
1522
await page.fill(".password", PASSWORD)
1623
// Click the submit button and login
1724
await page.click(".submit")
1825
await page.waitForLoadState("networkidle")
26+
// We do this because occassionally code-server doesn't load on Firefox
27+
// but loads if you reload once or twice
28+
await codeServer.reloadUntilEditorIsVisible()
1929
// Make sure the editor actually loaded
20-
expect(await page.isVisible("div.monaco-workbench")).toBe(true)
30+
expect(await codeServer.isEditorVisible()).toBe(true)
2131

2232
// Click the Application menu
2333
await page.click("[aria-label='Application Menu']")

test/e2e/models/CodeServer.ts

+31-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ export class CodeServer {
1515
*/
1616
async navigate() {
1717
await this.page.goto(CODE_SERVER_ADDRESS, { waitUntil: "networkidle" })
18+
}
1819

20+
/**
21+
* Checks if the editor is visible
22+
* and reloads until it is
23+
*/
24+
async reloadUntilEditorIsVisible() {
1925
const editorIsVisible = await this.isEditorVisible()
2026
let reloadCount = 0
2127

@@ -56,7 +62,12 @@ export class CodeServer {
5662
// then we can focus it by hitting the keyboard shortcut
5763
const isTerminalVisible = await this.page.isVisible("#terminal")
5864
if (isTerminalVisible) {
59-
await this.page.keyboard.press(`Meta+Backquote`)
65+
await this.page.keyboard.press(`Control+Backquote`)
66+
// Wait for terminal to receive focus
67+
await this.page.waitForSelector("div.terminal.xterm.focus")
68+
// Sometimes the terminal reloads
69+
// which is why we wait for it twice
70+
await this.page.waitForSelector("div.terminal.xterm.focus")
6071
return
6172
}
6273
// Open using the manu
@@ -70,5 +81,24 @@ export class CodeServer {
7081
// Click text=Terminal
7182
await this.page.hover("text=Terminal")
7283
await this.page.click("text=Terminal")
84+
85+
// Wait for terminal to receive focus
86+
// Sometimes the terminal reloads once or twice
87+
// which is why we wait for it to have the focus class
88+
await this.page.waitForSelector("div.terminal.xterm.focus")
89+
// Sometimes the terminal reloads
90+
// which is why we wait for it twice
91+
await this.page.waitForSelector("div.terminal.xterm.focus")
92+
}
93+
94+
/**
95+
* Navigates to CODE_SERVER_ADDRESS
96+
* and reloads until the editor is visible
97+
*
98+
* Helpful for running before tests
99+
*/
100+
async setup() {
101+
await this.navigate()
102+
await this.reloadUntilEditorIsVisible()
73103
}
74104
}

test/e2e/openHelpAbout.test.ts

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { test, expect } from "@playwright/test"
2-
import { CODE_SERVER_ADDRESS, STORAGE } from "../utils/constants"
2+
import { STORAGE } from "../utils/constants"
3+
import { CodeServer } from "./models/CodeServer"
34

45
test.describe("Open Help > About", () => {
56
// Create a new context with the saved storage state
67
// so we don't have to logged in
78
const options: any = {}
9+
let codeServer: CodeServer
810
// TODO@jsjoeio
911
// Fix this once https://github.com/microsoft/playwright-test/issues/240
1012
// is fixed
@@ -15,32 +17,30 @@ test.describe("Open Help > About", () => {
1517
}
1618
}
1719

20+
test.beforeEach(async ({ page }) => {
21+
codeServer = new CodeServer(page)
22+
await codeServer.setup()
23+
})
24+
1825
test(
1926
"should see a 'Help' then 'About' button in the Application Menu that opens a dialog",
2027
options,
2128
async ({ page }) => {
22-
await page.goto(CODE_SERVER_ADDRESS, { waitUntil: "networkidle" })
23-
// Make sure the editor actually loaded
24-
expect(await page.isVisible("div.monaco-workbench")).toBe(true)
25-
26-
// Click the Application menu
27-
await page.click("[aria-label='Application Menu']")
28-
// See the Help button
29-
const helpButton = "a.action-menu-item span[aria-label='Help']"
30-
expect(await page.isVisible(helpButton)).toBe(true)
29+
// Open using the manu
30+
// Click [aria-label="Application Menu"] div[role="none"]
31+
await page.click('[aria-label="Application Menu"] div[role="none"]')
3132

32-
// Hover the helpButton
33-
await page.hover(helpButton)
33+
// Click the Help button
34+
await page.hover("text=Help")
35+
await page.click("text=Help")
3436

35-
// see the About button and click it
36-
const aboutButton = "a.action-menu-item span[aria-label='About']"
37-
expect(await page.isVisible(aboutButton)).toBe(true)
38-
// NOTE: it won't work unless you hover it first
39-
await page.hover(aboutButton)
40-
await page.click(aboutButton)
37+
// Click the About button
38+
await page.hover("text=About")
39+
await page.click("text=About")
4140

42-
const codeServerText = "text=code-server"
43-
expect(await page.isVisible(codeServerText)).toBe(true)
41+
// Click div[role="dialog"] >> text=code-server
42+
const element = await page.waitForSelector('div[role="dialog"] >> text=code-server')
43+
expect(element).not.toBeNull()
4444
},
4545
)
4646
})

test/e2e/terminal.test.ts

+25-14
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@ import { test, expect } from "@playwright/test"
22
import * as fs from "fs"
33
import { tmpdir } from "os"
44
import * as path from "path"
5-
5+
import util from "util"
6+
import * as cp from "child_process"
67
import { STORAGE } from "../utils/constants"
78
import { CodeServer } from "./models/CodeServer"
89

910
test.describe("Integrated Terminal", () => {
1011
// Create a new context with the saved storage state
1112
// so we don't have to logged in
1213
const options: any = {}
13-
const testFileName = "test.txt"
14+
const testFileName = "pipe"
1415
const testString = "new string test from e2e test"
1516
let codeServer: CodeServer
17+
let tmpFolderPath: string = ""
18+
let tmpFile: string = ""
1619

1720
// TODO@jsjoeio
1821
// Fix this once https://github.com/microsoft/playwright-test/issues/240
@@ -25,26 +28,34 @@ test.describe("Integrated Terminal", () => {
2528
}
2629
test.beforeEach(async ({ page }) => {
2730
codeServer = new CodeServer(page)
28-
await codeServer.navigate()
29-
})
30-
31-
test("should echo a string to a file", options, async ({ page }) => {
31+
await codeServer.setup()
3232
// NOTE@jsjoeio
3333
// We're not using tmpdir from src/node/constants
3434
// because Playwright doesn't fully support ES modules from
3535
// the erorrs I'm seeing
36-
const tmpFolderPath = fs.mkdtempSync(path.join(tmpdir(), "code-server-test"))
37-
const tmpFile = `${tmpFolderPath}${path.sep}${testFileName}`
36+
tmpFolderPath = fs.mkdtempSync(path.join(tmpdir(), "code-server-test"))
37+
tmpFile = path.join(tmpFolderPath, testFileName)
38+
})
39+
40+
test.afterEach(async () => {
41+
// Ensure directory was removed
42+
fs.rmdirSync(tmpFolderPath, { recursive: true })
43+
})
44+
45+
test("should echo a string to a file", options, async ({ page }) => {
46+
const command = `mkfifo '${tmpFile}' && cat '${tmpFile}'`
47+
const exec = util.promisify(cp.exec)
48+
const output = exec(command, { encoding: "utf8" })
49+
3850
// Open terminal and type in value
3951
await codeServer.focusTerminal()
4052

41-
// give the terminal a second to load
42-
await page.waitForTimeout(3000)
43-
await page.keyboard.type(`echo '${testString}' > ${tmpFile}`)
44-
// Wait for the typing to finish before hitting enter
45-
await page.waitForTimeout(500)
53+
await page.waitForLoadState("load")
54+
await page.keyboard.type(`echo '${testString}' > '${tmpFile}'`)
4655
await page.keyboard.press("Enter")
47-
await page.waitForTimeout(2000)
56+
57+
const { stdout } = await output
58+
expect(stdout).toMatch(testString)
4859

4960
// .access checks if the file exists without opening it
5061
// it doesn't return anything hence why we expect it to

0 commit comments

Comments
 (0)