From 3afd69b3ec31bb3af8014a6792875921fa9a8441 Mon Sep 17 00:00:00 2001 From: kumavis Date: Sat, 7 Sep 2019 14:44:22 +0800 Subject: [PATCH] test - e2e - dedupe fetchMocking + compose script as fn --- test/e2e/address-book.spec.js | 28 ++-------------------- test/e2e/from-import-ui.spec.js | 29 ++-------------------- test/e2e/helpers.js | 32 +++++++++++++++++++++++++ test/e2e/incremental-security.spec.js | 28 ++-------------------- test/e2e/metamask-responsive-ui.spec.js | 28 ++-------------------- test/e2e/metamask-ui.spec.js | 28 ++-------------------- test/e2e/send-edit.spec.js | 29 ++-------------------- test/e2e/web3.spec.js | 29 ++-------------------- 8 files changed, 46 insertions(+), 185 deletions(-) diff --git a/test/e2e/address-book.spec.js b/test/e2e/address-book.spec.js index 3c5e78b50..3e95daaef 100644 --- a/test/e2e/address-book.spec.js +++ b/test/e2e/address-book.spec.js @@ -17,8 +17,8 @@ const { findElements, loadExtension, verboseReportOnFailure, + setupFetchMocking, } = require('./helpers') -const fetchMockResponses = require('./fetch-mocks.js') describe('MetaMask', function () { let extensionId @@ -63,31 +63,7 @@ describe('MetaMask', function () { }) beforeEach(async function () { - await driver.executeScript( - 'window.origFetch = window.fetch.bind(window);' + - 'window.fetch = ' + - '(...args) => { ' + - 'if (args[0] === "https://ethgasstation.info/json/ethgasAPI.json") { return ' + - 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.ethGasBasic + '\')) }); } else if ' + - '(args[0] === "https://ethgasstation.info/json/predictTable.json") { return ' + - 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.ethGasPredictTable + '\')) }); } else if ' + - '(args[0].match(/chromeextensionmm/)) { return ' + - 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.metametrics + '\')) }); } else if ' + - '(args[0] === "https://dev.blockscale.net/api/gasexpress.json") { return ' + - 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.gasExpress + '\')) }); } ' + - 'return window.origFetch(...args); };' + - 'function cancelInfuraRequest(requestDetails) {' + - 'console.log("Canceling: " + requestDetails.url);' + - 'return {' + - 'cancel: true' + - '};' + - ' }' + - 'window.chrome && window.chrome.webRequest && window.chrome.webRequest.onBeforeRequest.addListener(' + - 'cancelInfuraRequest,' + - '{urls: ["https://*.infura.io/*"]},' + - '["blocking"]' + - ');' - ) + await setupFetchMocking(driver) }) afterEach(async function () { diff --git a/test/e2e/from-import-ui.spec.js b/test/e2e/from-import-ui.spec.js index 896447c77..60bba25fa 100644 --- a/test/e2e/from-import-ui.spec.js +++ b/test/e2e/from-import-ui.spec.js @@ -16,9 +16,8 @@ const { verboseReportOnFailure, findElement, findElements, + setupFetchMocking, } = require('./helpers') -const fetchMockResponses = require('./fetch-mocks.js') - describe('Using MetaMask with an existing account', function () { let extensionId @@ -66,31 +65,7 @@ describe('Using MetaMask with an existing account', function () { }) beforeEach(async function () { - await driver.executeScript( - 'window.origFetch = window.fetch.bind(window);' + - 'window.fetch = ' + - '(...args) => { ' + - 'if (args[0] === "https://ethgasstation.info/json/ethgasAPI.json") { return ' + - 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.ethGasBasic + '\')) }); } else if ' + - '(args[0] === "https://ethgasstation.info/json/predictTable.json") { return ' + - 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.ethGasPredictTable + '\')) }); } else if ' + - '(args[0].match(/chromeextensionmm/)) { return ' + - 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.metametrics + '\')) }); } else if ' + - '(args[0] === "https://dev.blockscale.net/api/gasexpress.json") { return ' + - 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.gasExpress + '\')) }); } ' + - 'return window.origFetch(...args); };' + - 'function cancelInfuraRequest(requestDetails) {' + - 'console.log("Canceling: " + requestDetails.url);' + - 'return {' + - 'cancel: true' + - '};' + - ' }' + - 'window.chrome && window.chrome.webRequest && window.chrome.webRequest.onBeforeRequest.addListener(' + - 'cancelInfuraRequest,' + - '{urls: ["https://*.infura.io/*"]},' + - '["blocking"]' + - ');' - ) + await setupFetchMocking(driver) }) afterEach(async function () { diff --git a/test/e2e/helpers.js b/test/e2e/helpers.js index 30b8d13f6..d74e45544 100644 --- a/test/e2e/helpers.js +++ b/test/e2e/helpers.js @@ -4,6 +4,8 @@ const pify = require('pify') const assert = require('assert') const { delay } = require('./func') const { until } = require('selenium-webdriver') +const fetchMockResponses = require('./fetch-mocks.js') + module.exports = { assertElementNotPresent, @@ -17,6 +19,36 @@ module.exports = { switchToWindowWithUrlThatMatches, verboseReportOnFailure, waitUntilXWindowHandles, + setupFetchMocking, +} + +async function setupFetchMocking (driver) { + // 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 ) } + } 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 window.origFetch(...args) + } + function cancelInfuraRequest(requestDetails) { + console.log(`fetchMocking - Canceling request: "${requestDetails.url}"`) + return { cancel: true } + } + if (window.chrome && window.chrome.webRequest) { + window.chrome.webRequest.onBeforeRequest.addListener(cancelInfuraRequest, {urls: ['https://*.infura.io/*']}, ['blocking']) + } + } + // eval the fetchMocking script in the browser + await driver.executeScript(`(${fetchMocking})()`) } async function loadExtension (driver, extensionId) { diff --git a/test/e2e/incremental-security.spec.js b/test/e2e/incremental-security.spec.js index ecd6f5999..3a44badd6 100644 --- a/test/e2e/incremental-security.spec.js +++ b/test/e2e/incremental-security.spec.js @@ -19,8 +19,8 @@ const { loadExtension, openNewPage, verboseReportOnFailure, + setupFetchMocking, } = require('./helpers') -const fetchMockResponses = require('./fetch-mocks.js') describe('MetaMask', function () { let extensionId @@ -65,31 +65,7 @@ describe('MetaMask', function () { }) beforeEach(async function () { - await driver.executeScript( - 'window.origFetch = window.fetch.bind(window);' + - 'window.fetch = ' + - '(...args) => { ' + - 'if (args[0] === "https://ethgasstation.info/json/ethgasAPI.json") { return ' + - 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.ethGasBasic + '\')) }); } else if ' + - '(args[0] === "https://ethgasstation.info/json/predictTable.json") { return ' + - 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.ethGasPredictTable + '\')) }); } else if ' + - '(args[0].match(/chromeextensionmm/)) { return ' + - 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.metametrics + '\')) }); } else if ' + - '(args[0] === "https://dev.blockscale.net/api/gasexpress.json") { return ' + - 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.gasExpress + '\')) }); } ' + - 'return window.origFetch(...args); };' + - 'function cancelInfuraRequest(requestDetails) {' + - 'console.log("Canceling: " + requestDetails.url);' + - 'return {' + - 'cancel: true' + - '};' + - ' }' + - 'window.chrome && window.chrome.webRequest && window.chrome.webRequest.onBeforeRequest.addListener(' + - 'cancelInfuraRequest,' + - '{urls: ["https://*.infura.io/*"]},' + - '["blocking"]' + - ');' - ) + await setupFetchMocking(driver) }) afterEach(async function () { diff --git a/test/e2e/metamask-responsive-ui.spec.js b/test/e2e/metamask-responsive-ui.spec.js index fa7425d61..a7535a47a 100644 --- a/test/e2e/metamask-responsive-ui.spec.js +++ b/test/e2e/metamask-responsive-ui.spec.js @@ -17,8 +17,8 @@ const { findElements, loadExtension, verboseReportOnFailure, + setupFetchMocking, } = require('./helpers') -const fetchMockResponses = require('./fetch-mocks.js') describe('MetaMask', function () { let extensionId @@ -63,31 +63,7 @@ describe('MetaMask', function () { }) beforeEach(async function () { - await driver.executeScript( - 'window.origFetch = window.fetch.bind(window);' + - 'window.fetch = ' + - '(...args) => { ' + - 'if (args[0] === "https://ethgasstation.info/json/ethgasAPI.json") { return ' + - 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.ethGasBasic + '\')) }); } else if ' + - '(args[0] === "https://ethgasstation.info/json/predictTable.json") { return ' + - 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.ethGasPredictTable + '\')) }); } else if ' + - '(args[0].match(/chromeextensionmm/)) { return ' + - 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.metametrics + '\')) }); } else if ' + - '(args[0] === "https://dev.blockscale.net/api/gasexpress.json") { return ' + - 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.gasExpress + '\')) }); } ' + - 'return window.origFetch(...args); };' + - 'function cancelInfuraRequest(requestDetails) {' + - 'console.log("Canceling: " + requestDetails.url);' + - 'return {' + - 'cancel: true' + - '};' + - ' }' + - 'window.chrome && window.chrome.webRequest && window.chrome.webRequest.onBeforeRequest.addListener(' + - 'cancelInfuraRequest,' + - '{urls: ["https://*.infura.io/*"]},' + - '["blocking"]' + - ');' - ) + await setupFetchMocking(driver) }) afterEach(async function () { diff --git a/test/e2e/metamask-ui.spec.js b/test/e2e/metamask-ui.spec.js index 90f320904..031d2bb14 100644 --- a/test/e2e/metamask-ui.spec.js +++ b/test/e2e/metamask-ui.spec.js @@ -21,8 +21,8 @@ const { switchToWindowWithTitle, verboseReportOnFailure, waitUntilXWindowHandles, + setupFetchMocking, } = require('./helpers') -const fetchMockResponses = require('./fetch-mocks.js') describe('MetaMask', function () { let extensionId @@ -68,31 +68,7 @@ describe('MetaMask', function () { }) beforeEach(async function () { - await driver.executeScript( - 'window.origFetch = window.fetch.bind(window);' + - 'window.fetch = ' + - '(...args) => { ' + - 'if (args[0] === "https://ethgasstation.info/json/ethgasAPI.json") { return ' + - 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.ethGasBasic + '\')) }); } else if ' + - '(args[0] === "https://ethgasstation.info/json/predictTable.json") { return ' + - 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.ethGasPredictTable + '\')) }); } else if ' + - '(args[0].match(/chromeextensionmm/)) { return ' + - 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.metametrics + '\')) }); } else if ' + - '(args[0] === "https://dev.blockscale.net/api/gasexpress.json") { return ' + - 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.gasExpress + '\')) }); } ' + - 'return window.origFetch(...args); };' + - 'function cancelInfuraRequest(requestDetails) {' + - 'console.log("Canceling: " + requestDetails.url);' + - 'return {' + - 'cancel: true' + - '};' + - ' }' + - 'window.chrome && window.chrome.webRequest && window.chrome.webRequest.onBeforeRequest.addListener(' + - 'cancelInfuraRequest,' + - '{urls: ["https://*.infura.io/*"]},' + - '["blocking"]' + - ');' - ) + await setupFetchMocking(driver) }) afterEach(async function () { diff --git a/test/e2e/send-edit.spec.js b/test/e2e/send-edit.spec.js index 4eca232ee..bc5160112 100644 --- a/test/e2e/send-edit.spec.js +++ b/test/e2e/send-edit.spec.js @@ -16,9 +16,8 @@ const { verboseReportOnFailure, findElement, findElements, + setupFetchMocking, } = require('./helpers') -const fetchMockResponses = require('./fetch-mocks.js') - describe('Using MetaMask with an existing account', function () { let extensionId @@ -63,31 +62,7 @@ describe('Using MetaMask with an existing account', function () { }) beforeEach(async function () { - await driver.executeScript( - 'window.origFetch = window.fetch.bind(window);' + - 'window.fetch = ' + - '(...args) => { ' + - 'if (args[0] === "https://ethgasstation.info/json/ethgasAPI.json") { return ' + - 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.ethGasBasic + '\')) }); } else if ' + - '(args[0] === "https://ethgasstation.info/json/predictTable.json") { return ' + - 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.ethGasPredictTable + '\')) }); } else if ' + - '(args[0].match(/chromeextensionmm/)) { return ' + - 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.metametrics + '\')) }); } else if ' + - '(args[0] === "https://dev.blockscale.net/api/gasexpress.json") { return ' + - 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.gasExpress + '\')) }); } ' + - 'return window.origFetch(...args); };' + - 'function cancelInfuraRequest(requestDetails) {' + - 'console.log("Canceling: " + requestDetails.url);' + - 'return {' + - 'cancel: true' + - '};' + - ' }' + - 'window.chrome && window.chrome.webRequest && window.chrome.webRequest.onBeforeRequest.addListener(' + - 'cancelInfuraRequest,' + - '{urls: ["https://*.infura.io/*"]},' + - '["blocking"]' + - ');' - ) + await setupFetchMocking(driver) }) afterEach(async function () { diff --git a/test/e2e/web3.spec.js b/test/e2e/web3.spec.js index 42f235045..ae1c3091e 100644 --- a/test/e2e/web3.spec.js +++ b/test/e2e/web3.spec.js @@ -19,9 +19,8 @@ const { switchToWindowWithTitle, verboseReportOnFailure, waitUntilXWindowHandles, + setupFetchMocking, } = require('./helpers') -const fetchMockResponses = require('./fetch-mocks.js') - describe('Using MetaMask with an existing account', function () { let extensionId @@ -77,31 +76,7 @@ describe('Using MetaMask with an existing account', function () { }) beforeEach(async function () { - await driver.executeScript( - 'window.origFetch = window.fetch.bind(window);' + - 'window.fetch = ' + - '(...args) => { ' + - 'if (args[0] === "https://ethgasstation.info/json/ethgasAPI.json") { return ' + - 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.ethGasBasic + '\')) }); } else if ' + - '(args[0] === "https://ethgasstation.info/json/predictTable.json") { return ' + - 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.ethGasPredictTable + '\')) }); } else if ' + - '(args[0].match(/chromeextensionmm/)) { return ' + - 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.metametrics + '\')) }); } else if ' + - '(args[0] === "https://dev.blockscale.net/api/gasexpress.json") { return ' + - 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.gasExpress + '\')) }); } ' + - 'return window.origFetch(...args); };' + - 'function cancelInfuraRequest(requestDetails) {' + - 'console.log("Canceling: " + requestDetails.url);' + - 'return {' + - 'cancel: true' + - '};' + - ' }' + - 'window.chrome && window.chrome.webRequest && window.chrome.webRequest.onBeforeRequest.addListener(' + - 'cancelInfuraRequest,' + - '{urls: ["https://*.infura.io/*"]},' + - '["blocking"]' + - ');' - ) + await setupFetchMocking(driver) }) afterEach(async function () {