1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-26 12:29:06 +01:00
metamask-extension/test/e2e/helpers.js

120 lines
3.0 KiB
JavaScript
Raw Normal View History

const path = require('path');
const sinon = require('sinon');
const createStaticServer = require('../../development/create-static-server');
const {
createSegmentServer,
} = require('../../development/lib/create-segment-server');
const Ganache = require('./ganache');
const FixtureServer = require('./fixture-server');
const { buildWebDriver } = require('./webdriver');
const tinyDelayMs = 200;
const regularDelayMs = tinyDelayMs * 2;
const largeDelayMs = regularDelayMs * 2;
2018-05-25 03:17:26 +02:00
const dappPort = 8080;
2020-11-03 00:41:28 +01:00
async function withFixtures(options, testSuite) {
const {
dapp,
fixtures,
ganacheOptions,
driverOptions,
mockSegment,
title,
} = options;
const fixtureServer = new FixtureServer();
const ganacheServer = new Ganache();
let dappServer;
let segmentServer;
let segmentStub;
let webDriver;
try {
await ganacheServer.start(ganacheOptions);
await fixtureServer.start();
await fixtureServer.loadState(path.join(__dirname, 'fixtures', fixtures));
if (dapp) {
2020-11-03 00:41:28 +01:00
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);
});
}
if (mockSegment) {
segmentStub = sinon.stub();
segmentServer = createSegmentServer((_request, response, events) => {
for (const event of events) {
segmentStub(event);
}
response.statusCode = 200;
response.end();
});
await segmentServer.start(9090);
}
const { driver } = await buildWebDriver(driverOptions);
webDriver = driver;
await testSuite({
driver,
segmentStub,
});
if (process.env.SELENIUM_BROWSER === 'chrome') {
const errors = await driver.checkBrowserForConsoleErrors(driver);
if (errors.length) {
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',
)}`;
throw new Error(errorMessage);
}
}
} catch (error) {
if (webDriver) {
try {
await webDriver.verboseReportOnFailure(title);
} catch (verboseReportError) {
console.error(verboseReportError);
}
}
throw error;
} finally {
await fixtureServer.stop();
await ganacheServer.quit();
if (webDriver) {
await webDriver.quit();
}
if (dappServer) {
await new Promise((resolve, reject) => {
dappServer.close((error) => {
if (error) {
return reject(error);
}
return resolve();
});
});
}
if (segmentServer) {
await segmentServer.stop();
}
}
}
2018-05-25 03:17:26 +02:00
module.exports = {
tinyDelayMs,
regularDelayMs,
largeDelayMs,
withFixtures,
};