1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

test - e2e - factor out setup phase + rename METAMASK_CONFIG to METAMASK_TEST_CONFIG

This commit is contained in:
kumavis 2018-06-11 14:25:49 -07:00
parent 02f5502e16
commit ebb9447593
4 changed files with 51 additions and 47 deletions

View File

@ -31,7 +31,8 @@ const {
ENVIRONMENT_TYPE_FULLSCREEN, ENVIRONMENT_TYPE_FULLSCREEN,
} = require('./lib/enums') } = 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 STORAGE_KEY = 'metamask-config'
const METAMASK_DEBUG = process.env.METAMASK_DEBUG const METAMASK_DEBUG = process.env.METAMASK_DEBUG

View File

@ -4,11 +4,8 @@ const webdriver = require('selenium-webdriver')
const { By, Key, until } = webdriver const { By, Key, until } = webdriver
const { const {
delay, delay,
buildChromeWebDriver, createModifiedTestBuild,
buildFirefoxWebdriver, setupBrowserAndExtension,
installWebExt,
getExtensionIdChrome,
getExtensionIdFirefox,
} = require('../func') } = require('../func')
const { const {
findElement, findElement,
@ -19,6 +16,7 @@ const {
} = require('./helpers') } = require('./helpers')
describe('MetaMask', function () { describe('MetaMask', function () {
const browser = process.env.SELENIUM_BROWSER
let extensionId let extensionId
let driver let driver
let tokenAddress let tokenAddress
@ -33,27 +31,15 @@ describe('MetaMask', function () {
this.bail(true) this.bail(true)
before(async function () { before(async function () {
switch (process.env.SELENIUM_BROWSER) { const srcPath = path.resolve(`dist/${browser}`)
case 'chrome': { const { extPath } = await createModifiedTestBuild({ browser, srcPath })
const extPath = path.resolve('dist/chrome') const installResult = await setupBrowserAndExtension({ browser, extPath })
driver = buildChromeWebDriver(extPath) driver = installResult.driver
extensionId = await getExtensionIdChrome(driver) extensionUri = installResult.extensionUri
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`)
}
}
}) })
afterEach(async function () { afterEach(async function () {
if (process.env.SELENIUM_BROWSER === 'chrome') { if (browser === 'chrome') {
const errors = await checkBrowserForConsoleErrors(driver) const errors = await checkBrowserForConsoleErrors(driver)
if (errors.length) { if (errors.length) {
const errorReports = errors.map(err => err.message) const errorReports = errors.map(err => err.message)

View File

@ -3,12 +3,15 @@ require('geckodriver')
const fs = require('fs') const fs = require('fs')
const os = require('os') const os = require('os')
const path = require('path') const path = require('path')
const pify = require('pify')
const prependFile = pify(require('prepend-file'))
const webdriver = require('selenium-webdriver') const webdriver = require('selenium-webdriver')
const Command = require('selenium-webdriver/lib/command').Command const Command = require('selenium-webdriver/lib/command').Command
const By = webdriver.By const By = webdriver.By
module.exports = { module.exports = {
delay, delay,
createModifiedTestBuild,
buildChromeWebDriver, buildChromeWebDriver,
buildFirefoxWebdriver, buildFirefoxWebdriver,
installWebExt, installWebExt,
@ -20,6 +23,37 @@ function delay (time) {
return new Promise(resolve => setTimeout(resolve, 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) { function buildChromeWebDriver (extPath) {
const tmpProfile = path.join(os.tmpdir(), fs.mkdtempSync('mm-chrome-profile')); const tmpProfile = path.join(os.tmpdir(), fs.mkdtempSync('mm-chrome-profile'));
return new webdriver.Builder() return new webdriver.Builder()

View File

@ -3,10 +3,8 @@ const mkdirp = require('mkdirp')
const path = require('path') const path = require('path')
const assert = require('assert') const assert = require('assert')
const pify = require('pify') const pify = require('pify')
const prependFile = pify(require('prepend-file')) const { By, Key } = require('selenium-webdriver')
const webdriver = require('selenium-webdriver') const { delay, createModifiedTestBuild, setupBrowserAndExtension } = require('./func')
const { By, Key } = webdriver
const { delay, buildChromeWebDriver, buildFirefoxWebdriver, installWebExt, getExtensionIdChrome, getExtensionIdFirefox } = require('./func')
describe('Metamask popup page', function () { describe('Metamask popup page', function () {
const browser = process.env.SELENIUM_BROWSER const browser = process.env.SELENIUM_BROWSER
@ -16,25 +14,11 @@ describe('Metamask popup page', function () {
before(async function () { before(async function () {
const srcPath = path.resolve(`dist/${browser}`) const srcPath = path.resolve(`dist/${browser}`)
const extPath = path.resolve(`test-builds/${browser}`) const { extPath } = await createModifiedTestBuild({ browser, srcPath })
await fs.ensureDir(extPath) const installResult = await setupBrowserAndExtension({ browser, extPath })
await fs.copy(srcPath, extPath) driver = installResult.driver
const config = { NetworkController: { provider: { type: 'localhost' } } } extensionUri = installResult.extensionUri
await prependFile(`${extPath}/background.js`, `window.METAMASK_CONFIG=${JSON.stringify(config)};\n`)
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) await driver.get(extensionUri)
}) })
@ -53,7 +37,6 @@ describe('Metamask popup page', function () {
// gather extra data if test failed // gather extra data if test failed
if (this.currentTest.state === 'failed') { if (this.currentTest.state === 'failed') {
await verboseReportOnFailure(this.currentTest) await verboseReportOnFailure(this.currentTest)
await delay(1000000)
} }
}) })