diff options
test - e2e - factor out setup phase + rename METAMASK_CONFIG to METAMASK_TEST_CONFIG
-rw-r--r-- | app/scripts/background.js | 3 | ||||
-rw-r--r-- | test/e2e/beta/metamask-beta-ui.spec.js | 32 | ||||
-rw-r--r-- | test/e2e/func.js | 34 | ||||
-rw-r--r-- | test/e2e/metamask.spec.js | 29 |
4 files changed, 51 insertions, 47 deletions
diff --git a/app/scripts/background.js b/app/scripts/background.js index 41fd89016..9866ff0b0 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -31,7 +31,8 @@ const { ENVIRONMENT_TYPE_FULLSCREEN, } = require('./lib/enums') -const firstTimeState = Object.assign({}, rawFirstTimeState, global.METAMASK_CONFIG) +// METAMASK_TEST_CONFIG is used in e2e tests to set the default network to localhost +const firstTimeState = Object.assign({}, rawFirstTimeState, global.METAMASK_TEST_CONFIG) const STORAGE_KEY = 'metamask-config' const METAMASK_DEBUG = process.env.METAMASK_DEBUG diff --git a/test/e2e/beta/metamask-beta-ui.spec.js b/test/e2e/beta/metamask-beta-ui.spec.js index ceeea31a5..8960e7e79 100644 --- a/test/e2e/beta/metamask-beta-ui.spec.js +++ b/test/e2e/beta/metamask-beta-ui.spec.js @@ -4,11 +4,8 @@ const webdriver = require('selenium-webdriver') const { By, Key, until } = webdriver const { delay, - buildChromeWebDriver, - buildFirefoxWebdriver, - installWebExt, - getExtensionIdChrome, - getExtensionIdFirefox, + createModifiedTestBuild, + setupBrowserAndExtension, } = require('../func') const { findElement, @@ -19,6 +16,7 @@ const { } = require('./helpers') describe('MetaMask', function () { + const browser = process.env.SELENIUM_BROWSER let extensionId let driver let tokenAddress @@ -33,27 +31,15 @@ describe('MetaMask', function () { this.bail(true) before(async function () { - switch (process.env.SELENIUM_BROWSER) { - case 'chrome': { - const extPath = path.resolve('dist/chrome') - driver = buildChromeWebDriver(extPath) - extensionId = await getExtensionIdChrome(driver) - await driver.get(`chrome-extension://${extensionId}/popup.html`) - break - } - case 'firefox': { - const extPath = path.resolve('dist/firefox') - driver = buildFirefoxWebdriver() - await installWebExt(driver, extPath) - await delay(700) - extensionId = await getExtensionIdFirefox(driver) - await driver.get(`moz-extension://${extensionId}/popup.html`) - } - } + 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) diff --git a/test/e2e/func.js b/test/e2e/func.js index 9f06e7f37..60e02a296 100644 --- a/test/e2e/func.js +++ b/test/e2e/func.js @@ -3,12 +3,15 @@ require('geckodriver') const fs = require('fs') const os = require('os') const path = require('path') +const pify = require('pify') +const prependFile = pify(require('prepend-file')) const webdriver = require('selenium-webdriver') const Command = require('selenium-webdriver/lib/command').Command const By = webdriver.By module.exports = { delay, + createModifiedTestBuild, buildChromeWebDriver, buildFirefoxWebdriver, installWebExt, @@ -20,6 +23,37 @@ function delay (time) { return new Promise(resolve => setTimeout(resolve, time)) } +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 }) { + let drive, extensionId, extensionUri + + if (browser === 'chrome') { + driver = buildChromeWebDriver(extPath) + extensionId = await getExtensionIdChrome(driver) + extensionUri = `chrome-extension://${extensionId}/popup.html` + } else if (browser === 'firefox') { + driver = buildFirefoxWebdriver() + await installWebExt(driver, extPath) + await delay(700) + extensionId = await getExtensionIdFirefox(driver) + extensionUri = `moz-extension://${extensionId}/popup.html` + } else { + throw new Error(`Unknown Browser "${browser}"`) + } + + return { driver, extensionId, extensionUri } +} + function buildChromeWebDriver (extPath) { const tmpProfile = path.join(os.tmpdir(), fs.mkdtempSync('mm-chrome-profile')); return new webdriver.Builder() diff --git a/test/e2e/metamask.spec.js b/test/e2e/metamask.spec.js index 06c13b3d0..0d6ab9b35 100644 --- a/test/e2e/metamask.spec.js +++ b/test/e2e/metamask.spec.js @@ -3,10 +3,8 @@ const mkdirp = require('mkdirp') const path = require('path') const assert = require('assert') const pify = require('pify') -const prependFile = pify(require('prepend-file')) -const webdriver = require('selenium-webdriver') -const { By, Key } = webdriver -const { delay, buildChromeWebDriver, buildFirefoxWebdriver, installWebExt, getExtensionIdChrome, getExtensionIdFirefox } = require('./func') +const { By, Key } = require('selenium-webdriver') +const { delay, createModifiedTestBuild, setupBrowserAndExtension } = require('./func') describe('Metamask popup page', function () { const browser = process.env.SELENIUM_BROWSER @@ -16,25 +14,11 @@ describe('Metamask popup page', function () { before(async function () { const srcPath = path.resolve(`dist/${browser}`) - const extPath = path.resolve(`test-builds/${browser}`) - await fs.ensureDir(extPath) - await fs.copy(srcPath, extPath) - const config = { NetworkController: { provider: { type: 'localhost' } } } - await prependFile(`${extPath}/background.js`, `window.METAMASK_CONFIG=${JSON.stringify(config)};\n`) + const { extPath } = await createModifiedTestBuild({ browser, srcPath }) + const installResult = await setupBrowserAndExtension({ browser, extPath }) + driver = installResult.driver + extensionUri = installResult.extensionUri - if (browser === 'chrome') { - driver = buildChromeWebDriver(extPath) - extensionId = await getExtensionIdChrome(driver) - extensionUri = `chrome-extension://${extensionId}/popup.html` - } else if (browser === 'firefox') { - driver = buildFirefoxWebdriver() - await installWebExt(driver, extPath) - await delay(700) - extensionId = await getExtensionIdFirefox(driver) - extensionUri = `moz-extension://${extensionId}/popup.html` - } else { - throw new Error(`Unknown Browser "${browser}"`) - } await driver.get(extensionUri) }) @@ -53,7 +37,6 @@ describe('Metamask popup page', function () { // gather extra data if test failed if (this.currentTest.state === 'failed') { await verboseReportOnFailure(this.currentTest) - await delay(1000000) } }) |