mailtrain/test/e2e/page-objects/page.js

82 lines
2.1 KiB
JavaScript
Raw Normal View History

2017-05-09 23:40:02 +00:00
'use strict';
const config = require('../helpers/config');
const webdriver = require('selenium-webdriver');
const By = webdriver.By;
const until = webdriver.until;
const fs = require('fs-extra');
2017-05-09 23:40:02 +00:00
module.exports = (driver, ...extras) => Object.assign({
driver,
elements: {},
2017-05-09 23:40:02 +00:00
async element(key) {
return await this.driver.findElement(By.css(this.elements[key] || key));
},
2017-05-09 23:40:02 +00:00
async waitUntilVisible(selector) {
// This is left here to ease debugging
// await this.sleep(2000);
// await this.takeScreenshot('image.png');
// console.log(await this.source());
const sel = selector || this.elements[this.elementToWaitFor] || 'body';
await this.driver.wait(until.elementLocated(By.css(sel)), 10000);
2017-05-09 23:40:02 +00:00
if (this.url) {
await this.ensureUrl();
2017-05-09 23:40:02 +00:00
}
},
2017-05-09 23:40:02 +00:00
async link(key) {
const elem = await this.element(key);
return await elem.getAttribute('href');
},
2017-05-09 23:40:02 +00:00
async submit() {
const submitButton = await this.element('submitButton');
await submitButton.click();
},
2017-05-09 23:40:02 +00:00
async click(key) {
const elem = await this.element(key);
await elem.click();
},
2017-05-09 23:40:02 +00:00
async getText(key) {
const elem = await this.element(key);
return await elem.getText();
},
2017-05-09 23:40:02 +00:00
async getValue(key) {
const elem = await this.element(key);
return await elem.getAttribute('value');
},
2017-05-09 23:40:02 +00:00
async setValue(key, value) {
const elem = await this.element(key);
await elem.sendKeys(value);
},
async containsText(str) {
return await this.driver.executeScript(`
2017-05-09 23:40:02 +00:00
return (document.documentElement.textContent || document.documentElement.innerText).indexOf('${str}') > -1;
`);
},
async source() {
return await this.driver.getPageSource();
},
async takeScreenshot(destPath) {
const pngData = await this.driver.takeScreenshot();
const buf = new Buffer(pngData, 'base64');
await fs.writeFile(destPath, buf);
},
async sleep(ms) {
await this.driver.sleep(ms);
2017-05-09 23:40:02 +00:00
}
}, ...extras);