UNPKG

systelab-components-wdio-test

Version:
181 lines (180 loc) 6.72 kB
import * as tmp from "tmp"; import * as fs from "fs"; import { AutomationEnvironment } from "./automation-environment.js"; import { LocatorType } from "./locator.js"; import { ElementArrayFinder, ElementFinder } from "./element-finder.js"; import { DefaultTimeout } from "./default-timeout.js"; import { Constants } from "../constants.js"; import { BrowserRemote } from "../remote/client/browser-remote.js"; export class Browser { static async navigateToURL(url) { if (AutomationEnvironment.isLocalMode()) { await AutomationEnvironment.getWorkingBrowser().url(url); } else { await BrowserRemote.navigateToURL(url); } } static async pressEsc() { if (AutomationEnvironment.isLocalMode()) { return AutomationEnvironment.getWorkingBrowser().keys(['Escape']); } else { return BrowserRemote.pressEsc(); } } static async pressTab() { if (AutomationEnvironment.isLocalMode()) { return AutomationEnvironment.getWorkingBrowser().keys(['Tab']); } else { return BrowserRemote.pressTab(); } } static async pressBackspace() { if (AutomationEnvironment.isLocalMode()) { return AutomationEnvironment.getWorkingBrowser().keys(['Backspace']); } else { return BrowserRemote.pressBackspace(); } } static async pressEnter() { if (AutomationEnvironment.isLocalMode()) { return AutomationEnvironment.getWorkingBrowser().keys(['Enter']); } else { return BrowserRemote.pressEnter(); } } static async pressDelete() { if (AutomationEnvironment.isLocalMode()) { return AutomationEnvironment.getWorkingBrowser().keys(['Delete']); } else { return BrowserRemote.pressDelete(); } } static async writeText(stringToWrite) { if (AutomationEnvironment.isLocalMode()) { return AutomationEnvironment.getWorkingBrowser().keys(stringToWrite.split('')); } else { return BrowserRemote.writeText(stringToWrite); } } static byId(id) { return new ElementFinder({ type: LocatorType.ElementSelector, selector: `#${id}` }); } static byTagName(tagName) { return new ElementFinder({ type: LocatorType.ElementSelector, selector: `<${tagName}>` }); } static byClassName(className) { return new ElementFinder({ type: LocatorType.ElementSelector, selector: `.${className}` }); } static byCSS(cssExpression) { return new ElementFinder({ type: LocatorType.ElementSelector, selector: cssExpression }); } static byButtonText(text) { return this.byElementText('button', text); } static byElementText(tagName, text) { return new ElementFinder({ type: LocatorType.ElementSelector, selector: `${tagName}*=${text}` }); } static bySystelabTestId(systelabTestId) { return new ElementFinder({ type: LocatorType.ElementSelector, selector: `[${Constants.SYSTELAB_TEST_ID_ATTRIBUTE}="${systelabTestId}"]` }); } static allByTagName(tagName) { return new ElementArrayFinder({ type: LocatorType.ArraySelector, selector: `<${tagName}>` }); } static allByClassName(className) { return new ElementArrayFinder({ type: LocatorType.ArraySelector, selector: `.${className}` }); } static allByCSS(cssExpression) { return new ElementArrayFinder({ type: LocatorType.ArraySelector, selector: cssExpression }); } static allBySystelabTestId(systelabTestId) { return new ElementArrayFinder({ type: LocatorType.ArraySelector, selector: `[${Constants.SYSTELAB_TEST_ID_ATTRIBUTE}="${systelabTestId}"]` }); } static async sleep(duration) { await AutomationEnvironment.getWorkingBrowser().pause(duration); } static async waitUntil(condition, timeout = DefaultTimeout.SLOW_WAIT) { if (AutomationEnvironment.isLocalMode()) { await AutomationEnvironment.getWorkingBrowser().waitUntil(condition, { timeout }); } else { await BrowserRemote.waitUntil(condition, timeout); } } static async takeScreenshot() { if (AutomationEnvironment.isLocalMode()) { const tempFilepath = tmp.tmpNameSync({ postfix: '.png' }); const screenshotBuffer = await AutomationEnvironment.getWorkingBrowser().saveScreenshot(tempFilepath); fs.unlinkSync(tempFilepath); return screenshotBuffer.toString('base64'); } else { return BrowserRemote.takeScreenshot(); } } static async saveScreenshot(filepath) { if (AutomationEnvironment.isLocalMode()) { await AutomationEnvironment.getWorkingBrowser().saveScreenshot(filepath); } else { await BrowserRemote.saveScreenshot(filepath); } } static async getName() { if (AutomationEnvironment.isLocalMode()) { const caps = AutomationEnvironment.getWorkingBrowser().capabilities; return caps.browserName; } else { return BrowserRemote.getName(); } } static async getVersion() { if (AutomationEnvironment.isLocalMode()) { const caps = AutomationEnvironment.getWorkingBrowser().capabilities; return caps.browserVersion; } else { return BrowserRemote.getVersion(); } } static async getOperatingSystem() { if (AutomationEnvironment.isLocalMode()) { const caps = AutomationEnvironment.getWorkingBrowser().capabilities; return caps.platformName; } else { return BrowserRemote.getOperatingSystem(); } } static async getWindowSize() { if (AutomationEnvironment.isLocalMode()) { return await AutomationEnvironment.getWorkingBrowser().getWindowSize(); } else { return BrowserRemote.getWindowSize(); } } static async setWindowSize(width, height) { if (AutomationEnvironment.isLocalMode()) { await AutomationEnvironment.getWorkingBrowser().setWindowSize(width, height); } else { await BrowserRemote.setWindowSize(width, height); } } static async setFullscreen() { if (AutomationEnvironment.isLocalMode()) { await AutomationEnvironment.getWorkingBrowser().fullscreenWindow(); } else { await BrowserRemote.setFullscreen(); } } }