From 95b1cfd2823eb8435e1acbb5986bdf5452738e18 Mon Sep 17 00:00:00 2001 From: kumavis Date: Sat, 7 Sep 2019 15:13:58 +0800 Subject: [PATCH] test:e2e - factor out prepareExtensionForTesting --- test/e2e/address-book.spec.js | 38 ++----------- test/e2e/from-import-ui.spec.js | 38 +------------ test/e2e/helpers.js | 75 +++++++++++++++++++++---- test/e2e/incremental-security.spec.js | 38 ++----------- test/e2e/metamask-responsive-ui.spec.js | 38 ++----------- test/e2e/metamask-ui.spec.js | 37 ++---------- test/e2e/send-edit.spec.js | 38 +------------ test/e2e/web3.spec.js | 38 +------------ 8 files changed, 89 insertions(+), 251 deletions(-) diff --git a/test/e2e/address-book.spec.js b/test/e2e/address-book.spec.js index 3e95daaef..b23aa94ad 100644 --- a/test/e2e/address-book.spec.js +++ b/test/e2e/address-book.spec.js @@ -1,23 +1,17 @@ -const path = require('path') const assert = require('assert') const webdriver = require('selenium-webdriver') const { By, until } = webdriver const { delay, - buildChromeWebDriver, - buildFirefoxWebdriver, - installWebExt, - getExtensionIdChrome, - getExtensionIdFirefox, } = require('./func') const { checkBrowserForConsoleErrors, - closeAllWindowHandlesExcept, findElement, findElements, loadExtension, verboseReportOnFailure, setupFetchMocking, + prepareExtensionForTesting, } = require('./helpers') describe('MetaMask', function () { @@ -33,33 +27,9 @@ describe('MetaMask', function () { this.bail(true) before(async function () { - let extensionUrl - switch (process.env.SELENIUM_BROWSER) { - case 'chrome': { - const extPath = path.resolve('dist/chrome') - driver = buildChromeWebDriver(extPath) - extensionId = await getExtensionIdChrome(driver) - await delay(largeDelayMs) - extensionUrl = `chrome-extension://${extensionId}/home.html` - break - } - case 'firefox': { - const extPath = path.resolve('dist/firefox') - driver = buildFirefoxWebdriver() - await installWebExt(driver, extPath) - await delay(largeDelayMs) - extensionId = await getExtensionIdFirefox(driver) - extensionUrl = `moz-extension://${extensionId}/home.html` - break - } - } - // Depending on the state of the application built into the above directory (extPath) and the value of - // METAMASK_DEBUG we will see different post-install behaviour and possibly some extra windows. Here we - // are closing any extraneous windows to reset us to a single window before continuing. - const [tab1] = await driver.getAllWindowHandles() - await closeAllWindowHandlesExcept(driver, [tab1]) - await driver.switchTo().window(tab1) - await driver.get(extensionUrl) + const result = await prepareExtensionForTesting() + driver = result.driver + extensionId = result.extensionId }) beforeEach(async function () { diff --git a/test/e2e/from-import-ui.spec.js b/test/e2e/from-import-ui.spec.js index 60bba25fa..c0d0048f1 100644 --- a/test/e2e/from-import-ui.spec.js +++ b/test/e2e/from-import-ui.spec.js @@ -1,26 +1,19 @@ -const path = require('path') const assert = require('assert') const webdriver = require('selenium-webdriver') const { By, Key, until } = webdriver const { delay, - buildChromeWebDriver, - buildFirefoxWebdriver, - installWebExt, - getExtensionIdChrome, - getExtensionIdFirefox, } = require('./func') const { checkBrowserForConsoleErrors, - closeAllWindowHandlesExcept, verboseReportOnFailure, findElement, findElements, setupFetchMocking, + prepareExtensionForTesting, } = require('./helpers') describe('Using MetaMask with an existing account', function () { - let extensionId let driver const testSeedPhrase = 'forum vessel pink push lonely enact gentle tail admit parrot grunt dress' @@ -35,33 +28,8 @@ describe('Using MetaMask with an existing account', function () { this.bail(true) before(async function () { - let extensionUrl - switch (process.env.SELENIUM_BROWSER) { - case 'chrome': { - const extensionPath = path.resolve('dist/chrome') - driver = buildChromeWebDriver(extensionPath) - extensionId = await getExtensionIdChrome(driver) - await delay(regularDelayMs) - extensionUrl = `chrome-extension://${extensionId}/home.html` - break - } - case 'firefox': { - const extensionPath = path.resolve('dist/firefox') - driver = buildFirefoxWebdriver() - await installWebExt(driver, extensionPath) - await delay(regularDelayMs) - extensionId = await getExtensionIdFirefox(driver) - extensionUrl = `moz-extension://${extensionId}/home.html` - break - } - } - // Depending on the state of the application built into the above directory (extPath) and the value of - // METAMASK_DEBUG we will see different post-install behaviour and possibly some extra windows. Here we - // are closing any extraneous windows to reset us to a single window before continuing. - const [tab1] = await driver.getAllWindowHandles() - await closeAllWindowHandlesExcept(driver, [tab1]) - await driver.switchTo().window(tab1) - await driver.get(extensionUrl) + const result = await prepareExtensionForTesting() + driver = result.driver }) beforeEach(async function () { diff --git a/test/e2e/helpers.js b/test/e2e/helpers.js index d74e45544..4875f9d72 100644 --- a/test/e2e/helpers.js +++ b/test/e2e/helpers.js @@ -1,11 +1,23 @@ const fs = require('fs') +const path = require('path') const mkdirp = require('mkdirp') const pify = require('pify') const assert = require('assert') -const { delay } = require('./func') + +const { + delay, + getExtensionIdChrome, + getExtensionIdFirefox, + buildChromeWebDriver, + buildFirefoxWebdriver, + installWebExt, +} = require('./func') const { until } = require('selenium-webdriver') const fetchMockResponses = require('./fetch-mocks.js') +const tinyDelayMs = 200 +const regularDelayMs = tinyDelayMs * 2 +const largeDelayMs = regularDelayMs * 2 module.exports = { assertElementNotPresent, @@ -20,26 +32,67 @@ module.exports = { verboseReportOnFailure, waitUntilXWindowHandles, setupFetchMocking, + prepareExtensionForTesting, + tinyDelayMs, + regularDelayMs, + largeDelayMs, +} + + +async function prepareExtensionForTesting () { + let driver, extensionId, extensionUrl + const targetBrowser = process.env.SELENIUM_BROWSER + switch (targetBrowser) { + case 'chrome': { + const extPath = path.resolve('dist/chrome') + driver = buildChromeWebDriver(extPath, { responsive: true }) + await delay(largeDelayMs) + extensionId = await getExtensionIdChrome(driver) + extensionUrl = `chrome-extension://${extensionId}/home.html` + break + } + case 'firefox': { + const extPath = path.resolve('dist/firefox') + driver = buildFirefoxWebdriver({ responsive: true }) + await installWebExt(driver, extPath) + await delay(largeDelayMs) + extensionId = await getExtensionIdFirefox(driver) + extensionUrl = `moz-extension://${extensionId}/home.html` + break + } + default: { + throw new Error(`prepareExtensionForTesting - unable to prepare extension for unknown browser "${targetBrowser}"`) + } + } + // Depending on the state of the application built into the above directory (extPath) and the value of + // METAMASK_DEBUG we will see different post-install behaviour and possibly some extra windows. Here we + // are closing any extraneous windows to reset us to a single window before continuing. + const [tab1] = await driver.getAllWindowHandles() + await closeAllWindowHandlesExcept(driver, [tab1]) + await driver.switchTo().window(tab1) + await driver.get(extensionUrl) + + return { driver, extensionId, extensionUrl } } async function setupFetchMocking (driver) { - // define fetchMocking script, to be evaluated in the browser - function fetchMocking() { + // define fetchMocking script, to be evaluated in the browser + function fetchMocking () { window.origFetch = window.fetch.bind(window) window.fetch = async (...args) => { const url = args[0] - if (url === "https://ethgasstation.info/json/ethgasAPI.json") { - return { json: async () => JSON.parse( fetchMockResponses.ethGasBasic ) } - } else if (url === "https://ethgasstation.info/json/predictTable.json") { - return { json: async () => JSON.parse( fetchMockResponses.ethGasPredictTable ) } + if (url === 'https://ethgasstation.info/json/ethgasAPI.json') { + return { json: async () => JSON.parse(fetchMockResponses.ethGasBasic) } + } else if (url === 'https://ethgasstation.info/json/predictTable.json') { + return { json: async () => JSON.parse(fetchMockResponses.ethGasPredictTable) } } else if (url.match(/chromeextensionmm/)) { - return { json: async () => JSON.parse( fetchMockResponses.metametrics ) } - } else if (url === "https://dev.blockscale.net/api/gasexpress.json") { - return { json: async () => JSON.parse( fetchMockResponses.gasExpress ) } + return { json: async () => JSON.parse(fetchMockResponses.metametrics) } + } else if (url === 'https://dev.blockscale.net/api/gasexpress.json') { + return { json: async () => JSON.parse(fetchMockResponses.gasExpress) } } return window.origFetch(...args) } - function cancelInfuraRequest(requestDetails) { + function cancelInfuraRequest (requestDetails) { console.log(`fetchMocking - Canceling request: "${requestDetails.url}"`) return { cancel: true } } diff --git a/test/e2e/incremental-security.spec.js b/test/e2e/incremental-security.spec.js index 3a44badd6..7bf9d0902 100644 --- a/test/e2e/incremental-security.spec.js +++ b/test/e2e/incremental-security.spec.js @@ -1,25 +1,19 @@ -const path = require('path') const assert = require('assert') const webdriver = require('selenium-webdriver') const { By, until } = webdriver const { delay, - buildChromeWebDriver, - buildFirefoxWebdriver, - installWebExt, - getExtensionIdChrome, - getExtensionIdFirefox, } = require('./func') const { assertElementNotPresent, checkBrowserForConsoleErrors, - closeAllWindowHandlesExcept, findElement, findElements, loadExtension, openNewPage, verboseReportOnFailure, setupFetchMocking, + prepareExtensionForTesting, } = require('./helpers') describe('MetaMask', function () { @@ -35,33 +29,9 @@ describe('MetaMask', function () { this.bail(true) before(async function () { - let extensionUrl - switch (process.env.SELENIUM_BROWSER) { - case 'chrome': { - const extPath = path.resolve('dist/chrome') - driver = buildChromeWebDriver(extPath) - extensionId = await getExtensionIdChrome(driver) - await delay(largeDelayMs) - extensionUrl = `chrome-extension://${extensionId}/home.html` - break - } - case 'firefox': { - const extPath = path.resolve('dist/firefox') - driver = buildFirefoxWebdriver() - await installWebExt(driver, extPath) - await delay(largeDelayMs) - extensionId = await getExtensionIdFirefox(driver) - extensionUrl = `moz-extension://${extensionId}/home.html` - break - } - } - // Depending on the state of the application built into the above directory (extPath) and the value of - // METAMASK_DEBUG we will see different post-install behaviour and possibly some extra windows. Here we - // are closing any extraneous windows to reset us to a single window before continuing. - const [tab1] = await driver.getAllWindowHandles() - await closeAllWindowHandlesExcept(driver, [tab1]) - await driver.switchTo().window(tab1) - await driver.get(extensionUrl) + const result = await prepareExtensionForTesting() + driver = result.driver + extensionId = result.extensionId }) beforeEach(async function () { diff --git a/test/e2e/metamask-responsive-ui.spec.js b/test/e2e/metamask-responsive-ui.spec.js index a7535a47a..90b81df41 100644 --- a/test/e2e/metamask-responsive-ui.spec.js +++ b/test/e2e/metamask-responsive-ui.spec.js @@ -1,23 +1,17 @@ -const path = require('path') const assert = require('assert') const webdriver = require('selenium-webdriver') const { By, until } = webdriver const { delay, - buildChromeWebDriver, - buildFirefoxWebdriver, - installWebExt, - getExtensionIdChrome, - getExtensionIdFirefox, } = require('./func') const { checkBrowserForConsoleErrors, - closeAllWindowHandlesExcept, findElement, findElements, loadExtension, verboseReportOnFailure, setupFetchMocking, + prepareExtensionForTesting, } = require('./helpers') describe('MetaMask', function () { @@ -33,33 +27,9 @@ describe('MetaMask', function () { this.bail(true) before(async function () { - let extensionUrl - switch (process.env.SELENIUM_BROWSER) { - case 'chrome': { - const extPath = path.resolve('dist/chrome') - driver = buildChromeWebDriver(extPath, { responsive: true }) - extensionId = await getExtensionIdChrome(driver) - await delay(largeDelayMs) - extensionUrl = `chrome-extension://${extensionId}/home.html` - break - } - case 'firefox': { - const extPath = path.resolve('dist/firefox') - driver = buildFirefoxWebdriver({ responsive: true }) - await installWebExt(driver, extPath) - await delay(largeDelayMs) - extensionId = await getExtensionIdFirefox(driver) - extensionUrl = `moz-extension://${extensionId}/home.html` - break - } - } - // Depending on the state of the application built into the above directory (extPath) and the value of - // METAMASK_DEBUG we will see different post-install behaviour and possibly some extra windows. Here we - // are closing any extraneous windows to reset us to a single window before continuing. - const [tab1] = await driver.getAllWindowHandles() - await closeAllWindowHandlesExcept(driver, [tab1]) - await driver.switchTo().window(tab1) - await driver.get(extensionUrl) + const result = await prepareExtensionForTesting() + driver = result.driver + extensionId = result.extensionId }) beforeEach(async function () { diff --git a/test/e2e/metamask-ui.spec.js b/test/e2e/metamask-ui.spec.js index 031d2bb14..236d807fb 100644 --- a/test/e2e/metamask-ui.spec.js +++ b/test/e2e/metamask-ui.spec.js @@ -1,14 +1,8 @@ -const path = require('path') const assert = require('assert') const webdriver = require('selenium-webdriver') const { By, Key, until } = webdriver const { delay, - buildChromeWebDriver, - buildFirefoxWebdriver, - installWebExt, - getExtensionIdChrome, - getExtensionIdFirefox, } = require('./func') const { assertElementNotPresent, @@ -22,6 +16,7 @@ const { verboseReportOnFailure, waitUntilXWindowHandles, setupFetchMocking, + prepareExtensionForTesting, } = require('./helpers') describe('MetaMask', function () { @@ -38,33 +33,9 @@ describe('MetaMask', function () { this.bail(true) before(async function () { - let extensionUrl - switch (process.env.SELENIUM_BROWSER) { - case 'chrome': { - const extPath = path.resolve('dist/chrome') - driver = buildChromeWebDriver(extPath) - extensionId = await getExtensionIdChrome(driver) - await delay(largeDelayMs) - extensionUrl = `chrome-extension://${extensionId}/home.html` - break - } - case 'firefox': { - const extPath = path.resolve('dist/firefox') - driver = buildFirefoxWebdriver() - await installWebExt(driver, extPath) - await delay(largeDelayMs) - extensionId = await getExtensionIdFirefox(driver) - extensionUrl = `moz-extension://${extensionId}/home.html` - break - } - } - // Depending on the state of the application built into the above directory (extPath) and the value of - // METAMASK_DEBUG we will see different post-install behaviour and possibly some extra windows. Here we - // are closing any extraneous windows to reset us to a single window before continuing. - const [tab1] = await driver.getAllWindowHandles() - await closeAllWindowHandlesExcept(driver, [tab1]) - await driver.switchTo().window(tab1) - await driver.get(extensionUrl) + const result = await prepareExtensionForTesting() + driver = result.driver + extensionId = result.extensionId }) beforeEach(async function () { diff --git a/test/e2e/send-edit.spec.js b/test/e2e/send-edit.spec.js index bc5160112..c007b82fd 100644 --- a/test/e2e/send-edit.spec.js +++ b/test/e2e/send-edit.spec.js @@ -1,26 +1,19 @@ -const path = require('path') const assert = require('assert') const webdriver = require('selenium-webdriver') const { By, Key, until } = webdriver const { delay, - buildChromeWebDriver, - buildFirefoxWebdriver, - installWebExt, - getExtensionIdChrome, - getExtensionIdFirefox, } = require('./func') const { checkBrowserForConsoleErrors, - closeAllWindowHandlesExcept, verboseReportOnFailure, findElement, findElements, setupFetchMocking, + prepareExtensionForTesting, } = require('./helpers') describe('Using MetaMask with an existing account', function () { - let extensionId let driver const testSeedPhrase = 'forum vessel pink push lonely enact gentle tail admit parrot grunt dress' @@ -32,33 +25,8 @@ describe('Using MetaMask with an existing account', function () { this.bail(true) before(async function () { - let extensionUrl - switch (process.env.SELENIUM_BROWSER) { - case 'chrome': { - const extensionPath = path.resolve('dist/chrome') - driver = buildChromeWebDriver(extensionPath) - extensionId = await getExtensionIdChrome(driver) - await delay(regularDelayMs) - extensionUrl = `chrome-extension://${extensionId}/home.html` - break - } - case 'firefox': { - const extensionPath = path.resolve('dist/firefox') - driver = buildFirefoxWebdriver() - await installWebExt(driver, extensionPath) - await delay(regularDelayMs) - extensionId = await getExtensionIdFirefox(driver) - extensionUrl = `moz-extension://${extensionId}/home.html` - break - } - } - // Depending on the state of the application built into the above directory (extPath) and the value of - // METAMASK_DEBUG we will see different post-install behaviour and possibly some extra windows. Here we - // are closing any extraneous windows to reset us to a single window before continuing. - const [tab1] = await driver.getAllWindowHandles() - await closeAllWindowHandlesExcept(driver, [tab1]) - await driver.switchTo().window(tab1) - await driver.get(extensionUrl) + const result = await prepareExtensionForTesting() + driver = result.driver }) beforeEach(async function () { diff --git a/test/e2e/web3.spec.js b/test/e2e/web3.spec.js index ae1c3091e..e22d0e596 100644 --- a/test/e2e/web3.spec.js +++ b/test/e2e/web3.spec.js @@ -1,18 +1,11 @@ -const path = require('path') const assert = require('assert') const webdriver = require('selenium-webdriver') const { By } = webdriver const { delay, - buildChromeWebDriver, - buildFirefoxWebdriver, - installWebExt, - getExtensionIdChrome, - getExtensionIdFirefox, } = require('./func') const { checkBrowserForConsoleErrors, - closeAllWindowHandlesExcept, findElement, findElements, openNewPage, @@ -20,10 +13,10 @@ const { verboseReportOnFailure, waitUntilXWindowHandles, setupFetchMocking, + prepareExtensionForTesting, } = require('./helpers') describe('Using MetaMask with an existing account', function () { - let extensionId let driver const testSeedPhrase = 'forum vessel pink push lonely enact gentle tail admit parrot grunt dress' @@ -46,33 +39,8 @@ describe('Using MetaMask with an existing account', function () { this.bail(true) before(async function () { - let extensionUrl - switch (process.env.SELENIUM_BROWSER) { - case 'chrome': { - const extensionPath = path.resolve('dist/chrome') - driver = buildChromeWebDriver(extensionPath) - extensionId = await getExtensionIdChrome(driver) - await delay(regularDelayMs) - extensionUrl = `chrome-extension://${extensionId}/home.html` - break - } - case 'firefox': { - const extensionPath = path.resolve('dist/firefox') - driver = buildFirefoxWebdriver() - await installWebExt(driver, extensionPath) - await delay(regularDelayMs) - extensionId = await getExtensionIdFirefox(driver) - extensionUrl = `moz-extension://${extensionId}/home.html` - break - } - } - // Depending on the state of the application built into the above directory (extPath) and the value of - // METAMASK_DEBUG we will see different post-install behaviour and possibly some extra windows. Here we - // are closing any extraneous windows to reset us to a single window before continuing. - const [tab1] = await driver.getAllWindowHandles() - await closeAllWindowHandlesExcept(driver, [tab1]) - await driver.switchTo().window(tab1) - await driver.get(extensionUrl) + const result = await prepareExtensionForTesting() + driver = result.driver }) beforeEach(async function () {