2020-01-20 19:50:25 +01:00
|
|
|
const path = require('path')
|
2020-08-18 21:18:25 +02:00
|
|
|
const createStaticServer = require('../../development/create-static-server')
|
2020-01-20 19:50:25 +01:00
|
|
|
const Ganache = require('./ganache')
|
|
|
|
const FixtureServer = require('./fixture-server')
|
|
|
|
const { buildWebDriver } = require('./webdriver')
|
|
|
|
|
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
|
|
|
|
2020-07-10 05:57:54 +02:00
|
|
|
const dappPort = 8080
|
|
|
|
|
2020-01-20 19:50:25 +01:00
|
|
|
async function withFixtures (options, callback) {
|
2020-07-10 05:57:54 +02:00
|
|
|
const { dapp, fixtures, ganacheOptions, driverOptions, title } = options
|
2020-01-20 19:50:25 +01:00
|
|
|
const fixtureServer = new FixtureServer()
|
|
|
|
const ganacheServer = new Ganache()
|
2020-07-10 05:57:54 +02:00
|
|
|
let dappServer
|
2020-01-20 19:50:25 +01:00
|
|
|
|
|
|
|
let webDriver
|
|
|
|
try {
|
|
|
|
await ganacheServer.start(ganacheOptions)
|
|
|
|
await fixtureServer.start()
|
|
|
|
await fixtureServer.loadState(path.join(__dirname, 'fixtures', fixtures))
|
2020-07-10 05:57:54 +02:00
|
|
|
if (dapp) {
|
|
|
|
const dappDirectory = path.resolve(__dirname, '..', '..', 'node_modules', '@metamask', 'test-dapp', 'dist')
|
|
|
|
dappServer = createStaticServer(dappDirectory)
|
|
|
|
dappServer.listen(dappPort)
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
dappServer.on('listening', resolve)
|
|
|
|
dappServer.on('error', reject)
|
|
|
|
})
|
|
|
|
}
|
2020-01-20 19:50:25 +01:00
|
|
|
const { driver } = await buildWebDriver(driverOptions)
|
|
|
|
webDriver = driver
|
|
|
|
|
2020-07-18 00:49:52 +02:00
|
|
|
// eslint-disable-next-line callback-return
|
2020-01-20 19:50:25 +01:00
|
|
|
await callback({
|
|
|
|
driver,
|
|
|
|
})
|
2020-07-09 17:01:39 +02:00
|
|
|
|
|
|
|
if (process.env.SELENIUM_BROWSER === 'chrome') {
|
|
|
|
const errors = await driver.checkBrowserForConsoleErrors(driver)
|
|
|
|
if (errors.length) {
|
|
|
|
const errorReports = errors.map((err) => err.message)
|
|
|
|
const errorMessage = `Errors found in browser console:\n${errorReports.join('\n')}`
|
|
|
|
throw new Error(errorMessage)
|
|
|
|
}
|
|
|
|
}
|
2020-07-09 17:42:56 +02:00
|
|
|
} catch (error) {
|
2020-07-22 20:25:04 +02:00
|
|
|
if (webDriver) {
|
|
|
|
await webDriver.verboseReportOnFailure(title)
|
|
|
|
}
|
2020-07-09 17:42:56 +02:00
|
|
|
throw error
|
2020-01-20 19:50:25 +01:00
|
|
|
} finally {
|
|
|
|
await fixtureServer.stop()
|
|
|
|
await ganacheServer.quit()
|
|
|
|
if (webDriver) {
|
|
|
|
await webDriver.quit()
|
|
|
|
}
|
2020-07-10 05:57:54 +02:00
|
|
|
if (dappServer) {
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
dappServer.close((error) => {
|
|
|
|
if (error) {
|
|
|
|
return reject(error)
|
|
|
|
}
|
|
|
|
return resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2020-01-20 19:50:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-25 03:17:26 +02:00
|
|
|
module.exports = {
|
2019-09-07 09:13:58 +02:00
|
|
|
tinyDelayMs,
|
|
|
|
regularDelayMs,
|
|
|
|
largeDelayMs,
|
2020-01-20 19:50:25 +01:00
|
|
|
withFixtures,
|
2019-09-07 09:13:58 +02:00
|
|
|
}
|