|
| 1 | +import { test as base, type Page } from '@playwright/test'; |
| 2 | +import type { Robot, RobotClient } from '../../src/robot'; |
| 3 | +import type { ResolvedReturnType } from '../helpers/api-types'; |
| 4 | + |
| 5 | +export class RobotPage { |
| 6 | + private readonly connectionStatusID = 'connection-status'; |
| 7 | + private readonly dialingStatusID = 'dialing-status'; |
| 8 | + private readonly connectButtonID = 'connect-btn'; |
| 9 | + private readonly disconnectButtonID = 'disconnect-btn'; |
| 10 | + private readonly outputID = 'output'; |
| 11 | + |
| 12 | + constructor(private readonly page: Page) {} |
| 13 | + |
| 14 | + async ensureReady(): Promise<void> { |
| 15 | + if (!this.page.url().includes('localhost:5173')) { |
| 16 | + await this.page.goto('/'); |
| 17 | + await this.page.waitForSelector('body[data-ready="true"]'); |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + async connect(): Promise<void> { |
| 22 | + await this.ensureReady(); |
| 23 | + await this.page.getByTestId(this.connectButtonID).click(); |
| 24 | + await this.page.waitForSelector( |
| 25 | + `[data-testid="${this.connectionStatusID}"]:is(:text("Connected"))` |
| 26 | + ); |
| 27 | + } |
| 28 | + |
| 29 | + async disconnect(): Promise<void> { |
| 30 | + await this.page.getByTestId(this.disconnectButtonID).click(); |
| 31 | + await this.page.waitForSelector( |
| 32 | + `[data-testid="${this.connectionStatusID}"]:is(:text("Disconnected"))` |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + async getConnectionStatus(): Promise<string> { |
| 37 | + const connectionStatusEl = this.page.getByTestId(this.connectionStatusID); |
| 38 | + const text = await connectionStatusEl.textContent(); |
| 39 | + return text ?? 'Unknown'; |
| 40 | + } |
| 41 | + |
| 42 | + async waitForDialing(): Promise<void> { |
| 43 | + await this.page.waitForSelector( |
| 44 | + `[data-testid="${this.dialingStatusID}"]:not(:empty)`, |
| 45 | + { timeout: 5000 } |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + async waitForFirstDialingAttempt(): Promise<void> { |
| 50 | + await this.page.waitForFunction( |
| 51 | + (testId: string) => { |
| 52 | + const el = document.querySelector(`[data-testid="${testId}"]`); |
| 53 | + const text = el?.textContent ?? ''; |
| 54 | + const match = text.match(/attempt (?<attemptNumber>\d+)/u); |
| 55 | + if (!match?.groups) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + const attemptNumber = Number.parseInt( |
| 59 | + match.groups.attemptNumber ?? '0', |
| 60 | + 10 |
| 61 | + ); |
| 62 | + return attemptNumber === 1; |
| 63 | + }, |
| 64 | + this.dialingStatusID, |
| 65 | + { timeout: 10_000 } |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + async waitForSubsequentDialingAttempts(): Promise<void> { |
| 70 | + await this.page.waitForFunction( |
| 71 | + (testId: string) => { |
| 72 | + const el = document.querySelector(`[data-testid="${testId}"]`); |
| 73 | + const text = el?.textContent ?? ''; |
| 74 | + const match = text.match(/attempt (?<attemptNumber>\d+)/u); |
| 75 | + if (!match?.groups) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + const attemptNumber = Number.parseInt( |
| 79 | + match.groups.attemptNumber ?? '0', |
| 80 | + 10 |
| 81 | + ); |
| 82 | + return attemptNumber > 1; |
| 83 | + }, |
| 84 | + this.dialingStatusID, |
| 85 | + { timeout: 10_000 } |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + async getDialingStatus(): Promise<string> { |
| 90 | + const dialingStatusEl = this.page.getByTestId(this.dialingStatusID); |
| 91 | + const text = await dialingStatusEl.textContent(); |
| 92 | + return text ?? ''; |
| 93 | + } |
| 94 | + |
| 95 | + async getOutput<T extends Robot, K extends keyof T>(): Promise< |
| 96 | + ResolvedReturnType<T[K]> |
| 97 | + > { |
| 98 | + // Wait for the output to be updated by checking for the data-has-output attribute |
| 99 | + await this.page.waitForSelector( |
| 100 | + `[data-testid="${this.outputID}"][data-has-output="true"]`, |
| 101 | + { timeout: 30_000 } |
| 102 | + ); |
| 103 | + const outputEl = this.page.getByTestId(this.outputID); |
| 104 | + const text = await outputEl.textContent(); |
| 105 | + return JSON.parse(text ?? '{}') as ResolvedReturnType<T[K]>; |
| 106 | + } |
| 107 | + |
| 108 | + async clickButton(testId: string): Promise<void> { |
| 109 | + await this.page.click(`[data-testid="${testId}"]`); |
| 110 | + } |
| 111 | + |
| 112 | + async clickRobotAPIButton(apiName: keyof RobotClient): Promise<void> { |
| 113 | + await this.page.click(`[data-robot-api="${apiName}"]`); |
| 114 | + } |
| 115 | + |
| 116 | + getPage(): Page { |
| 117 | + return this.page; |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +export const withRobot = base.extend<{ robotPage: RobotPage }>({ |
| 122 | + robotPage: async ({ page }, use) => { |
| 123 | + const robotPage = new RobotPage(page); |
| 124 | + await use(robotPage); |
| 125 | + }, |
| 126 | +}); |
0 commit comments