From 2665a4f61b76b8359df6ad8e944f05fdbd409039 Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Mon, 19 Apr 2021 15:25:57 -0700 Subject: [PATCH] feat: add CodeServer page object for e2e tests --- test/e2e/codeServer.test.ts | 46 ++++++++++++++++++++++++++ test/e2e/models/CodeServer.ts | 61 +++++++++++++++++++++++++++++++++++ test/e2e/terminal.test.ts | 60 ++++++++++++++++++++++++++++++++++ 3 files changed, 167 insertions(+) create mode 100644 test/e2e/codeServer.test.ts create mode 100644 test/e2e/models/CodeServer.ts create mode 100644 test/e2e/terminal.test.ts diff --git a/test/e2e/codeServer.test.ts b/test/e2e/codeServer.test.ts new file mode 100644 index 000000000..1982a100b --- /dev/null +++ b/test/e2e/codeServer.test.ts @@ -0,0 +1,46 @@ +import { test, expect } from "@playwright/test" +import { STORAGE } from "../utils/constants" +import { CodeServer } from "./models/CodeServer" + +test.describe("CodeServer", () => { + // Create a new context with the saved storage state + // so we don't have to logged in + const options: any = {} + let codeServer: CodeServer + + // TODO@jsjoeio + // Fix this once https://github.com/microsoft/playwright-test/issues/240 + // is fixed + if (STORAGE) { + const storageState = JSON.parse(STORAGE) || {} + options.contextOptions = { + storageState, + } + } + + test.beforeEach(async ({ page }) => { + codeServer = new CodeServer(page) + await codeServer.navigate() + }) + + test("should open the default folder if not open", options, async ({ page }) => { + await codeServer.openFolder() + + // find workspaceStorage in the Explorer menu, which would be open in the User folder + // which is the default folder that opens + expect(await page.isVisible("text=workspaceStorage")).toBe(true) + }) + + test("should show the Integrated Terminal", options, async ({ page }) => { + await codeServer.viewTerminal() + expect(await page.isVisible("#terminal")).toBe(true) + }) + + test("should open a file with quickOpen", options, async ({ page }) => { + await codeServer.openFolder() + await codeServer.quickOpen("extensions.json") + // If the file is open, we will see an empty array + // assuming no extensions are installed + expect(await page.isVisible("text=[]")) + }) +}) diff --git a/test/e2e/models/CodeServer.ts b/test/e2e/models/CodeServer.ts new file mode 100644 index 000000000..98fce1907 --- /dev/null +++ b/test/e2e/models/CodeServer.ts @@ -0,0 +1,61 @@ +import { Page } from "playwright" +import { CODE_SERVER_ADDRESS } from "../../utils/constants" +// This is a Page Object Model +// We use these to simplify e2e test authoring +// See Playwright docs: https://playwright.dev/docs/pom/ +export class CodeServer { + page: Page + + constructor(page: Page) { + this.page = page + } + async navigate() { + await this.page.goto(CODE_SERVER_ADDRESS, { waitUntil: "networkidle" }) + // Make sure the editor actually loaded + await this.page.isVisible("div.monaco-workbench") + } + /** + * Opens the default folder /User if no arg passed + * @param absolutePath Example: /Users/jp/.local/share/code-server/User/ + * + */ + async openFolder(absolutePath?: string) { + // Check if no folder is opened + const folderIsNotOpen = await this.page.isVisible("text=You have not yet opened") + + if (folderIsNotOpen) { + // Open the default folder + await this.page.keyboard.press("Meta+O") + await this.page.keyboard.press("Enter") + await this.page.waitForLoadState("networkidle") + } + } + + /** + * Toggles the integrated terminal if not already in view + * and focuses it + */ + async viewTerminal() { + // Check if Terminal is already in view + const isTerminalInView = await this.page.isVisible("#terminal") + + if (!isTerminalInView) { + // Open using default keyboard shortcut + await this.focusTerminal() + await this.page.waitForSelector("#terminal") + } + } + + async focusTerminal() { + await this.page.keyboard.press("Control+Backquote") + } + + async quickOpen(input: string) { + await this.page.keyboard.press("Meta+P") + await this.page.waitForSelector('[aria-describedby="quickInput_message"]') + await this.page.keyboard.type(input) + await this.page.waitForTimeout(2000) + await this.page.keyboard.press("Enter") + await this.page.waitForTimeout(2000) + } +} diff --git a/test/e2e/terminal.test.ts b/test/e2e/terminal.test.ts new file mode 100644 index 000000000..a68f01bd1 --- /dev/null +++ b/test/e2e/terminal.test.ts @@ -0,0 +1,60 @@ +import { test, expect } from "@playwright/test" +import { STORAGE } from "../utils/constants" +import { CodeServer } from "./models/CodeServer" + +test.describe("Integrated Terminal", () => { + // Create a new context with the saved storage state + // so we don't have to logged in + const options: any = {} + const testFileName = "hello.txt" + const testString = "new string test from e2e test" + let codeServer: CodeServer + + // TODO@jsjoeio + // Fix this once https://github.com/microsoft/playwright-test/issues/240 + // is fixed + if (STORAGE) { + const storageState = JSON.parse(STORAGE) || {} + options.contextOptions = { + storageState, + } + } + test.beforeEach(async ({ page }) => { + codeServer = new CodeServer(page) + await codeServer.navigate() + }) + + test("should echo a string to a file", options, async ({ page }) => { + // Open the default folder + await codeServer.openFolder() + + // Open terminal and type in value + await codeServer.viewTerminal() + await codeServer.focusTerminal() + + await page.keyboard.type(`echo '${testString}' >> ${testFileName}`) + await page.keyboard.press("Enter") + await page.waitForTimeout(2000) + // It should show up on the left sidebar as a new file + const isFileVisible = await page.isVisible(`text="${testFileName}"`) + expect(isFileVisible).toBe(true) + + if (isFileVisible) { + // Check that the file has the test string in it + await codeServer.quickOpen(testFileName) + expect(await page.isVisible(`text="${testString}"`)).toBe(true) + + // Clean up + // Remove file + await codeServer.focusTerminal() + await page.keyboard.type(`rm ${testFileName}`) + await page.keyboard.press("Enter") + await page.waitForTimeout(2000) + // Close the file from workbench + // otherwise it will still be visible + // and our assertion will fail + await page.keyboard.press(`Meta+W`) + expect(await page.isVisible(`text="${testString}"`)).toBe(false) + } + }) +})