2019-12-11 20:14:58 +01:00
|
|
|
const { Browser } = require('selenium-webdriver')
|
2020-08-18 21:18:25 +02:00
|
|
|
const fetchMockResponses = require('../../data/fetch-mocks.json')
|
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-14 18:34:38 +01:00
|
|
|
async function buildWebDriver ({ responsive, port } = {}) {
|
|
|
|
const browser = process.env.SELENIUM_BROWSER
|
|
|
|
const extensionPath = `dist/${browser}`
|
|
|
|
|
2020-01-16 19:35:50 +01:00
|
|
|
const { driver: seleniumDriver, extensionId, extensionUrl } = await buildBrowserWebDriver(browser, { extensionPath, responsive, port })
|
2020-07-22 14:52:50 +02:00
|
|
|
await setupFetchMocking(seleniumDriver)
|
2020-01-20 18:03:07 +01:00
|
|
|
const driver = new Driver(seleniumDriver, browser, extensionUrl)
|
|
|
|
await driver.navigate()
|
2020-01-16 19:35:50 +01:00
|
|
|
|
2020-01-20 23:40:01 +01:00
|
|
|
await driver.delay(1000)
|
|
|
|
|
2020-01-16 19:35:50 +01:00
|
|
|
return {
|
|
|
|
driver,
|
|
|
|
extensionId,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function buildBrowserWebDriver (browser, webDriverOptions) {
|
2019-12-11 20:14:58 +01:00
|
|
|
switch (browser) {
|
|
|
|
case Browser.CHROME: {
|
2020-01-16 19:35:50 +01:00
|
|
|
return await ChromeDriver.build(webDriverOptions)
|
2019-12-11 20:14:58 +01:00
|
|
|
}
|
|
|
|
case Browser.FIREFOX: {
|
2020-01-16 19:35:50 +01:00
|
|
|
return await FirefoxDriver.build(webDriverOptions)
|
2019-12-11 20:14:58 +01:00
|
|
|
}
|
|
|
|
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
|
2020-08-18 18:36:45 +02:00
|
|
|
function fetchMocking (mockResponses) {
|
2020-01-13 16:07:32 +01:00
|
|
|
window.origFetch = window.fetch.bind(window)
|
|
|
|
window.fetch = async (...args) => {
|
|
|
|
const url = args[0]
|
2020-06-29 19:26:21 +02:00
|
|
|
if (url.match(/^http(s)?:\/\/ethgasstation\.info\/json\/ethgasAPI.*/u)) {
|
2020-08-18 18:36:45 +02:00
|
|
|
return { json: async () => clone(mockResponses.ethGasBasic) }
|
2020-06-29 19:26:21 +02:00
|
|
|
} else if (url.match(/http(s?):\/\/ethgasstation\.info\/json\/predictTable.*/u)) {
|
2020-08-18 18:36:45 +02:00
|
|
|
return { json: async () => clone(mockResponses.ethGasPredictTable) }
|
2020-08-14 13:48:42 +02:00
|
|
|
} else if (url.match(/chromeextensionmm/u)) {
|
2020-08-18 18:36:45 +02:00
|
|
|
return { json: async () => clone(mockResponses.metametrics) }
|
2020-01-13 16:07:32 +01:00
|
|
|
}
|
|
|
|
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,
|
|
|
|
}
|