require('chromedriver') require('geckodriver') const fs = require('fs-extra') const os = require('os') const path = require('path') const webdriver = require('selenium-webdriver') const Command = require('selenium-webdriver/lib/command').Command const By = webdriver.By module.exports = { delay, buildChromeWebDriver, buildFirefoxWebdriver, installWebExt, getExtensionIdChrome, getExtensionIdFirefox, } function delay (time) { return new Promise(resolve => setTimeout(resolve, time)) } function buildChromeWebDriver (extPath, opts = {}) { const tmpProfile = fs.mkdtempSync(path.join(os.tmpdir(), 'mm-chrome-profile')) const args = [ `load-extension=${extPath}`, `user-data-dir=${tmpProfile}`, ] if (opts.responsive) { args.push('--auto-open-devtools-for-tabs') } return new webdriver.Builder() .withCapabilities({ chromeOptions: { args, binary: process.env.SELENIUM_CHROME_BINARY, }, }) .build() } function buildFirefoxWebdriver (opts = {}) { const driver = new webdriver.Builder().build() if (opts.responsive) { driver.manage().window().setSize(320, 600) } return driver } async function getExtensionIdChrome (driver) { await driver.get('chrome://extensions') const extensionId = await driver.executeScript('return document.querySelector("extensions-manager").shadowRoot.querySelector("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.wait(webdriver.until.elementLocated(By.xpath('//dl/div[contains(., \'Internal UUID\')]/dd')), 1000).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 + ')') }