UNPKG

systelab-components-wdio-test

Version:
102 lines (101 loc) 3.85 kB
import { AutomationEnvironment } from "../../wdio/index.js"; import { HttpStatus } from "../server/http-status.js"; import fs from "fs"; export class BrowserRemote { static async navigateToURL(url) { await this.executeEndpoint('POST', 'navigate', { url }); } static async pressEsc() { await this.executeEndpoint('POST', 'keyboard/escape'); } static async pressTab() { await this.executeEndpoint('POST', 'keyboard/tab'); } static async pressBackspace() { await this.executeEndpoint('POST', 'keyboard/backspace'); } static async pressEnter() { await this.executeEndpoint('POST', 'keyboard/enter'); } static async pressDelete() { await this.executeEndpoint('POST', 'keyboard/delete'); } static async writeText(stringToWrite) { await this.executeEndpoint('POST', 'keyboard/text', { stringToWrite }); } static async waitUntil(condition, timeout) { const startTime = Date.now(); while (Date.now() - startTime < timeout) { const satisfied = await condition(); if (satisfied) { return; } } throw new Error('Timeout: condition not satisfied'); } static async takeScreenshot() { const response = await this.executeEndpoint('POST', 'screenshot'); const body = await response.json(); return body.screenshot; } static async saveScreenshot(filepath) { const screenshot = await this.takeScreenshot(); const base64Data = screenshot.replace(/^data:image\/png;base64,/, ''); const buffer = Buffer.from(base64Data, 'base64'); fs.writeFileSync(filepath, buffer); } static async getName() { const response = await this.executeEndpoint('GET', 'name'); const body = await response.json(); return body.name; } static async getVersion() { const response = await this.executeEndpoint('GET', 'version'); const body = await response.json(); return body.version; } static async getOperatingSystem() { const response = await this.executeEndpoint('GET', 'os'); const body = await response.json(); return body.os; } static async getWindowSize() { const response = await this.executeEndpoint('GET', 'size'); const body = await response.json(); return body; } static async setWindowSize(width, height) { await this.executeEndpoint('POST', 'window/size', { width, height }); } static async setFullscreen() { await this.executeEndpoint('POST', 'window/fullscreen'); } static async executeEndpoint(method, route, body = {}) { const remoteApplication = AutomationEnvironment.getWorkingRemoteApplication(); const hostname = remoteApplication.host.name; const port = remoteApplication.host.port; const apiPrefix = remoteApplication.host.apiPrefix; const applicationId = remoteApplication.remoteId; const baseURL = `http://${hostname}:${port}/${apiPrefix}/applications/${applicationId}`; const endpointURL = `${baseURL}/${route}`; let response; if (method === 'POST') { response = await fetch(endpointURL, { method, headers: { 'content-type': 'application/json' }, body: JSON.stringify(body), }); } else { response = await fetch(endpointURL, { method, headers: { 'content-type': 'application/json' }, }); } if (response.status === HttpStatus.INTERNAL_SERVER_ERROR) { const errorMessage = response.body.error; throw new Error('Error: ' + errorMessage); } return response; } }