mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
test - e2e - dedupe fetchMocking + compose script as fn
This commit is contained in:
parent
b884cd573c
commit
54491974ec
@ -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 () {
|
||||
|
@ -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 () {
|
||||
|
@ -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) {
|
||||
|
@ -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 () {
|
||||
|
@ -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 () {
|
||||
|
@ -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 () {
|
||||
|
@ -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 () {
|
||||
|
@ -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 () {
|
||||
|
Loading…
Reference in New Issue
Block a user