From 8a0961b4a8bd7eef50b8d8e45d73390f43795a14 Mon Sep 17 00:00:00 2001 From: kumavis Date: Mon, 11 Jun 2018 14:46:45 -0700 Subject: [PATCH] test - e2e - more factoring of test setup --- test/e2e/beta/from-import-beta-ui.spec.js | 39 +++++++---------------- test/e2e/beta/helpers.js | 21 +----------- test/e2e/beta/metamask-beta-ui.spec.js | 4 +-- test/e2e/func.js | 11 +++++++ test/e2e/metamask.spec.js | 16 ++-------- 5 files changed, 27 insertions(+), 64 deletions(-) diff --git a/test/e2e/beta/from-import-beta-ui.spec.js b/test/e2e/beta/from-import-beta-ui.spec.js index efae948f9..d6e976dea 100644 --- a/test/e2e/beta/from-import-beta-ui.spec.js +++ b/test/e2e/beta/from-import-beta-ui.spec.js @@ -4,22 +4,20 @@ const webdriver = require('selenium-webdriver') const { By, Key, until } = webdriver const { delay, - buildChromeWebDriver, - buildFirefoxWebdriver, - installWebExt, - getExtensionIdChrome, - getExtensionIdFirefox, + createModifiedTestBuild, + setupBrowserAndExtension, + verboseReportOnFailure, } = require('../func') const { checkBrowserForConsoleErrors, loadExtension, - verboseReportOnFailure, findElement, findElements, } = require('./helpers') describe('Using MetaMask with an existing account', function () { + const browser = process.env.SELENIUM_BROWSER let extensionId let driver let tokenAddress @@ -34,30 +32,15 @@ describe('Using MetaMask with an existing account', function () { this.bail(true) before(async function () { - switch (process.env.SELENIUM_BROWSER) { - case 'chrome': { - const extensionPath = path.resolve('dist/chrome') - driver = buildChromeWebDriver(extensionPath) - extensionId = await getExtensionIdChrome(driver) - await driver.get(`chrome-extension://${extensionId}/popup.html`) - await delay(regularDelayMs) - break - } - case 'firefox': { - const extensionPath = path.resolve('dist/firefox') - driver = buildFirefoxWebdriver() - await installWebExt(driver, extensionPath) - await delay(regularDelayMs) - extensionId = await getExtensionIdFirefox(driver) - await driver.get(`moz-extension://${extensionId}/popup.html`) - await delay(regularDelayMs) - break - } - } + const srcPath = path.resolve(`dist/${browser}`) + const { extPath } = await createModifiedTestBuild({ browser, srcPath }) + const installResult = await setupBrowserAndExtension({ browser, extPath }) + driver = installResult.driver + extensionUri = installResult.extensionUri }) afterEach(async function () { - if (process.env.SELENIUM_BROWSER === 'chrome') { + if (browser === 'chrome') { const errors = await checkBrowserForConsoleErrors(driver) if (errors.length) { const errorReports = errors.map(err => err.message) @@ -66,7 +49,7 @@ describe('Using MetaMask with an existing account', function () { } } if (this.currentTest.state === 'failed') { - await verboseReportOnFailure(driver, this.currentTest) + await verboseReportOnFailure({ browser, driver, title: this.currentTest.title }) } }) diff --git a/test/e2e/beta/helpers.js b/test/e2e/beta/helpers.js index 31c41d8b7..5951ea79b 100644 --- a/test/e2e/beta/helpers.js +++ b/test/e2e/beta/helpers.js @@ -1,12 +1,8 @@ -const fs = require('fs') -const mkdirp = require('mkdirp') -const pify = require('pify') -const {until} = require('selenium-webdriver') +const { until } = require('selenium-webdriver') module.exports = { checkBrowserForConsoleErrors, loadExtension, - verboseReportOnFailure, findElement, findElements, } @@ -42,21 +38,6 @@ async function checkBrowserForConsoleErrors (driver) { return errorObjects.filter(entry => !ignoredErrorMessages.some(message => entry.message.includes(message))) } -async function verboseReportOnFailure (driver, test) { - let artifactDir - if (process.env.SELENIUM_BROWSER === 'chrome') { - artifactDir = `./test-artifacts/chrome/${test.title}` - } else if (process.env.SELENIUM_BROWSER === 'firefox') { - artifactDir = `./test-artifacts/firefox/${test.title}` - } - const filepathBase = `${artifactDir}/test-failure` - await pify(mkdirp)(artifactDir) - const screenshot = await driver.takeScreenshot() - await pify(fs.writeFile)(`${filepathBase}-screenshot.png`, screenshot, { encoding: 'base64' }) - const htmlSource = await driver.getPageSource() - await pify(fs.writeFile)(`${filepathBase}-dom.html`, htmlSource) -} - async function findElement (driver, by, timeout = 10000) { return driver.wait(until.elementLocated(by), timeout) } diff --git a/test/e2e/beta/metamask-beta-ui.spec.js b/test/e2e/beta/metamask-beta-ui.spec.js index 8960e7e79..14ea5b0a5 100644 --- a/test/e2e/beta/metamask-beta-ui.spec.js +++ b/test/e2e/beta/metamask-beta-ui.spec.js @@ -6,13 +6,13 @@ const { delay, createModifiedTestBuild, setupBrowserAndExtension, + verboseReportOnFailure, } = require('../func') const { findElement, findElements, checkBrowserForConsoleErrors, loadExtension, - verboseReportOnFailure, } = require('./helpers') describe('MetaMask', function () { @@ -48,7 +48,7 @@ describe('MetaMask', function () { } } if (this.currentTest.state === 'failed') { - await verboseReportOnFailure(this.currentTest) + await verboseReportOnFailure({ browser, driver, title: this.currentTest.tile }) } }) diff --git a/test/e2e/func.js b/test/e2e/func.js index ca590d84a..ca8d05b18 100644 --- a/test/e2e/func.js +++ b/test/e2e/func.js @@ -12,6 +12,7 @@ const By = webdriver.By module.exports = { delay, createModifiedTestBuild, + verboseReportOnFailure, buildChromeWebDriver, buildFirefoxWebdriver, installWebExt, @@ -95,3 +96,13 @@ async function installWebExt (driver, extension) { return await driver.schedule(cmd, 'installWebExt(' + extension + ')') } + +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) +} diff --git a/test/e2e/metamask.spec.js b/test/e2e/metamask.spec.js index 0d6ab9b35..5153c0ee5 100644 --- a/test/e2e/metamask.spec.js +++ b/test/e2e/metamask.spec.js @@ -4,7 +4,7 @@ const path = require('path') const assert = require('assert') const pify = require('pify') const { By, Key } = require('selenium-webdriver') -const { delay, createModifiedTestBuild, setupBrowserAndExtension } = require('./func') +const { delay, createModifiedTestBuild, setupBrowserAndExtension, verboseReportOnFailure } = require('./func') describe('Metamask popup page', function () { const browser = process.env.SELENIUM_BROWSER @@ -36,7 +36,7 @@ describe('Metamask popup page', function () { } // gather extra data if test failed if (this.currentTest.state === 'failed') { - await verboseReportOnFailure(this.currentTest) + await verboseReportOnFailure({ browser, driver, title: this.currentTest.title }) } }) @@ -319,16 +319,4 @@ describe('Metamask popup page', function () { return matchedErrorObjects } - async function verboseReportOnFailure (test) { - const artifactDir = `./test-artifacts/${browser}/${test.title}` - const filepathBase = `${artifactDir}/test-failure` - await pify(mkdirp)(artifactDir) - // capture screenshot - const screenshot = await driver.takeScreenshot() - await pify(fs.writeFile)(`${filepathBase}-screenshot.png`, screenshot, { encoding: 'base64' }) - // capture dom source - const htmlSource = await driver.getPageSource() - await pify(fs.writeFile)(`${filepathBase}-dom.html`, htmlSource) - } - })