|
|
@ -1,9 +1,23 @@ |
|
|
|
const fs = require('fs') |
|
|
|
const fs = require('fs') |
|
|
|
|
|
|
|
const path = require('path') |
|
|
|
const mkdirp = require('mkdirp') |
|
|
|
const mkdirp = require('mkdirp') |
|
|
|
const pify = require('pify') |
|
|
|
const pify = require('pify') |
|
|
|
const assert = require('assert') |
|
|
|
const assert = require('assert') |
|
|
|
const { delay } = require('./func') |
|
|
|
|
|
|
|
|
|
|
|
const { |
|
|
|
|
|
|
|
delay, |
|
|
|
|
|
|
|
getExtensionIdChrome, |
|
|
|
|
|
|
|
getExtensionIdFirefox, |
|
|
|
|
|
|
|
buildChromeWebDriver, |
|
|
|
|
|
|
|
buildFirefoxWebdriver, |
|
|
|
|
|
|
|
installWebExt, |
|
|
|
|
|
|
|
} = require('./func') |
|
|
|
const { until } = require('selenium-webdriver') |
|
|
|
const { until } = require('selenium-webdriver') |
|
|
|
|
|
|
|
const fetchMockResponses = require('./fetch-mocks.json') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const tinyDelayMs = 200 |
|
|
|
|
|
|
|
const regularDelayMs = tinyDelayMs * 2 |
|
|
|
|
|
|
|
const largeDelayMs = regularDelayMs * 2 |
|
|
|
|
|
|
|
|
|
|
|
module.exports = { |
|
|
|
module.exports = { |
|
|
|
assertElementNotPresent, |
|
|
|
assertElementNotPresent, |
|
|
@ -17,6 +31,86 @@ module.exports = { |
|
|
|
switchToWindowWithUrlThatMatches, |
|
|
|
switchToWindowWithUrlThatMatches, |
|
|
|
verboseReportOnFailure, |
|
|
|
verboseReportOnFailure, |
|
|
|
waitUntilXWindowHandles, |
|
|
|
waitUntilXWindowHandles, |
|
|
|
|
|
|
|
setupFetchMocking, |
|
|
|
|
|
|
|
prepareExtensionForTesting, |
|
|
|
|
|
|
|
tinyDelayMs, |
|
|
|
|
|
|
|
regularDelayMs, |
|
|
|
|
|
|
|
largeDelayMs, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function prepareExtensionForTesting ({ responsive } = {}) { |
|
|
|
|
|
|
|
let driver, extensionId, extensionUrl |
|
|
|
|
|
|
|
const targetBrowser = process.env.SELENIUM_BROWSER |
|
|
|
|
|
|
|
switch (targetBrowser) { |
|
|
|
|
|
|
|
case 'chrome': { |
|
|
|
|
|
|
|
const extPath = path.resolve('dist/chrome') |
|
|
|
|
|
|
|
driver = buildChromeWebDriver(extPath, { responsive }) |
|
|
|
|
|
|
|
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 }) |
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// wait an extra long time so any slow popups can trigger
|
|
|
|
|
|
|
|
await delay(4 * largeDelayMs) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 (fetchMockResponses) { |
|
|
|
|
|
|
|
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 () => clone(fetchMockResponses.ethGasBasic) } |
|
|
|
|
|
|
|
} else if (url === 'https://ethgasstation.info/json/predictTable.json') { |
|
|
|
|
|
|
|
return { json: async () => clone(fetchMockResponses.ethGasPredictTable) } |
|
|
|
|
|
|
|
} else if (url.match(/chromeextensionmm/)) { |
|
|
|
|
|
|
|
return { json: async () => clone(fetchMockResponses.metametrics) } |
|
|
|
|
|
|
|
} else if (url === 'https://dev.blockscale.net/api/gasexpress.json') { |
|
|
|
|
|
|
|
return { json: async () => clone(fetchMockResponses.gasExpress) } |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return window.origFetch(...args) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (window.chrome && window.chrome.webRequest) { |
|
|
|
|
|
|
|
window.chrome.webRequest.onBeforeRequest.addListener(cancelInfuraRequest, {urls: ['https://*.infura.io/*']}, ['blocking']) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
function cancelInfuraRequest (requestDetails) { |
|
|
|
|
|
|
|
console.log(`fetchMocking - Canceling request: "${requestDetails.url}"`) |
|
|
|
|
|
|
|
return { cancel: true } |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
function clone (obj) { |
|
|
|
|
|
|
|
return JSON.parse(JSON.stringify(obj)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// fetchMockResponses are parsed last minute to ensure that objects are uniquely instantiated
|
|
|
|
|
|
|
|
const fetchMockResponsesJson = JSON.stringify(fetchMockResponses) |
|
|
|
|
|
|
|
// eval the fetchMocking script in the browser
|
|
|
|
|
|
|
|
await driver.executeScript(`(${fetchMocking})(${fetchMockResponsesJson})`) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function loadExtension (driver, extensionId) { |
|
|
|
async function loadExtension (driver, extensionId) { |
|
|
|