2017-10-22 00:39:17 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
// The geckodriver package downloads and installs geckodriver for us.
|
|
|
|
// We use it by requiring it.
|
|
|
|
require("geckodriver");
|
|
|
|
|
|
|
|
const firefox = require("selenium-webdriver/firefox");
|
|
|
|
const webdriver = require("selenium-webdriver");
|
|
|
|
const injectedScripts = require("./injected");
|
2017-10-22 05:40:39 +00:00
|
|
|
const by = webdriver.By;
|
2017-10-22 00:39:17 +00:00
|
|
|
const Context = firefox.Context;
|
|
|
|
const until = webdriver.until;
|
2017-10-22 05:40:39 +00:00
|
|
|
const keys = webdriver.Key;
|
2017-10-22 00:39:17 +00:00
|
|
|
const path = require("path");
|
|
|
|
|
2017-10-22 05:40:39 +00:00
|
|
|
module.exports.constants = {
|
|
|
|
svgBlockRule: ["Scalable Vector Graphics (SVG) 1.1 (Second Edition)"]
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.pause = function (ms = 2000) {
|
|
|
|
return new Promise(function (resolve) {
|
2017-10-22 07:07:22 +00:00
|
|
|
setTimeout(resolve, ms);
|
2017-10-22 05:40:39 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.promiseSetFormAndSubmit = function (driver, values) {
|
|
|
|
driver.setContext(Context.CONTENT);
|
|
|
|
const numberOfPairs = values.length;
|
|
|
|
|
|
|
|
const setFormValue = function (index = 0) {
|
|
|
|
|
|
|
|
const [name, value] = values[index];
|
|
|
|
|
|
|
|
if (index === numberOfPairs - 1) {
|
|
|
|
|
|
|
|
return driver.findElement(by.name(name))
|
|
|
|
.sendKeys(value, keys.ENTER);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return driver.findElement(by.name(name))
|
|
|
|
.sendKeys(value)
|
|
|
|
.then(() => setFormValue(index + 1));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return setFormValue();
|
|
|
|
};
|
|
|
|
|
2017-10-22 00:39:17 +00:00
|
|
|
module.exports.promiseAddonButton = function (driver) {
|
|
|
|
driver.setContext(Context.CHROME);
|
2017-10-23 08:29:59 +00:00
|
|
|
return driver.wait(until.elementLocated(by.css("[tooltiptext='WebAPI Manager']")), 2000);
|
2017-10-22 00:39:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.promiseExtensionConfigPage = function (driver) {
|
|
|
|
const extensionIdPattern = /url\("moz-extension:\/\/(.*?)\/images/;
|
|
|
|
return this.promiseAddonButton(driver)
|
|
|
|
.then(button => button.getAttribute("style"))
|
|
|
|
.then(function (buttonStyle) {
|
|
|
|
const match = extensionIdPattern.exec(buttonStyle);
|
|
|
|
const extensionId = match[1];
|
|
|
|
driver.setContext(Context.CONTENT);
|
|
|
|
return driver.get(`moz-extension://${extensionId}/config/index.html`);
|
2017-10-22 07:07:22 +00:00
|
|
|
});
|
2017-10-22 00:39:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.promiseAddonConfigButton = function (driver) {
|
|
|
|
driver.setContext(Context.CHROME);
|
2017-10-23 08:29:59 +00:00
|
|
|
return driver.wait(until.elementLocated(by.id("config-page-link")), 2000);
|
2017-10-22 00:39:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.promiseSetBlockingRules = function (driver, standardsToBlock) {
|
|
|
|
const setStandardsScript = injectedScripts.setStandardsAsBlockedScript(standardsToBlock);
|
|
|
|
driver.setContext(Context.CONTENT);
|
|
|
|
|
|
|
|
return this.promiseExtensionConfigPage(driver)
|
|
|
|
.then(driver.executeAsyncScript(setStandardsScript));
|
|
|
|
};
|
|
|
|
|
2017-10-22 05:40:39 +00:00
|
|
|
module.exports.promiseGetDriver = function () {
|
2017-10-22 00:39:17 +00:00
|
|
|
|
2017-10-23 08:29:59 +00:00
|
|
|
const driver = new webdriver.Builder()
|
|
|
|
.forBrowser("firefox")
|
2017-10-22 00:39:17 +00:00
|
|
|
.build();
|
|
|
|
|
|
|
|
driver.setContext(Context.CHROME);
|
|
|
|
|
2017-10-23 08:29:59 +00:00
|
|
|
const fileLocation = path.join(process.cwd(), "dist", "webapi_manager.zip");
|
2017-10-22 00:39:17 +00:00
|
|
|
|
|
|
|
// This manually installs the add-on as a temporary add-on.
|
|
|
|
// Hopefully selenium/geckodriver will get a way to do this soon:
|
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=1298025
|
2017-10-23 08:29:59 +00:00
|
|
|
const installAddOnPromise = driver.executeAsyncScript(
|
2017-10-22 00:39:17 +00:00
|
|
|
injectedScripts.temporaryAddOnInstallScript(),
|
|
|
|
fileLocation
|
|
|
|
);
|
|
|
|
|
|
|
|
return installAddOnPromise
|
|
|
|
.then(function (result) {
|
|
|
|
if (!result[0] && result[1]) {
|
|
|
|
return driver.quit().then(() => {
|
|
|
|
throw new Error(`Failed to install add-on: ${result[1]}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-22 05:40:39 +00:00
|
|
|
driver.setContext(Context.CONTENT);
|
2017-10-22 00:39:17 +00:00
|
|
|
return Promise.resolve(driver, result[0]);
|
|
|
|
});
|
2017-10-23 08:29:59 +00:00
|
|
|
};
|