|
| 1 | +import * as path from "node:path"; |
| 2 | + |
| 3 | +import { test as base, expect, chromium } from '@playwright/test'; |
| 4 | + |
| 5 | +import {GenericContainer} from "testcontainers"; |
| 6 | +import {HttpWaitStrategy} from "testcontainers/build/wait-strategies/http-wait-strategy"; |
| 7 | + |
| 8 | +import {CodeServer} from "./models/codeserver" |
| 9 | + |
| 10 | +import {setupTestcontainers} from "./testcontainers"; |
| 11 | + |
| 12 | +import * as utils from './utils' |
| 13 | + |
| 14 | +// Declare the types of your fixtures. |
| 15 | +type MyFixtures = { |
| 16 | + connectCDP: false | number; |
| 17 | + codeServerSource: {url?: string, image?: string}; |
| 18 | + codeServer: CodeServer |
| 19 | +}; |
| 20 | +const test = base.extend<MyFixtures>({ |
| 21 | + connectCDP: [false, {option: true}], |
| 22 | + codeServerSource: [{url:'http://localhost:8787'}, {option: true}], |
| 23 | + page: async ({ page, connectCDP }, use) => { |
| 24 | + if (!connectCDP) { |
| 25 | + await use(page) |
| 26 | + } else { |
| 27 | + // we close the provided page and send onwards our own |
| 28 | + await page.close() |
| 29 | + { |
| 30 | + const browser = await chromium.connectOverCDP(`http://localhost:${connectCDP}`); |
| 31 | + const defaultContext = browser.contexts()[0]; |
| 32 | + const page = defaultContext.pages()[0]; |
| 33 | + await use(page) |
| 34 | + } |
| 35 | + } |
| 36 | + }, |
| 37 | + codeServer: [async ({ page, codeServerSource }, use) => { |
| 38 | + if (codeServerSource?.url) { |
| 39 | + await use(new CodeServer(page, codeServerSource.url)) |
| 40 | + } else { |
| 41 | + const image = codeServerSource.image ?? (() => { |
| 42 | + throw new Error("invalid config: codeserver image not specified") |
| 43 | + })() |
| 44 | + const container = await new GenericContainer(image) |
| 45 | + .withExposedPorts(8787) |
| 46 | + .withWaitStrategy(new HttpWaitStrategy('/', 8787, {abortOnContainerExit: true})) |
| 47 | + .start(); |
| 48 | + await use(new CodeServer(page, `http://${container.getHost()}:${container.getMappedPort(8787)}`)) |
| 49 | + await container.stop() |
| 50 | + } |
| 51 | + }, {timeout: 10 * 60 * 1000}], |
| 52 | +}); |
| 53 | + |
| 54 | +test.beforeAll(setupTestcontainers) |
| 55 | + |
| 56 | +test('open codeserver', async ({codeServer, page}) => { |
| 57 | + await page.goto(codeServer.url) |
| 58 | + |
| 59 | + await codeServer.isEditorVisible() |
| 60 | +}) |
| 61 | + |
| 62 | +test('wait for welcome screen to load', async ({codeServer, page}, testInfo) => { |
| 63 | + await page.goto(codeServer.url); |
| 64 | + |
| 65 | + await codeServer.isEditorVisible() |
| 66 | + page.on("console", console.log) |
| 67 | + |
| 68 | + await codeServer.isEditorVisible() |
| 69 | + await utils.waitForStableDOM(page, "div.monaco-workbench", 1000, 10000) |
| 70 | + await utils.waitForNextRender(page) |
| 71 | + |
| 72 | + await utils.takeScreenshot(page, testInfo, "welcome.png") |
| 73 | +}) |
| 74 | + |
| 75 | +test('use the terminal to run command', async ({codeServer, page}, testInfo) => { |
| 76 | + await page.goto(codeServer.url); |
| 77 | + |
| 78 | + await test.step("Should always see the code-server editor", async () => { |
| 79 | + expect(await codeServer.isEditorVisible()).toBe(true) |
| 80 | + }) |
| 81 | + |
| 82 | + await test.step("should show the Integrated Terminal", async () => { |
| 83 | + await codeServer.focusTerminal() |
| 84 | + expect(await page.isVisible("#terminal")).toBe(true) |
| 85 | + }) |
| 86 | + |
| 87 | + await test.step("should execute Terminal command successfully", async () => { |
| 88 | + await page.keyboard.type('echo The answer is $(( 6 * 7 )). > answer.txt', {delay: 100}) |
| 89 | + await page.keyboard.press('Enter', {delay: 100}) |
| 90 | + }) |
| 91 | + |
| 92 | + await test.step("should open the file", async() => { |
| 93 | + const file = path.join('/opt/app-root/src', 'answer.txt') |
| 94 | + await codeServer.openFile(file) |
| 95 | + await expect(page.getByText("The answer is 42.")).toBeVisible() |
| 96 | + }) |
| 97 | + |
| 98 | +}) |
0 commit comments