2018-01-18 17:10:53 +01:00
|
|
|
require('chromedriver')
|
2018-05-01 21:18:22 +02:00
|
|
|
require('geckodriver')
|
2018-06-11 23:35:20 +02:00
|
|
|
const fs = require('fs-extra')
|
2018-05-25 03:17:26 +02:00
|
|
|
const os = require('os')
|
2018-05-15 02:17:36 +02:00
|
|
|
const path = require('path')
|
2018-06-11 23:25:49 +02:00
|
|
|
const pify = require('pify')
|
|
|
|
const prependFile = pify(require('prepend-file'))
|
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,
|
2018-06-11 23:25:49 +02:00
|
|
|
createModifiedTestBuild,
|
2018-06-11 23:55:50 +02:00
|
|
|
setupBrowserAndExtension,
|
2018-06-11 23:46:45 +02:00
|
|
|
verboseReportOnFailure,
|
2018-05-15 02:17:36 +02:00
|
|
|
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-06-11 23:25:49 +02:00
|
|
|
async function createModifiedTestBuild ({ browser, srcPath }) {
|
|
|
|
// copy build to test-builds directory
|
|
|
|
const extPath = path.resolve(`test-builds/${browser}`)
|
|
|
|
await fs.ensureDir(extPath)
|
|
|
|
await fs.copy(srcPath, extPath)
|
|
|
|
// inject METAMASK_TEST_CONFIG setting default test network
|
|
|
|
const config = { NetworkController: { provider: { type: 'localhost' } } }
|
|
|
|
await prependFile(`${extPath}/background.js`, `window.METAMASK_TEST_CONFIG=${JSON.stringify(config)};\n`)
|
|
|
|
return { extPath }
|
|
|
|
}
|
|
|
|
|
|
|
|
async function setupBrowserAndExtension ({ browser, extPath }) {
|
2018-07-03 21:42:14 +02:00
|
|
|
let driver, extensionId, extensionUri
|
2018-06-11 23:25:49 +02:00
|
|
|
|
|
|
|
if (browser === 'chrome') {
|
|
|
|
driver = buildChromeWebDriver(extPath)
|
|
|
|
extensionId = await getExtensionIdChrome(driver)
|
2018-06-12 03:56:59 +02:00
|
|
|
extensionUri = `chrome-extension://${extensionId}/home.html`
|
2018-06-11 23:25:49 +02:00
|
|
|
} else if (browser === 'firefox') {
|
|
|
|
driver = buildFirefoxWebdriver()
|
|
|
|
await installWebExt(driver, extPath)
|
|
|
|
await delay(700)
|
|
|
|
extensionId = await getExtensionIdFirefox(driver)
|
2018-06-12 03:56:59 +02:00
|
|
|
extensionUri = `moz-extension://${extensionId}/home.html`
|
2018-06-11 23:25:49 +02:00
|
|
|
} else {
|
|
|
|
throw new Error(`Unknown Browser "${browser}"`)
|
|
|
|
}
|
|
|
|
|
|
|
|
return { driver, extensionId, extensionUri }
|
|
|
|
}
|
|
|
|
|
2018-11-05 14:02:06 +01:00
|
|
|
function buildChromeWebDriver (extPath, opts = {}) {
|
2018-06-12 18:38:30 +02:00
|
|
|
const tmpProfile = fs.mkdtempSync(path.join(os.tmpdir(), 'mm-chrome-profile'))
|
2018-11-05 14:02:06 +01:00
|
|
|
const args = [
|
|
|
|
`load-extension=${extPath}`,
|
|
|
|
`user-data-dir=${tmpProfile}`,
|
|
|
|
]
|
|
|
|
if (opts.responsive) {
|
|
|
|
args.push('--auto-open-devtools-for-tabs')
|
|
|
|
}
|
2017-09-12 23:14:24 +02:00
|
|
|
return new webdriver.Builder()
|
|
|
|
.withCapabilities({
|
|
|
|
chromeOptions: {
|
2018-11-05 14:02:06 +01:00
|
|
|
args,
|
2018-05-25 03:17:26 +02:00
|
|
|
binary: process.env.SELENIUM_CHROME_BINARY,
|
2017-09-12 23:14:24 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
.build()
|
|
|
|
}
|
2018-05-01 21:18:22 +02:00
|
|
|
|
2018-11-05 14:02:06 +01:00
|
|
|
function buildFirefoxWebdriver (opts = {}) {
|
|
|
|
const driver = new webdriver.Builder().build()
|
|
|
|
if (opts.responsive) {
|
|
|
|
driver.manage().window().setSize(320, 600)
|
|
|
|
}
|
|
|
|
return driver
|
2018-05-01 21:18:22 +02:00
|
|
|
}
|
2018-05-15 02:17:36 +02:00
|
|
|
|
|
|
|
async function getExtensionIdChrome (driver) {
|
|
|
|
await driver.get('chrome://extensions')
|
2019-01-23 16:25:34 +01:00
|
|
|
const extensionId = await driver.executeScript('return document.querySelector("extensions-manager").shadowRoot.querySelector("extensions-item-list").shadowRoot.querySelector("extensions-item:nth-child(2)").getAttribute("id")')
|
2018-05-15 02:17:36 +02:00
|
|
|
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
|
|
|
}
|
2018-06-11 23:46:45 +02:00
|
|
|
|
|
|
|
async function verboseReportOnFailure ({ browser, driver, title }) {
|
|
|
|
const artifactDir = `./test-artifacts/${browser}/${title}`
|
|
|
|
const filepathBase = `${artifactDir}/test-failure`
|
|
|
|
await fs.ensureDir(artifactDir)
|
|
|
|
const screenshot = await driver.takeScreenshot()
|
|
|
|
await fs.writeFile(`${filepathBase}-screenshot.png`, screenshot, { encoding: 'base64' })
|
|
|
|
const htmlSource = await driver.getPageSource()
|
|
|
|
await fs.writeFile(`${filepathBase}-dom.html`, htmlSource)
|
|
|
|
}
|