@assert-equals/dappdriver
Version:
DappDriver is an e2e testing framework designed for testing decentralized applications (dApps) using MetaMask, Rainbow or Zerion
116 lines (115 loc) • 3.84 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebDriverHTMLElement = void 0;
const selenium_webdriver_1 = require("selenium-webdriver");
const log_1 = require("../log");
const page_1 = require("../page");
const dapp_driver_1 = require("../session/dapp-driver");
class WebDriverHTMLElement {
cssLocator;
driver;
webElement;
element;
timeout;
constructor(cssLocator, timeout = 20000, element = null) {
this.cssLocator = cssLocator;
this.driver = dapp_driver_1.DappDriver.Instance.Driver;
this.element = element;
this.timeout = timeout;
this.webElement = null;
}
async search() {
await this.driver.manage().setTimeouts({ implicit: this.timeout });
if (this.cssLocator.startsWith('xpath=')) {
this.webElement = !(await this.element)
? await this.driver.findElement({ xpath: this.cssLocator.substring(6, this.cssLocator.length) })
: await this.element;
}
else {
this.webElement = !(await this.element)
? await this.driver.findElement({ css: this.cssLocator })
: await this.element;
}
await this.driver.manage().setTimeouts({ implicit: 20000 });
}
async hardClick() {
await this.search();
await this.driver.wait(selenium_webdriver_1.until.elementIsVisible(this.webElement), 10000);
await this.driver.wait(selenium_webdriver_1.until.elementIsEnabled(this.webElement), 10000);
await this.webElement.click();
}
async click(page) {
try {
await this.hardClick();
}
catch (err) {
if (err instanceof selenium_webdriver_1.error.StaleElementReferenceError) {
(0, log_1.logWarning)(err.name + ': ' + this.cssLocator);
await this.hardClick();
}
}
if (page) {
return await dapp_driver_1.DappDriver.getPage(page);
}
}
async clickAndWait(duration) {
await this.click();
return await dapp_driver_1.DappDriver.sleep(duration);
}
async clickAndOpensInNewWindow(page) {
await this.click();
await new page_1.PageObject().opensInNewWindow();
if (page) {
return await dapp_driver_1.DappDriver.getPage(page);
}
}
async clickAndOpensInWindow(page) {
await this.click();
return await new page_1.PageObject().opensInWindow(page);
}
async clickAndSwitchToMainWindow(page) {
await this.click();
await new page_1.PageObject().switchToMainWindow();
if (page) {
return await dapp_driver_1.DappDriver.getPage(page);
}
}
async getAttribute(attribute) {
await this.search();
return await this.webElement.getAttribute(attribute);
}
async getCssValue(property) {
await this.search();
return await this.webElement.getCssValue(property);
}
async getText() {
await this.search();
return await this.webElement.getText();
}
async hover() {
await this.search();
return await this.driver.actions({ async: true }).move({ origin: this.webElement }).perform();
}
async isDisplayed() {
try {
await this.search();
return true;
}
catch (ex) {
return false;
}
}
async isEnabled() {
await this.search();
return await this.webElement.isEnabled();
}
async isVisible() {
await this.search();
return await this.webElement.isDisplayed();
}
async type(keys) {
await this.search();
await this.webElement.sendKeys(keys);
}
}
exports.WebDriverHTMLElement = WebDriverHTMLElement;