2018-01-18 17:10:53 +01:00
|
|
|
require('chromedriver')
|
2018-05-01 21:18:22 +02:00
|
|
|
require('geckodriver')
|
2018-05-25 03:17:26 +02:00
|
|
|
const fs = require('fs')
|
|
|
|
const os = require('os')
|
2018-05-15 02:17:36 +02:00
|
|
|
const path = require('path')
|
2017-09-12 23:14:24 +02:00
|
|
|
const webdriver = require('selenium-webdriver')
|
2018-05-15 02:17:36 +02:00
|
|
|
const Command = require('selenium-webdriver/lib/command').Command
|
|
|
|
const By = webdriver.By
|
2017-09-12 23:14:24 +02:00
|
|
|
|
2018-05-15 02:17:36 +02:00
|
|
|
module.exports = {
|
|
|
|
delay,
|
|
|
|
buildChromeWebDriver,
|
|
|
|
buildFirefoxWebdriver,
|
|
|
|
installWebExt,
|
|
|
|
getExtensionIdChrome,
|
|
|
|
getExtensionIdFirefox,
|
2017-09-12 23:14:24 +02:00
|
|
|
}
|
|
|
|
|
2018-05-15 02:17:36 +02:00
|
|
|
function delay (time) {
|
|
|
|
return new Promise(resolve => setTimeout(resolve, time))
|
|
|
|
}
|
2017-09-12 23:14:24 +02:00
|
|
|
|
2018-05-15 02:17:36 +02:00
|
|
|
function buildChromeWebDriver (extPath) {
|
2018-05-25 03:17:26 +02:00
|
|
|
const tmpProfile = path.join(os.tmpdir(), fs.mkdtempSync('mm-chrome-profile'));
|
2017-09-12 23:14:24 +02:00
|
|
|
return new webdriver.Builder()
|
|
|
|
.withCapabilities({
|
|
|
|
chromeOptions: {
|
2018-05-25 03:17:26 +02:00
|
|
|
args: [
|
|
|
|
`load-extension=${extPath}`,
|
|
|
|
`user-data-dir=${tmpProfile}`,
|
|
|
|
],
|
|
|
|
binary: process.env.SELENIUM_CHROME_BINARY,
|
2017-09-12 23:14:24 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
.build()
|
|
|
|
}
|
2018-05-01 21:18:22 +02:00
|
|
|
|
2018-05-15 02:17:36 +02:00
|
|
|
function buildFirefoxWebdriver () {
|
2018-05-01 21:18:22 +02:00
|
|
|
return new webdriver.Builder().build()
|
|
|
|
}
|
2018-05-15 02:17:36 +02:00
|
|
|
|
|
|
|
async function getExtensionIdChrome (driver) {
|
|
|
|
await driver.get('chrome://extensions')
|
|
|
|
const extensionId = await driver.executeScript('return document.querySelector("extensions-manager").shadowRoot.querySelector("extensions-view-manager extensions-item-list").shadowRoot.querySelector("extensions-item:nth-child(2)").getAttribute("id")')
|
|
|
|
return extensionId
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getExtensionIdFirefox (driver) {
|
|
|
|
await driver.get('about:debugging#addons')
|
|
|
|
const extensionId = await driver.findElement(By.css('dd.addon-target-info-content:nth-child(6) > span:nth-child(1)')).getText()
|
|
|
|
return extensionId
|
|
|
|
}
|
|
|
|
|
|
|
|
async function installWebExt (driver, extension) {
|
|
|
|
const cmd = await new Command('moz-install-web-ext')
|
|
|
|
.setParameter('path', path.resolve(extension))
|
|
|
|
.setParameter('temporary', true)
|
|
|
|
|
|
|
|
await driver.getExecutor()
|
|
|
|
.defineCommand(cmd.getName(), 'POST', '/session/:sessionId/moz/addon/install')
|
|
|
|
|
|
|
|
return await driver.schedule(cmd, 'installWebExt(' + extension + ')')
|
2018-05-25 03:17:26 +02:00
|
|
|
}
|