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');
|
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;
|
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-02-04 19:15:23 +01:00
|
|
|
} = options;
|
|
|
|
const fixtureServer = new FixtureServer();
|
|
|
|
const ganacheServer = new Ganache();
|
|
|
|
let dappServer;
|
|
|
|
let segmentServer;
|
|
|
|
let segmentStub;
|
2020-01-20 19:50:25 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let webDriver;
|
2020-01-20 19:50:25 +01:00
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
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) {
|
2020-11-03 00:41:28 +01:00
|
|
|
const 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-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
|
|
|
)}`;
|
|
|
|
throw new Error(errorMessage);
|
2020-07-09 17:01:39 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-09 17:42:56 +02:00
|
|
|
} catch (error) {
|
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-02-04 19:15:23 +01:00
|
|
|
await fixtureServer.stop();
|
|
|
|
await ganacheServer.quit();
|
2020-01-20 19:50:25 +01:00
|
|
|
if (webDriver) {
|
2021-02-04 19:15:23 +01:00
|
|
|
await webDriver.quit();
|
2020-01-20 19:50:25 +01:00
|
|
|
}
|
2020-07-10 05:57:54 +02:00
|
|
|
if (dappServer) {
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
dappServer.close((error) => {
|
|
|
|
if (error) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return reject(error);
|
2020-07-10 05:57:54 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return resolve();
|
|
|
|
});
|
|
|
|
});
|
2020-07-10 05:57:54 +02:00
|
|
|
}
|
2020-12-01 22:24:56 +01:00
|
|
|
if (segmentServer) {
|
2021-02-04 19:15:23 +01:00
|
|
|
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,
|
2020-01-20 19:50:25 +01:00
|
|
|
withFixtures,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|