2018-05-25 03:17:26 +02:00
|
|
|
const fs = require('fs')
|
|
|
|
const mkdirp = require('mkdirp')
|
|
|
|
const pify = require('pify')
|
2018-07-10 17:43:17 +02:00
|
|
|
const assert = require('assert')
|
2019-12-11 20:14:58 +01:00
|
|
|
const { until } = require('selenium-webdriver')
|
2019-09-07 09:13:58 +02:00
|
|
|
|
2019-12-11 20:14:58 +01:00
|
|
|
const { buildWebDriver } = require('./webdriver')
|
2019-09-10 17:04:03 +02:00
|
|
|
const fetchMockResponses = require('./fetch-mocks.json')
|
2019-09-07 08:44:22 +02:00
|
|
|
|
2019-09-07 09:13:58 +02:00
|
|
|
const tinyDelayMs = 200
|
|
|
|
const regularDelayMs = tinyDelayMs * 2
|
|
|
|
const largeDelayMs = regularDelayMs * 2
|
2018-05-25 03:17:26 +02:00
|
|
|
|
|
|
|
module.exports = {
|
2018-07-10 17:43:17 +02:00
|
|
|
assertElementNotPresent,
|
2018-05-25 03:17:26 +02:00
|
|
|
checkBrowserForConsoleErrors,
|
2018-07-07 00:55:30 +02:00
|
|
|
closeAllWindowHandlesExcept,
|
2019-12-10 20:23:25 +01:00
|
|
|
delay,
|
2018-06-06 04:21:55 +02:00
|
|
|
findElement,
|
|
|
|
findElements,
|
2018-06-21 14:19:27 +02:00
|
|
|
openNewPage,
|
2018-07-07 00:55:30 +02:00
|
|
|
switchToWindowWithTitle,
|
|
|
|
verboseReportOnFailure,
|
|
|
|
waitUntilXWindowHandles,
|
2019-09-07 08:44:22 +02:00
|
|
|
setupFetchMocking,
|
2019-09-07 09:13:58 +02:00
|
|
|
prepareExtensionForTesting,
|
|
|
|
tinyDelayMs,
|
|
|
|
regularDelayMs,
|
|
|
|
largeDelayMs,
|
|
|
|
}
|
|
|
|
|
2020-01-07 15:01:06 +01:00
|
|
|
async function prepareExtensionForTesting ({ responsive, port } = {}) {
|
2019-12-11 20:14:58 +01:00
|
|
|
const browser = process.env.SELENIUM_BROWSER
|
|
|
|
const extensionPath = `dist/${browser}`
|
2020-01-07 15:01:06 +01:00
|
|
|
const { driver, extensionId, extensionUrl } = await buildWebDriver({ browser, extensionPath, responsive, port })
|
2019-12-11 20:14:58 +01:00
|
|
|
|
2019-09-07 09:13:58 +02:00
|
|
|
// Depending on the state of the application built into the above directory (extPath) and the value of
|
|
|
|
// METAMASK_DEBUG we will see different post-install behaviour and possibly some extra windows. Here we
|
|
|
|
// are closing any extraneous windows to reset us to a single window before continuing.
|
2019-09-10 13:53:21 +02:00
|
|
|
|
|
|
|
// wait an extra long time so any slow popups can trigger
|
|
|
|
await delay(4 * largeDelayMs)
|
|
|
|
|
2019-09-07 09:13:58 +02:00
|
|
|
const [tab1] = await driver.getAllWindowHandles()
|
|
|
|
await closeAllWindowHandlesExcept(driver, [tab1])
|
|
|
|
await driver.switchTo().window(tab1)
|
|
|
|
await driver.get(extensionUrl)
|
|
|
|
|
|
|
|
return { driver, extensionId, extensionUrl }
|
2019-09-07 08:44:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function setupFetchMocking (driver) {
|
2019-09-07 09:13:58 +02:00
|
|
|
// define fetchMocking script, to be evaluated in the browser
|
2019-09-10 17:04:03 +02:00
|
|
|
function fetchMocking (fetchMockResponses) {
|
2019-09-07 08:44:22 +02:00
|
|
|
window.origFetch = window.fetch.bind(window)
|
|
|
|
window.fetch = async (...args) => {
|
|
|
|
const url = args[0]
|
2019-09-07 09:13:58 +02:00
|
|
|
if (url === 'https://ethgasstation.info/json/ethgasAPI.json') {
|
2019-09-10 17:04:03 +02:00
|
|
|
return { json: async () => clone(fetchMockResponses.ethGasBasic) }
|
2019-09-07 09:13:58 +02:00
|
|
|
} else if (url === 'https://ethgasstation.info/json/predictTable.json') {
|
2019-09-10 17:04:03 +02:00
|
|
|
return { json: async () => clone(fetchMockResponses.ethGasPredictTable) }
|
2019-09-07 08:44:22 +02:00
|
|
|
} else if (url.match(/chromeextensionmm/)) {
|
2019-09-10 17:04:03 +02:00
|
|
|
return { json: async () => clone(fetchMockResponses.metametrics) }
|
2019-09-07 08:44:22 +02:00
|
|
|
}
|
|
|
|
return window.origFetch(...args)
|
|
|
|
}
|
2019-09-10 17:04:03 +02:00
|
|
|
if (window.chrome && window.chrome.webRequest) {
|
2019-12-03 21:50:55 +01:00
|
|
|
window.chrome.webRequest.onBeforeRequest.addListener(cancelInfuraRequest, { urls: ['https://*.infura.io/*'] }, ['blocking'])
|
2019-09-10 17:04:03 +02:00
|
|
|
}
|
2019-09-07 09:13:58 +02:00
|
|
|
function cancelInfuraRequest (requestDetails) {
|
2019-09-07 08:44:22 +02:00
|
|
|
console.log(`fetchMocking - Canceling request: "${requestDetails.url}"`)
|
|
|
|
return { cancel: true }
|
|
|
|
}
|
2019-09-10 17:04:03 +02:00
|
|
|
function clone (obj) {
|
|
|
|
return JSON.parse(JSON.stringify(obj))
|
2019-09-07 08:44:22 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-10 17:04:03 +02:00
|
|
|
// fetchMockResponses are parsed last minute to ensure that objects are uniquely instantiated
|
|
|
|
const fetchMockResponsesJson = JSON.stringify(fetchMockResponses)
|
2019-09-07 08:44:22 +02:00
|
|
|
// eval the fetchMocking script in the browser
|
2019-09-10 17:04:03 +02:00
|
|
|
await driver.executeScript(`(${fetchMocking})(${fetchMockResponsesJson})`)
|
2018-05-25 03:17:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function checkBrowserForConsoleErrors (driver) {
|
|
|
|
const ignoredLogTypes = ['WARNING']
|
|
|
|
const ignoredErrorMessages = [
|
|
|
|
// React throws error warnings on "dataset", but still sets the data-* properties correctly
|
|
|
|
'Warning: Unknown prop `dataset` on ',
|
|
|
|
// Third-party Favicon 404s show up as errors
|
|
|
|
'favicon.ico - Failed to load resource: the server responded with a status of 404 (Not Found)',
|
|
|
|
// React Development build - known issue blocked by test build sys
|
|
|
|
'Warning: It looks like you\'re using a minified copy of the development build of React.',
|
|
|
|
// Redux Development build - known issue blocked by test build sys
|
|
|
|
'This means that you are running a slower development build of Redux.',
|
|
|
|
]
|
|
|
|
const browserLogs = await driver.manage().logs().get('browser')
|
|
|
|
const errorEntries = browserLogs.filter(entry => !ignoredLogTypes.includes(entry.level.toString()))
|
|
|
|
const errorObjects = errorEntries.map(entry => entry.toJSON())
|
|
|
|
return errorObjects.filter(entry => !ignoredErrorMessages.some(message => entry.message.includes(message)))
|
|
|
|
}
|
|
|
|
|
|
|
|
async function verboseReportOnFailure (driver, test) {
|
|
|
|
let artifactDir
|
|
|
|
if (process.env.SELENIUM_BROWSER === 'chrome') {
|
|
|
|
artifactDir = `./test-artifacts/chrome/${test.title}`
|
|
|
|
} else if (process.env.SELENIUM_BROWSER === 'firefox') {
|
|
|
|
artifactDir = `./test-artifacts/firefox/${test.title}`
|
|
|
|
}
|
|
|
|
const filepathBase = `${artifactDir}/test-failure`
|
|
|
|
await pify(mkdirp)(artifactDir)
|
|
|
|
const screenshot = await driver.takeScreenshot()
|
|
|
|
await pify(fs.writeFile)(`${filepathBase}-screenshot.png`, screenshot, { encoding: 'base64' })
|
|
|
|
const htmlSource = await driver.getPageSource()
|
|
|
|
await pify(fs.writeFile)(`${filepathBase}-dom.html`, htmlSource)
|
|
|
|
}
|
2018-06-06 04:21:55 +02:00
|
|
|
|
2019-12-10 20:23:25 +01:00
|
|
|
function delay (time) {
|
|
|
|
return new Promise(resolve => setTimeout(resolve, time))
|
|
|
|
}
|
|
|
|
|
2018-06-06 04:21:55 +02:00
|
|
|
async function findElement (driver, by, timeout = 10000) {
|
|
|
|
return driver.wait(until.elementLocated(by), timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function findElements (driver, by, timeout = 10000) {
|
|
|
|
return driver.wait(until.elementsLocated(by), timeout)
|
|
|
|
}
|
2018-06-21 14:19:27 +02:00
|
|
|
|
|
|
|
async function openNewPage (driver, url) {
|
|
|
|
await driver.executeScript('window.open()')
|
|
|
|
await delay(1000)
|
|
|
|
|
|
|
|
const handles = await driver.getAllWindowHandles()
|
2018-07-07 00:55:30 +02:00
|
|
|
const lastHandle = handles[handles.length - 1]
|
|
|
|
await driver.switchTo().window(lastHandle)
|
2018-06-21 14:19:27 +02:00
|
|
|
|
|
|
|
await driver.get(url)
|
|
|
|
await delay(1000)
|
|
|
|
}
|
2018-07-07 00:55:30 +02:00
|
|
|
|
2018-11-19 18:13:49 +01:00
|
|
|
async function waitUntilXWindowHandles (driver, x, delayStep = 1000, timeout = 5000) {
|
|
|
|
let timeElapsed = 0
|
|
|
|
async function _pollWindowHandles () {
|
|
|
|
const windowHandles = await driver.getAllWindowHandles()
|
|
|
|
if (windowHandles.length === x) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
await delay(delayStep)
|
|
|
|
timeElapsed += delayStep
|
|
|
|
if (timeElapsed > timeout) {
|
|
|
|
throw new Error('waitUntilXWindowHandles timed out polling window handles')
|
|
|
|
} else {
|
|
|
|
await _pollWindowHandles()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return await _pollWindowHandles()
|
2018-07-07 00:55:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function switchToWindowWithTitle (driver, title, windowHandles) {
|
|
|
|
if (!windowHandles) {
|
|
|
|
windowHandles = await driver.getAllWindowHandles()
|
|
|
|
} else if (windowHandles.length === 0) {
|
|
|
|
throw new Error('No window with title: ' + title)
|
|
|
|
}
|
|
|
|
const firstHandle = windowHandles[0]
|
|
|
|
await driver.switchTo().window(firstHandle)
|
|
|
|
const handleTitle = await driver.getTitle()
|
|
|
|
|
|
|
|
if (handleTitle === title) {
|
|
|
|
return firstHandle
|
|
|
|
} else {
|
|
|
|
return await switchToWindowWithTitle(driver, title, windowHandles.slice(1))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-26 21:43:01 +01:00
|
|
|
/**
|
|
|
|
* Closes all windows except those in the given list of exceptions
|
|
|
|
* @param {object} driver the WebDriver instance
|
|
|
|
* @param {string|Array<string>} exceptions the list of window handle exceptions
|
|
|
|
* @param {Array?} windowHandles the full list of window handles
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
2018-07-07 00:55:30 +02:00
|
|
|
async function closeAllWindowHandlesExcept (driver, exceptions, windowHandles) {
|
|
|
|
exceptions = typeof exceptions === 'string' ? [ exceptions ] : exceptions
|
|
|
|
windowHandles = windowHandles || await driver.getAllWindowHandles()
|
|
|
|
const lastWindowHandle = windowHandles.pop()
|
|
|
|
if (!exceptions.includes(lastWindowHandle)) {
|
|
|
|
await driver.switchTo().window(lastWindowHandle)
|
|
|
|
await delay(1000)
|
|
|
|
await driver.close()
|
|
|
|
await delay(1000)
|
|
|
|
}
|
|
|
|
return windowHandles.length && await closeAllWindowHandlesExcept(driver, exceptions, windowHandles)
|
|
|
|
}
|
2018-07-10 17:43:17 +02:00
|
|
|
|
|
|
|
async function assertElementNotPresent (webdriver, driver, by) {
|
2018-08-14 04:07:02 +02:00
|
|
|
let dataTab
|
2018-07-10 17:43:17 +02:00
|
|
|
try {
|
2018-08-14 04:07:02 +02:00
|
|
|
dataTab = await findElement(driver, by, 4000)
|
2018-07-10 17:43:17 +02:00
|
|
|
} catch (err) {
|
2018-08-14 04:07:02 +02:00
|
|
|
assert(err instanceof webdriver.error.NoSuchElementError || err instanceof webdriver.error.TimeoutError)
|
|
|
|
}
|
2018-10-01 16:58:24 +02:00
|
|
|
assert.ok(!dataTab, 'Found element that should not be present')
|
2018-07-10 17:43:17 +02:00
|
|
|
}
|