2021-02-04 19:15:23 +01:00
|
|
|
const { Browser } = require('selenium-webdriver');
|
|
|
|
const fetchMockResponses = require('../../data/fetch-mocks.json');
|
|
|
|
const Driver = require('./driver');
|
|
|
|
const ChromeDriver = require('./chrome');
|
|
|
|
const FirefoxDriver = require('./firefox');
|
2019-12-11 20:14:58 +01:00
|
|
|
|
2022-03-10 23:01:55 +01:00
|
|
|
async function buildWebDriver({ responsive, port, type } = {}) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const browser = process.env.SELENIUM_BROWSER;
|
2020-01-14 18:34:38 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const {
|
|
|
|
driver: seleniumDriver,
|
|
|
|
extensionId,
|
|
|
|
extensionUrl,
|
2022-03-10 23:01:55 +01:00
|
|
|
} = await buildBrowserWebDriver(browser, { responsive, port, type });
|
2021-02-04 19:15:23 +01:00
|
|
|
await setupFetchMocking(seleniumDriver);
|
|
|
|
const driver = new Driver(seleniumDriver, browser, extensionUrl);
|
2020-01-20 23:40:01 +01:00
|
|
|
|
2020-01-16 19:35:50 +01:00
|
|
|
return {
|
|
|
|
driver,
|
|
|
|
extensionId,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-01-16 19:35:50 +01:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async function buildBrowserWebDriver(browser, webDriverOptions) {
|
2019-12-11 20:14:58 +01:00
|
|
|
switch (browser) {
|
|
|
|
case Browser.CHROME: {
|
2021-02-04 19:15:23 +01:00
|
|
|
return await ChromeDriver.build(webDriverOptions);
|
2019-12-11 20:14:58 +01:00
|
|
|
}
|
|
|
|
case Browser.FIREFOX: {
|
2021-02-04 19:15:23 +01:00
|
|
|
return await FirefoxDriver.build(webDriverOptions);
|
2019-12-11 20:14:58 +01:00
|
|
|
}
|
|
|
|
default: {
|
2021-02-04 19:15:23 +01:00
|
|
|
throw new Error(`Unrecognized browser: ${browser}`);
|
2019-12-11 20:14:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async function setupFetchMocking(driver) {
|
2020-01-13 16:07:32 +01:00
|
|
|
// define fetchMocking script, to be evaluated in the browser
|
2020-11-03 00:41:28 +01:00
|
|
|
function fetchMocking(mockResponses) {
|
2021-02-04 19:15:23 +01:00
|
|
|
window.origFetch = window.fetch.bind(window);
|
2020-01-13 16:07:32 +01:00
|
|
|
window.fetch = async (...args) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const url = args[0];
|
2020-12-03 00:25:19 +01:00
|
|
|
// api.metaswap.codefi.network/gasPrices
|
|
|
|
if (
|
|
|
|
url.match(/^http(s)?:\/\/api\.metaswap\.codefi\.network\/gasPrices/u)
|
2020-11-03 00:41:28 +01:00
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return { json: async () => clone(mockResponses.gasPricesBasic) };
|
2020-08-14 13:48:42 +02:00
|
|
|
} else if (url.match(/chromeextensionmm/u)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return { json: async () => clone(mockResponses.metametrics) };
|
2022-03-21 10:26:52 +01:00
|
|
|
} else if (url.match(/^https:\/\/(swap\.metaswap\.codefi\.network)/u)) {
|
2021-07-09 17:24:00 +02:00
|
|
|
if (url.match(/featureFlags$/u)) {
|
|
|
|
return { json: async () => clone(mockResponses.swaps.featureFlags) };
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|
2021-09-09 22:56:27 +02:00
|
|
|
} else if (
|
|
|
|
url.match(/^https:\/\/(token-api\.airswap-prod\.codefi\.network)/u)
|
|
|
|
) {
|
|
|
|
if (url.match(/tokens\/1337$/u)) {
|
|
|
|
return { json: async () => clone(mockResponses.tokenList) };
|
|
|
|
}
|
2020-01-13 16:07:32 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return window.origFetch(...args);
|
|
|
|
};
|
2020-01-13 16:07:32 +01:00
|
|
|
if (window.chrome && window.chrome.webRequest) {
|
2020-11-03 00:41:28 +01:00
|
|
|
window.chrome.webRequest.onBeforeRequest.addListener(
|
|
|
|
cancelInfuraRequest,
|
|
|
|
{ urls: ['https://*.infura.io/*'] },
|
|
|
|
['blocking'],
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-01-13 16:07:32 +01:00
|
|
|
}
|
2020-11-03 00:41:28 +01:00
|
|
|
function cancelInfuraRequest(requestDetails) {
|
2021-02-04 19:15:23 +01:00
|
|
|
console.log(`fetchMocking - Canceling request: "${requestDetails.url}"`);
|
|
|
|
return { cancel: true };
|
2020-01-13 16:07:32 +01:00
|
|
|
}
|
2020-11-03 00:41:28 +01:00
|
|
|
function clone(obj) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return JSON.parse(JSON.stringify(obj));
|
2020-01-13 16:07:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// fetchMockResponses are parsed last minute to ensure that objects are uniquely instantiated
|
2021-02-04 19:15:23 +01:00
|
|
|
const fetchMockResponsesJson = JSON.stringify(fetchMockResponses);
|
2020-01-13 16:07:32 +01:00
|
|
|
// eval the fetchMocking script in the browser
|
2021-02-04 19:15:23 +01:00
|
|
|
await driver.executeScript(`(${fetchMocking})(${fetchMockResponsesJson})`);
|
2020-01-13 16:07:32 +01:00
|
|
|
}
|
|
|
|
|
2019-12-11 20:14:58 +01:00
|
|
|
module.exports = {
|
|
|
|
buildWebDriver,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|