Skip to content

Commit c27a69b

Browse files
committed
refactor: globalSetup to use CodeServer model
1 parent 0436f77 commit c27a69b

8 files changed

+91
-46
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

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

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

2626
test("should navigate to the CODE_SERVER_ADDRESS", options, async ({ page }) => {

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

+18-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,7 @@ 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`)
6066
return
6167
}
6268
// Open using the manu
@@ -71,4 +77,15 @@ export class CodeServer {
7177
await this.page.hover("text=Terminal")
7278
await this.page.click("text=Terminal")
7379
}
80+
81+
/**
82+
* Navigates to CODE_SERVER_ADDRESS
83+
* and reloads until the editor is visible
84+
*
85+
* Helpful for running before tests
86+
*/
87+
async setup() {
88+
await this.navigate()
89+
await this.reloadUntilEditorIsVisible()
90+
}
7491
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ test.describe("Integrated Terminal", () => {
2525
}
2626
test.beforeEach(async ({ page }) => {
2727
codeServer = new CodeServer(page)
28-
await codeServer.navigate()
28+
await codeServer.setup()
2929
})
3030

3131
test("should echo a string to a file", options, async ({ page }) => {

0 commit comments

Comments
 (0)