mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
206920e9bd
There have been intermittent test failures at the beginning of various e2e test runs. Most tests start with waiting for the 'Welcome' button to be visible and enabled, which means waiting for the loading screen to go away. It looks like the reason the test intermittently fails is that sometimes the loading screen doesn't appear until a few moments _after_ the page loads (or that it vanishes and comes back). It was rather difficult to track down each possible cause for the loading screens, so in the meantime a pause has been added at the start of each run. This should hopefully suffice to ensure the momentary gap in loading has been passed by the time the first test starts up.
73 lines
2.5 KiB
JavaScript
73 lines
2.5 KiB
JavaScript
const { Browser } = require('selenium-webdriver')
|
|
const Driver = require('./driver')
|
|
const ChromeDriver = require('./chrome')
|
|
const FirefoxDriver = require('./firefox')
|
|
const fetchMockResponses = require('../../data/fetch-mocks.json')
|
|
|
|
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 })
|
|
setupFetchMocking(seleniumDriver)
|
|
const driver = new Driver(seleniumDriver, browser, extensionUrl)
|
|
await driver.navigate()
|
|
|
|
await driver.delay(1000)
|
|
|
|
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 (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})`)
|
|
}
|
|
|
|
module.exports = {
|
|
buildWebDriver,
|
|
}
|