|
| 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 | + |
| 13 | + /** |
| 14 | + * Navigates to CODE_SERVER_ADDRESS |
| 15 | + */ |
| 16 | + async navigate() { |
| 17 | + await this.page.goto(CODE_SERVER_ADDRESS, { waitUntil: "networkidle" }) |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Checks if the editor is visible |
| 22 | + * and reloads until it is |
| 23 | + */ |
| 24 | + async reloadUntilEditorIsVisible() { |
| 25 | + const editorIsVisible = await this.isEditorVisible() |
| 26 | + let reloadCount = 0 |
| 27 | + |
| 28 | + // Occassionally code-server timeouts in Firefox |
| 29 | + // we're not sure why |
| 30 | + // but usually a reload or two fixes it |
| 31 | + // TODO@jsjoeio @oxy look into Firefox reconnection/timeout issues |
| 32 | + while (!editorIsVisible) { |
| 33 | + reloadCount += 1 |
| 34 | + if (await this.isEditorVisible()) { |
| 35 | + console.log(` Editor became visible after ${reloadCount} reloads`) |
| 36 | + break |
| 37 | + } |
| 38 | + // When a reload happens, we want to wait for all resources to be |
| 39 | + // loaded completely. Hence why we use that instead of DOMContentLoaded |
| 40 | + // Read more: https://thisthat.dev/dom-content-loaded-vs-load/ |
| 41 | + await this.page.reload({ waitUntil: "load" }) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Checks if the editor is visible |
| 47 | + */ |
| 48 | + async isEditorVisible() { |
| 49 | + // Make sure the editor actually loaded |
| 50 | + // If it's not visible after 5 seconds, something is wrong |
| 51 | + await this.page.waitForLoadState("networkidle") |
| 52 | + return await this.page.isVisible("div.monaco-workbench", { timeout: 5000 }) |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Focuses Integrated Terminal |
| 57 | + * by going to the Application Menu |
| 58 | + * and clicking View > Terminal |
| 59 | + */ |
| 60 | + async focusTerminal() { |
| 61 | + // If the terminal is already visible |
| 62 | + // then we can focus it by hitting the keyboard shortcut |
| 63 | + const isTerminalVisible = await this.page.isVisible("#terminal") |
| 64 | + if (isTerminalVisible) { |
| 65 | + await this.page.keyboard.press(`Control+Backquote`) |
| 66 | + // Wait for terminal to receive focus |
| 67 | + await this.page.waitForSelector("div.terminal.xterm.focus") |
| 68 | + // Sometimes the terminal reloads |
| 69 | + // which is why we wait for it twice |
| 70 | + await this.page.waitForSelector("div.terminal.xterm.focus") |
| 71 | + return |
| 72 | + } |
| 73 | + // Open using the manu |
| 74 | + // Click [aria-label="Application Menu"] div[role="none"] |
| 75 | + await this.page.click('[aria-label="Application Menu"] div[role="none"]') |
| 76 | + |
| 77 | + // Click text=View |
| 78 | + await this.page.hover("text=View") |
| 79 | + await this.page.click("text=View") |
| 80 | + |
| 81 | + // Click text=Terminal |
| 82 | + await this.page.hover("text=Terminal") |
| 83 | + await this.page.click("text=Terminal") |
| 84 | + |
| 85 | + // Wait for terminal to receive focus |
| 86 | + // Sometimes the terminal reloads once or twice |
| 87 | + // which is why we wait for it to have the focus class |
| 88 | + await this.page.waitForSelector("div.terminal.xterm.focus") |
| 89 | + // Sometimes the terminal reloads |
| 90 | + // which is why we wait for it twice |
| 91 | + await this.page.waitForSelector("div.terminal.xterm.focus") |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Navigates to CODE_SERVER_ADDRESS |
| 96 | + * and reloads until the editor is visible |
| 97 | + * |
| 98 | + * Helpful for running before tests |
| 99 | + */ |
| 100 | + async setup() { |
| 101 | + await this.navigate() |
| 102 | + await this.reloadUntilEditorIsVisible() |
| 103 | + } |
| 104 | +} |
0 commit comments