Skip to content

Commit ba2a39a

Browse files
committed
feat: add CodeServer page object for e2e tests
1 parent 72ca12c commit ba2a39a

File tree

3 files changed

+167
-0
lines changed

3 files changed

+167
-0
lines changed

test/e2e/codeServer.test.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { test, expect } from "@playwright/test"
2+
import { STORAGE } from "../utils/constants"
3+
import { CodeServer } from "./models/CodeServer"
4+
5+
test.describe("CodeServer", () => {
6+
// Create a new context with the saved storage state
7+
// so we don't have to logged in
8+
const options: any = {}
9+
let codeServer: CodeServer
10+
11+
// TODO@jsjoeio
12+
// Fix this once https://github.com/microsoft/playwright-test/issues/240
13+
// is fixed
14+
if (STORAGE) {
15+
const storageState = JSON.parse(STORAGE) || {}
16+
options.contextOptions = {
17+
storageState,
18+
}
19+
}
20+
21+
test.beforeEach(async ({ page }) => {
22+
codeServer = new CodeServer(page)
23+
await codeServer.navigate()
24+
})
25+
26+
test("should open the default folder if not open", options, async ({ page }) => {
27+
await codeServer.openFolder()
28+
29+
// find workspaceStorage in the Explorer menu, which would be open in the User folder
30+
// which is the default folder that opens
31+
expect(await page.isVisible("text=workspaceStorage")).toBe(true)
32+
})
33+
34+
test("should show the Integrated Terminal", options, async ({ page }) => {
35+
await codeServer.viewTerminal()
36+
expect(await page.isVisible("#terminal")).toBe(true)
37+
})
38+
39+
test("should open a file with quickOpen", options, async ({ page }) => {
40+
await codeServer.openFolder()
41+
await codeServer.quickOpen("extensions.json")
42+
// If the file is open, we will see an empty array
43+
// assuming no extensions are installed
44+
expect(await page.isVisible("text=[]"))
45+
})
46+
})

test/e2e/models/CodeServer.ts

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { Page } from "playwright"
2+
import { CODE_SERVER_ADDRESS } from "../../utils/constants"
3+
// This is a Page Object Model
4+
// We use these to simplify e2e test authoring
5+
// See Playwright docs: https://playwright.dev/docs/pom/
6+
export class CodeServer {
7+
page: Page
8+
9+
constructor(page: Page) {
10+
this.page = page
11+
}
12+
async navigate() {
13+
await this.page.goto(CODE_SERVER_ADDRESS, { waitUntil: "networkidle" })
14+
// Make sure the editor actually loaded
15+
await this.page.isVisible("div.monaco-workbench")
16+
}
17+
/**
18+
* Opens the default folder /User if no arg passed
19+
* @param absolutePath Example: /Users/jp/.local/share/code-server/User/
20+
*
21+
*/
22+
async openFolder(absolutePath?: string) {
23+
// Check if no folder is opened
24+
const folderIsNotOpen = await this.page.isVisible("text=You have not yet opened")
25+
26+
if (folderIsNotOpen) {
27+
// Open the default folder
28+
await this.page.keyboard.press("Meta+O")
29+
await this.page.keyboard.press("Enter")
30+
await this.page.waitForLoadState("networkidle")
31+
}
32+
}
33+
34+
/**
35+
* Toggles the integrated terminal if not already in view
36+
* and focuses it
37+
*/
38+
async viewTerminal() {
39+
// Check if Terminal is already in view
40+
const isTerminalInView = await this.page.isVisible("#terminal")
41+
42+
if (!isTerminalInView) {
43+
// Open using default keyboard shortcut
44+
await this.focusTerminal()
45+
await this.page.waitForSelector("#terminal")
46+
}
47+
}
48+
49+
async focusTerminal() {
50+
await this.page.keyboard.press("Control+Backquote")
51+
}
52+
53+
async quickOpen(input: string) {
54+
await this.page.keyboard.press("Meta+P")
55+
await this.page.waitForSelector('[aria-describedby="quickInput_message"]')
56+
await this.page.keyboard.type(input)
57+
await this.page.waitForTimeout(2000)
58+
await this.page.keyboard.press("Enter")
59+
await this.page.waitForTimeout(2000)
60+
}
61+
}

test/e2e/terminal.test.ts

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { test, expect } from "@playwright/test"
2+
import { STORAGE } from "../utils/constants"
3+
import { CodeServer } from "./models/CodeServer"
4+
5+
test.describe("Integrated Terminal", () => {
6+
// Create a new context with the saved storage state
7+
// so we don't have to logged in
8+
const options: any = {}
9+
const testFileName = "hello.txt"
10+
const testString = "new string test from e2e test"
11+
let codeServer: CodeServer
12+
13+
// TODO@jsjoeio
14+
// Fix this once https://github.com/microsoft/playwright-test/issues/240
15+
// is fixed
16+
if (STORAGE) {
17+
const storageState = JSON.parse(STORAGE) || {}
18+
options.contextOptions = {
19+
storageState,
20+
}
21+
}
22+
test.beforeEach(async ({ page }) => {
23+
codeServer = new CodeServer(page)
24+
await codeServer.navigate()
25+
})
26+
27+
test("should echo a string to a file", options, async ({ page }) => {
28+
// Open the default folder
29+
await codeServer.openFolder()
30+
31+
// Open terminal and type in value
32+
await codeServer.viewTerminal()
33+
await codeServer.focusTerminal()
34+
35+
await page.keyboard.type(`echo '${testString}' >> ${testFileName}`)
36+
await page.keyboard.press("Enter")
37+
await page.waitForTimeout(2000)
38+
// It should show up on the left sidebar as a new file
39+
const isFileVisible = await page.isVisible(`text="${testFileName}"`)
40+
expect(isFileVisible).toBe(true)
41+
42+
if (isFileVisible) {
43+
// Check that the file has the test string in it
44+
await codeServer.quickOpen(testFileName)
45+
expect(await page.isVisible(`text="${testString}"`)).toBe(true)
46+
47+
// Clean up
48+
// Remove file
49+
await codeServer.focusTerminal()
50+
await page.keyboard.type(`rm ${testFileName}`)
51+
await page.keyboard.press("Enter")
52+
await page.waitForTimeout(2000)
53+
// Close the file from workbench
54+
// otherwise it will still be visible
55+
// and our assertion will fail
56+
await page.keyboard.press(`Meta+W`)
57+
expect(await page.isVisible(`text="${testString}"`)).toBe(false)
58+
}
59+
})
60+
})

0 commit comments

Comments
 (0)