mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-23 02:10:12 +01:00
7a2b3b908a
The e2e test driver used to perform the initial navigation automatically within the `buildWebDriver` function, so that that step wouldn't need to be repeated at the beginning of each test. However this prevented you from doing any setup in the test before the first navigation. The navigation has now been moved into each individual test. It should be functionally equivalent, except now it's possible to control exactly when the first navigation occurs. A 1 second delay was also removed, as it didn't seem to be necessary when testing this. It was initially added as an attempted fix to an intermittent failure. It did not fix that failure.
83 lines
2.6 KiB
JavaScript
83 lines
2.6 KiB
JavaScript
const { Browser } = require('selenium-webdriver')
|
|
const fetchMockResponses = require('../../data/fetch-mocks.json')
|
|
const Driver = require('./driver')
|
|
const ChromeDriver = require('./chrome')
|
|
const FirefoxDriver = require('./firefox')
|
|
|
|
async function buildWebDriver({ responsive, port } = {}) {
|
|
const browser = process.env.SELENIUM_BROWSER
|
|
const extensionPath = `dist/${browser}`
|
|
|
|
const {
|
|
driver: seleniumDriver,
|
|
extensionId,
|
|
extensionUrl,
|
|
} = await buildBrowserWebDriver(browser, { extensionPath, responsive, port })
|
|
await setupFetchMocking(seleniumDriver)
|
|
const driver = new Driver(seleniumDriver, browser, extensionUrl)
|
|
|
|
return {
|
|
driver,
|
|
extensionId,
|
|
}
|
|
}
|
|
|
|
async function buildBrowserWebDriver(browser, webDriverOptions) {
|
|
switch (browser) {
|
|
case Browser.CHROME: {
|
|
return await ChromeDriver.build(webDriverOptions)
|
|
}
|
|
case Browser.FIREFOX: {
|
|
return await FirefoxDriver.build(webDriverOptions)
|
|
}
|
|
default: {
|
|
throw new Error(`Unrecognized browser: ${browser}`)
|
|
}
|
|
}
|
|
}
|
|
|
|
async function setupFetchMocking(driver) {
|
|
// define fetchMocking script, to be evaluated in the browser
|
|
function fetchMocking(mockResponses) {
|
|
window.origFetch = window.fetch.bind(window)
|
|
window.fetch = async (...args) => {
|
|
const url = args[0]
|
|
// api.metaswap.codefi.network/gasPrices
|
|
if (
|
|
url.match(/^http(s)?:\/\/api\.metaswap\.codefi\.network\/gasPrices/u)
|
|
) {
|
|
return { json: async () => clone(mockResponses.gasPricesBasic) }
|
|
} else if (url.match(/chromeextensionmm/u)) {
|
|
return { json: async () => clone(mockResponses.metametrics) }
|
|
} else if (url.match(/^https:\/\/(api\.metaswap|.*airswap-dev)/u)) {
|
|
if (url.match(/featureFlag$/u)) {
|
|
return { json: async () => clone(mockResponses.swaps.featureFlag) }
|
|
}
|
|
}
|
|
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})`)
|
|
}
|
|
|
|
module.exports = {
|
|
buildWebDriver,
|
|
}
|