2021-02-04 19:15:23 +01:00
|
|
|
const path = require('path');
|
|
|
|
const sinon = require('sinon');
|
|
|
|
const createStaticServer = require('../../development/create-static-server');
|
2020-12-01 22:24:56 +01:00
|
|
|
const {
|
|
|
|
createSegmentServer,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = require('../../development/lib/create-segment-server');
|
|
|
|
const Ganache = require('./ganache');
|
|
|
|
const FixtureServer = require('./fixture-server');
|
|
|
|
const { buildWebDriver } = require('./webdriver');
|
2021-09-01 18:40:40 +02:00
|
|
|
const { ensureXServerIsRunning } = require('./x-server');
|
2020-01-20 19:50:25 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const tinyDelayMs = 200;
|
|
|
|
const regularDelayMs = tinyDelayMs * 2;
|
|
|
|
const largeDelayMs = regularDelayMs * 2;
|
2021-08-11 00:09:42 +02:00
|
|
|
const xLargeDelayMs = largeDelayMs * 2;
|
|
|
|
const xxLargeDelayMs = xLargeDelayMs * 2;
|
2018-05-25 03:17:26 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const dappPort = 8080;
|
2020-07-10 05:57:54 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async function withFixtures(options, testSuite) {
|
2020-12-01 22:24:56 +01:00
|
|
|
const {
|
|
|
|
dapp,
|
|
|
|
fixtures,
|
|
|
|
ganacheOptions,
|
|
|
|
driverOptions,
|
|
|
|
mockSegment,
|
|
|
|
title,
|
2021-04-15 15:58:51 +02:00
|
|
|
failOnConsoleError = true,
|
2021-04-26 17:02:29 +02:00
|
|
|
dappPath = undefined,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = options;
|
|
|
|
const fixtureServer = new FixtureServer();
|
|
|
|
const ganacheServer = new Ganache();
|
2021-06-04 15:52:07 +02:00
|
|
|
let secondaryGanacheServer;
|
2021-02-04 19:15:23 +01:00
|
|
|
let dappServer;
|
|
|
|
let segmentServer;
|
|
|
|
let segmentStub;
|
2020-01-20 19:50:25 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let webDriver;
|
2021-06-16 17:12:20 +02:00
|
|
|
let failed = false;
|
2020-01-20 19:50:25 +01:00
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
await ganacheServer.start(ganacheOptions);
|
2021-06-04 15:52:07 +02:00
|
|
|
if (ganacheOptions?.concurrent) {
|
|
|
|
const { port, chainId } = ganacheOptions.concurrent;
|
|
|
|
secondaryGanacheServer = new Ganache();
|
|
|
|
await secondaryGanacheServer.start({
|
|
|
|
blockTime: 2,
|
|
|
|
_chainIdRpc: chainId,
|
|
|
|
port,
|
|
|
|
vmErrorsOnRPCResponse: false,
|
|
|
|
});
|
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
await fixtureServer.start();
|
|
|
|
await fixtureServer.loadState(path.join(__dirname, 'fixtures', fixtures));
|
2020-07-10 05:57:54 +02:00
|
|
|
if (dapp) {
|
2021-04-26 17:02:29 +02:00
|
|
|
let dappDirectory;
|
|
|
|
if (dappPath) {
|
|
|
|
dappDirectory = path.resolve(__dirname, dappPath);
|
|
|
|
} else {
|
|
|
|
dappDirectory = path.resolve(
|
|
|
|
__dirname,
|
|
|
|
'..',
|
|
|
|
'..',
|
|
|
|
'node_modules',
|
|
|
|
'@metamask',
|
|
|
|
'test-dapp',
|
|
|
|
'dist',
|
|
|
|
);
|
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
dappServer = createStaticServer(dappDirectory);
|
|
|
|
dappServer.listen(dappPort);
|
2020-07-10 05:57:54 +02:00
|
|
|
await new Promise((resolve, reject) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
dappServer.on('listening', resolve);
|
|
|
|
dappServer.on('error', reject);
|
|
|
|
});
|
2020-07-10 05:57:54 +02:00
|
|
|
}
|
2020-12-01 22:24:56 +01:00
|
|
|
if (mockSegment) {
|
2021-02-04 19:15:23 +01:00
|
|
|
segmentStub = sinon.stub();
|
2020-12-01 22:24:56 +01:00
|
|
|
segmentServer = createSegmentServer((_request, response, events) => {
|
|
|
|
for (const event of events) {
|
2021-02-04 19:15:23 +01:00
|
|
|
segmentStub(event);
|
2020-12-01 22:24:56 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
response.statusCode = 200;
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
await segmentServer.start(9090);
|
2020-12-01 22:24:56 +01:00
|
|
|
}
|
2021-09-08 20:02:15 +02:00
|
|
|
if (
|
|
|
|
process.env.SELENIUM_BROWSER === 'chrome' &&
|
|
|
|
process.env.CI === 'true'
|
|
|
|
) {
|
2021-09-01 18:40:40 +02:00
|
|
|
await ensureXServerIsRunning();
|
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
const { driver } = await buildWebDriver(driverOptions);
|
|
|
|
webDriver = driver;
|
2020-01-20 19:50:25 +01:00
|
|
|
|
2020-10-21 20:24:51 +02:00
|
|
|
await testSuite({
|
2020-01-20 19:50:25 +01:00
|
|
|
driver,
|
2020-12-03 19:00:50 +01:00
|
|
|
segmentStub,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-07-09 17:01:39 +02:00
|
|
|
|
|
|
|
if (process.env.SELENIUM_BROWSER === 'chrome') {
|
2021-02-04 19:15:23 +01:00
|
|
|
const errors = await driver.checkBrowserForConsoleErrors(driver);
|
2020-07-09 17:01:39 +02:00
|
|
|
if (errors.length) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const errorReports = errors.map((err) => err.message);
|
2020-11-03 00:41:28 +01:00
|
|
|
const errorMessage = `Errors found in browser console:\n${errorReports.join(
|
|
|
|
'\n',
|
2021-02-04 19:15:23 +01:00
|
|
|
)}`;
|
2021-04-15 15:58:51 +02:00
|
|
|
if (failOnConsoleError) {
|
|
|
|
throw new Error(errorMessage);
|
|
|
|
} else {
|
|
|
|
console.error(new Error(errorMessage));
|
|
|
|
}
|
2020-07-09 17:01:39 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-09 17:42:56 +02:00
|
|
|
} catch (error) {
|
2021-06-16 17:12:20 +02:00
|
|
|
failed = true;
|
2020-07-22 20:25:04 +02:00
|
|
|
if (webDriver) {
|
2020-12-01 22:24:56 +01:00
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
await webDriver.verboseReportOnFailure(title);
|
2020-12-01 22:24:56 +01:00
|
|
|
} catch (verboseReportError) {
|
2021-02-04 19:15:23 +01:00
|
|
|
console.error(verboseReportError);
|
2020-12-01 22:24:56 +01:00
|
|
|
}
|
2020-07-22 20:25:04 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
throw error;
|
2020-01-20 19:50:25 +01:00
|
|
|
} finally {
|
2021-06-16 17:12:20 +02:00
|
|
|
if (!failed || process.env.E2E_LEAVE_RUNNING !== 'true') {
|
|
|
|
await fixtureServer.stop();
|
|
|
|
await ganacheServer.quit();
|
|
|
|
if (ganacheOptions?.concurrent) {
|
|
|
|
await secondaryGanacheServer.quit();
|
|
|
|
}
|
|
|
|
if (webDriver) {
|
|
|
|
await webDriver.quit();
|
|
|
|
}
|
|
|
|
if (dappServer) {
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
dappServer.close((error) => {
|
|
|
|
if (error) {
|
|
|
|
return reject(error);
|
|
|
|
}
|
|
|
|
return resolve();
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-06-16 17:12:20 +02:00
|
|
|
}
|
|
|
|
if (segmentServer) {
|
|
|
|
await segmentServer.stop();
|
|
|
|
}
|
2020-12-01 22:24:56 +01:00
|
|
|
}
|
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,
|
2021-08-11 00:09:42 +02:00
|
|
|
xLargeDelayMs,
|
|
|
|
xxLargeDelayMs,
|
2020-01-20 19:50:25 +01:00
|
|
|
withFixtures,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|