UNPKG

systelab-components-wdio-test

Version:
168 lines (167 loc) 7.08 kB
import fs from "fs"; import { HttpStatus } from "../server/http-status.js"; export class ElementFinderRemote { constructor(remoteApplication, locators) { this.remoteApplication = remoteApplication; this.locators = locators; } getLocators() { return this.locators; } async isPresent() { const response = await this.executeEndpoint('POST', 'element/query/present', { locators: this.locators }); const body = await response.json(); return body.isPresent; } async isDisplayed() { const response = await this.executeEndpoint('POST', 'element/query/displayed', { locators: this.locators }); const body = await response.json(); return body.isDisplayed; } async isClickable() { const response = await this.executeEndpoint('POST', 'element/query/clickable', { locators: this.locators }); const body = await response.json(); return body.isClickable; } async isEnabled() { const response = await this.executeEndpoint('POST', 'element/query/enabled', { locators: this.locators }); const body = await response.json(); return body.isEnabled; } async isSelected() { const response = await this.executeEndpoint('POST', 'element/query/selected', { locators: this.locators }); const body = await response.json(); return body.isSelected; } async isFocused() { const response = await this.executeEndpoint('POST', 'element/query/focused', { locators: this.locators }); const body = await response.json(); return body.isFocused; } async getText() { const response = await this.executeEndpoint('POST', 'element/query/text', { locators: this.locators }); const body = await response.json(); return body.text; } async getValue() { const response = await this.executeEndpoint('POST', 'element/query/value', { locators: this.locators }); const body = await response.json(); return body.value; } async getHTML(includeSelectorTag) { const response = await this.executeEndpoint('POST', 'element/query/html', { locators: this.locators, includeSelectorTag }); const body = await response.json(); return body.html; } async getAttribute(name) { const response = await this.executeEndpoint('POST', 'element/query/attribute', { locators: this.locators, name }); const body = await response.json(); return body.attribute; } async getCSSProperty(name, pseudoElement) { const response = await this.executeEndpoint('POST', 'element/query/css-property', { locators: this.locators, name, pseudoElement }); const body = await response.json(); return body.property; } async getProperty(name) { const response = await this.executeEndpoint('POST', 'element/query/property', { locators: this.locators, name }); const body = await response.json(); return body.property; } async getBoundingRect() { const response = await this.executeEndpoint('POST', 'element/query/bounding-rect', { locators: this.locators }); const body = await response.json(); return body.boundingRect; } async getPosition() { const response = await this.executeEndpoint('POST', 'element/query/position', { locators: this.locators }); const body = await response.json(); return body.position; } async getSize() { const response = await this.executeEndpoint('POST', 'element/query/size', { locators: this.locators }); const body = await response.json(); return body.size; } async click() { await this.executeEndpoint('POST', 'element/action/click', { locators: this.locators }); } async moveTo() { await this.executeEndpoint('POST', 'element/action/move-to', { locators: this.locators }); } async clear() { await this.executeEndpoint('POST', 'element/action/clear', { locators: this.locators }); } async write(text) { await this.executeEndpoint('POST', 'element/action/write', { locators: this.locators, text }); } async tap() { await this.executeEndpoint('POST', 'element/action/tap', { locators: this.locators }); } async scrollToElement(options) { await this.executeEndpoint('POST', 'element/action/scroll', { locators: this.locators, options }); } async waitToBePresent(timeout = 500) { await this.executeEndpoint('POST', 'element/wait/present', { locators: this.locators }); } async waitToBeDisplayed(timeout = 500) { await this.executeEndpoint('POST', 'element/wait/displayed', { locators: this.locators }); } async waitToBeClickable(timeout = 500) { await this.executeEndpoint('POST', 'element/wait/clickable', { locators: this.locators }); } async waitToBeEnabled(timeout = 500) { await this.executeEndpoint('POST', 'element/wait/enabled', { locators: this.locators }); } async waitUntil(condition, timeout = 5000) { const startTime = Date.now(); while (Date.now() - startTime < timeout) { try { const satisfied = await condition(); if (satisfied) { return; } } catch (err) { } } throw new Error('Timeout: condition not satisfied'); } async takeScreenshot() { const response = await this.executeEndpoint('POST', 'element/screenshot', { locators: this.locators }); const body = await response.json(); return body.screenshot; } 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); } async count() { const response = await this.executeEndpoint('POST', 'element/count', { locators: this.locators }); const body = await response.json(); return body.count; } async executeEndpoint(method, route, body) { const hostname = this.remoteApplication.host.name; const port = this.remoteApplication.host.port; const apiPrefix = this.remoteApplication.host.apiPrefix; const applicationId = this.remoteApplication.remoteId; const baseURL = `http://${hostname}:${port}/${apiPrefix}/applications/${applicationId}`; const endpointURL = `${baseURL}/${route}`; const response = await fetch(endpointURL, { method, headers: { 'content-type': 'application/json' }, body: JSON.stringify(body), }); if (response.status === HttpStatus.INTERNAL_SERVER_ERROR) { const errorMessage = response.body.error; throw new Error('Error: ' + errorMessage); } return response; } }