Skip to content

Commit 9bf5cc2

Browse files
committed
refactor: e2e tests based on jest-playwright
1 parent 7ece003 commit 9bf5cc2

File tree

4 files changed

+18
-66
lines changed

4 files changed

+18
-66
lines changed

test/e2e/login.test.ts

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,20 @@
1-
import { chromium, Page, Browser, BrowserContext } from "playwright"
1+
/// <reference types="jest-playwright-preset" />
22
import { CODE_SERVER_ADDRESS, PASSWORD } from "../utils/constants"
33

44
describe("login", () => {
5-
let browser: Browser
6-
let page: Page
7-
let context: BrowserContext
8-
9-
beforeAll(async () => {
10-
browser = await chromium.launch()
11-
context = await browser.newContext()
12-
})
13-
14-
afterAll(async () => {
15-
await browser.close()
16-
})
17-
185
beforeEach(async () => {
19-
page = await context.newPage()
20-
})
21-
22-
afterEach(async () => {
23-
await page.close()
24-
// Remove password from local storage
25-
await context.clearCookies()
6+
await jestPlaywright.resetContext()
7+
await page.goto(CODE_SERVER_ADDRESS)
268
})
279

2810
it("should be able to login", async () => {
29-
await page.goto(CODE_SERVER_ADDRESS)
3011
// Type in password
3112
await page.fill(".password", PASSWORD)
3213
// Click the submit button and login
3314
await page.click(".submit")
15+
// For some reason, it wasn't waiting for the click and navigation before checking
16+
// so adding a timeout ensures that we allow the editor time to load
17+
await page.waitForTimeout(1000)
3418
// See the editor
3519
const codeServerEditor = await page.isVisible(".monaco-workbench")
3620
expect(codeServerEditor).toBeTruthy()

test/e2e/logout.test.ts

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,9 @@
1-
import { chromium, Page, Browser, BrowserContext } from "playwright"
1+
/// <reference types="jest-playwright-preset" />
22
import { CODE_SERVER_ADDRESS, PASSWORD } from "../utils/constants"
33

44
describe("logout", () => {
5-
let browser: Browser
6-
let page: Page
7-
let context: BrowserContext
8-
9-
beforeAll(async () => {
10-
browser = await chromium.launch()
11-
context = await browser.newContext()
12-
})
13-
14-
afterAll(async () => {
15-
await browser.close()
16-
})
17-
185
beforeEach(async () => {
19-
page = await context.newPage()
20-
})
21-
22-
afterEach(async () => {
23-
await page.close()
24-
// Remove password from local storage
25-
await context.clearCookies()
6+
await jestPlaywright.resetContext()
267
})
278

289
it("should be able login and logout", async () => {
@@ -31,6 +12,8 @@ describe("logout", () => {
3112
await page.fill(".password", PASSWORD)
3213
// Click the submit button and login
3314
await page.click(".submit")
15+
// Allow time to navigate
16+
await page.waitForTimeout(1000)
3417
// See the editor
3518
const codeServerEditor = await page.isVisible(".monaco-workbench")
3619
expect(codeServerEditor).toBeTruthy()

test/e2e/openHelpAbout.test.ts

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
import { chromium, Page, Browser, BrowserContext, Cookie } from "playwright"
1+
/// <reference types="jest-playwright-preset" />
2+
import { Cookie } from "playwright"
23
import { hash } from "../../src/node/util"
34
import { CODE_SERVER_ADDRESS, PASSWORD, STORAGE } from "../utils/constants"
45
import { createCookieIfDoesntExist } from "../utils/helpers"
56

67
describe("Open Help > About", () => {
7-
let browser: Browser
8-
let page: Page
9-
let context: BrowserContext
10-
11-
beforeAll(async () => {
12-
browser = await chromium.launch()
8+
beforeEach(async () => {
139
// Create a new context with the saved storage state
1410
const storageState = JSON.parse(STORAGE) || {}
1511

@@ -42,22 +38,7 @@ describe("Open Help > About", () => {
4238
// See discussion: https://github.com/cdr/code-server/pull/2648#discussion_r575434946
4339

4440
const maybeUpdatedCookies = createCookieIfDoesntExist(cookies, cookieToStore)
45-
46-
context = await browser.newContext({
47-
storageState: { cookies: maybeUpdatedCookies },
48-
})
49-
})
50-
51-
afterAll(async () => {
52-
// Remove password from local storage
53-
await context.clearCookies()
54-
55-
await context.close()
56-
await browser.close()
57-
})
58-
59-
beforeEach(async () => {
60-
page = await context.newPage()
41+
await jestPlaywright.resetBrowser({ storageState: { cookies: maybeUpdatedCookies } })
6142
})
6243

6344
it("should see a 'Help' then 'About' button in the Application Menu that opens a dialog", async () => {

test/utils/globalSetup.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ module.exports = async () => {
2121
await page.fill(".password", PASSWORD)
2222
// Click the submit button and login
2323
await page.click(".submit")
24+
// After logging in, we store a cookie in localStorage
25+
// we need to wait a bit to make sure that happens
26+
// before we grab the storage and save it
27+
await page.waitForTimeout(1000)
2428

2529
// Save storage state and store as an env variable
2630
// More info: https://playwright.dev/docs/auth?_highlight=authe#reuse-authentication-state

0 commit comments

Comments
 (0)