2019-12-11 20:14:58 +01:00
|
|
|
const { Browser } = require('selenium-webdriver')
|
2020-01-13 16:07:32 +01:00
|
|
|
const Driver = require('./driver')
|
2019-12-11 20:14:58 +01:00
|
|
|
const ChromeDriver = require('./chrome')
|
|
|
|
const FirefoxDriver = require('./firefox')
|
2020-01-13 16:07:32 +01:00
|
|
|
const fetchMockResponses = require('../../data/fetch-mocks.json')
|
2019-12-11 20:14:58 +01:00
|
|
|
|
2020-01-13 16:07:32 +01:00
|
|
|
async function buildWebDriver ({ browser, extensionPath, responsive, port }) {
|
2019-12-11 20:14:58 +01:00
|
|
|
switch (browser) {
|
|
|
|
case Browser.CHROME: {
|
2020-01-07 15:01:06 +01:00
|
|
|
const { driver, extensionId, extensionUrl } = await ChromeDriver.build({ extensionPath, responsive, port })
|
2020-01-13 16:07:32 +01:00
|
|
|
setupFetchMocking(driver)
|
|
|
|
await driver.get(extensionUrl)
|
2019-12-11 20:14:58 +01:00
|
|
|
|
|
|
|
return {
|
2020-01-13 16:07:32 +01:00
|
|
|
driver: new Driver(driver, browser),
|
2019-12-11 20:14:58 +01:00
|
|
|
extensionId,
|
|
|
|
extensionUrl,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case Browser.FIREFOX: {
|
2020-01-07 15:01:06 +01:00
|
|
|
const { driver, extensionId, extensionUrl } = await FirefoxDriver.build({ extensionPath, responsive, port })
|
2020-01-13 16:07:32 +01:00
|
|
|
setupFetchMocking(driver)
|
|
|
|
await driver.get(extensionUrl)
|
2019-12-11 20:14:58 +01:00
|
|
|
|
|
|
|
return {
|
2020-01-13 16:07:32 +01:00
|
|
|
driver: new Driver(driver, browser),
|
2019-12-11 20:14:58 +01:00
|
|
|
extensionId,
|
|
|
|
extensionUrl,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
throw new Error(`Unrecognized browser: ${browser}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-13 16:07:32 +01:00
|
|
|
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) }
|
|
|
|
}
|
|
|
|
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})`)
|
|
|
|
}
|
|
|
|
|
2019-12-11 20:14:58 +01:00
|
|
|
module.exports = {
|
|
|
|
buildWebDriver,
|
|
|
|
}
|