2021-02-04 19:15:23 +01:00
|
|
|
const path = require('path');
|
2022-01-19 00:08:41 +01:00
|
|
|
const BigNumber = require('bignumber.js');
|
2022-02-16 15:21:41 +01:00
|
|
|
const mockttp = require('mockttp');
|
2021-02-04 19:15:23 +01:00
|
|
|
const createStaticServer = require('../../development/create-static-server');
|
2022-02-23 18:27:13 +01:00
|
|
|
const enLocaleMessages = require('../../app/_locales/en/messages.json');
|
2022-03-15 17:17:48 +01:00
|
|
|
const { setupMocking } = require('./mock-e2e');
|
2021-02-04 19:15:23 +01:00
|
|
|
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;
|
2022-03-09 15:38:12 +01:00
|
|
|
const veryLargeDelayMs = largeDelayMs * 2;
|
2022-03-29 17:47:45 +02:00
|
|
|
const dappBasePort = 8080;
|
2020-07-10 05:57:54 +02:00
|
|
|
|
2022-01-19 00:08:41 +01:00
|
|
|
const convertToHexValue = (val) => `0x${new BigNumber(val, 10).toString(16)}`;
|
|
|
|
|
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,
|
2022-03-29 17:47:45 +02:00
|
|
|
dappOptions,
|
2020-12-01 22:24:56 +01:00
|
|
|
title,
|
2021-04-15 15:58:51 +02:00
|
|
|
failOnConsoleError = true,
|
2021-04-26 17:02:29 +02:00
|
|
|
dappPath = undefined,
|
2022-02-22 17:48:12 +01:00
|
|
|
testSpecificMock = function () {
|
|
|
|
// do nothing.
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
} = options;
|
|
|
|
const fixtureServer = new FixtureServer();
|
|
|
|
const ganacheServer = new Ganache();
|
2022-03-07 20:23:04 +01:00
|
|
|
const https = await mockttp.generateCACertificate();
|
2022-03-15 17:17:48 +01:00
|
|
|
const mockServer = mockttp.getLocal({ https, cors: true });
|
2021-06-04 15:52:07 +02:00
|
|
|
let secondaryGanacheServer;
|
2022-03-29 17:47:45 +02:00
|
|
|
let numberOfDapps = dapp ? 1 : 0;
|
|
|
|
const dappServer = [];
|
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,
|
2022-01-19 00:08:41 +01:00
|
|
|
chain: { chainId },
|
2021-06-04 15:52:07 +02:00
|
|
|
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) {
|
2022-03-29 17:47:45 +02:00
|
|
|
if (dappOptions?.numberOfDapps) {
|
|
|
|
numberOfDapps = dappOptions.numberOfDapps;
|
|
|
|
}
|
|
|
|
for (let i = 0; i < numberOfDapps; i++) {
|
|
|
|
let dappDirectory;
|
|
|
|
if (dappPath) {
|
|
|
|
dappDirectory = path.resolve(__dirname, dappPath);
|
|
|
|
} else {
|
|
|
|
dappDirectory = path.resolve(
|
|
|
|
__dirname,
|
|
|
|
'..',
|
|
|
|
'..',
|
|
|
|
'node_modules',
|
|
|
|
'@metamask',
|
|
|
|
'test-dapp',
|
|
|
|
'dist',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
dappServer.push(createStaticServer(dappDirectory));
|
|
|
|
dappServer[i].listen(`${dappBasePort + i}`);
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
dappServer[i].on('listening', resolve);
|
|
|
|
dappServer[i].on('error', reject);
|
|
|
|
});
|
2021-04-26 17:02:29 +02:00
|
|
|
}
|
2020-07-10 05:57:54 +02:00
|
|
|
}
|
2022-03-07 20:23:04 +01:00
|
|
|
await setupMocking(mockServer, testSpecificMock);
|
2022-02-16 15:21:41 +01:00
|
|
|
await mockServer.start(8000);
|
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,
|
2022-02-16 15:21:41 +01:00
|
|
|
mockServer,
|
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();
|
|
|
|
}
|
2022-03-29 17:47:45 +02:00
|
|
|
if (dapp) {
|
|
|
|
for (let i = 0; i < numberOfDapps; i++) {
|
|
|
|
if (dappServer[i] && dappServer[i].listening) {
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
dappServer[i].close((error) => {
|
|
|
|
if (error) {
|
|
|
|
return reject(error);
|
|
|
|
}
|
|
|
|
return resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2021-06-16 17:12:20 +02:00
|
|
|
}
|
2022-03-07 20:23:04 +01:00
|
|
|
await mockServer.stop();
|
2020-12-01 22:24:56 +01:00
|
|
|
}
|
2020-01-20 19:50:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 19:34:43 +01:00
|
|
|
/**
|
|
|
|
* @param {*} driver - selinium driver
|
|
|
|
* @param {*} handlesCount - total count of windows that should be loaded
|
|
|
|
* @returns handles - an object with window handles, properties in object represent windows:
|
|
|
|
* 1. extension: metamask extension window
|
|
|
|
* 2. dapp: test-app window
|
|
|
|
* 3. popup: metsmask extension popup window
|
|
|
|
*/
|
|
|
|
const getWindowHandles = async (driver, handlesCount) => {
|
|
|
|
await driver.waitUntilXWindowHandles(handlesCount);
|
|
|
|
const windowHandles = await driver.getAllWindowHandles();
|
|
|
|
|
|
|
|
const extension = windowHandles[0];
|
|
|
|
const dapp = await driver.switchToWindowWithTitle(
|
|
|
|
'E2E Test Dapp',
|
|
|
|
windowHandles,
|
|
|
|
);
|
|
|
|
const popup = windowHandles.find(
|
|
|
|
(handle) => handle !== extension && handle !== dapp,
|
|
|
|
);
|
|
|
|
return { extension, dapp, popup };
|
|
|
|
};
|
|
|
|
|
|
|
|
const connectDappWithExtensionPopup = async (driver) => {
|
2022-03-29 17:47:45 +02:00
|
|
|
await driver.openNewPage(`http://127.0.0.1:${dappBasePort}/`);
|
2022-01-19 19:34:43 +01:00
|
|
|
await driver.delay(regularDelayMs);
|
|
|
|
await driver.clickElement({ text: 'Connect', tag: 'button' });
|
|
|
|
await driver.delay(regularDelayMs);
|
|
|
|
|
|
|
|
const windowHandles = await getWindowHandles(driver, 3);
|
|
|
|
|
|
|
|
// open extension popup and confirm connect
|
|
|
|
await driver.switchToWindow(windowHandles.popup);
|
|
|
|
await driver.delay(largeDelayMs);
|
|
|
|
await driver.clickElement({ text: 'Next', tag: 'button' });
|
|
|
|
await driver.clickElement({ text: 'Connect', tag: 'button' });
|
|
|
|
|
|
|
|
// send from dapp
|
|
|
|
await driver.waitUntilXWindowHandles(2);
|
|
|
|
await driver.switchToWindow(windowHandles.dapp);
|
|
|
|
await driver.delay(regularDelayMs);
|
|
|
|
};
|
|
|
|
|
2022-02-23 18:27:13 +01:00
|
|
|
const completeImportSRPOnboardingFlow = async (
|
|
|
|
driver,
|
|
|
|
seedPhrase,
|
|
|
|
password,
|
|
|
|
) => {
|
|
|
|
if (process.env.ONBOARDING_V2 === '1') {
|
|
|
|
// welcome
|
|
|
|
await driver.clickElement('[data-testid="onboarding-import-wallet"]');
|
|
|
|
|
|
|
|
// metrics
|
|
|
|
await driver.clickElement('[data-testid="metametrics-no-thanks"]');
|
|
|
|
|
|
|
|
// import with recovery phrase
|
|
|
|
await driver.fill('[data-testid="import-srp-text"]', seedPhrase);
|
|
|
|
await driver.clickElement('[data-testid="import-srp-confirm"]');
|
|
|
|
|
|
|
|
// create password
|
|
|
|
await driver.fill('[data-testid="create-password-new"]', password);
|
|
|
|
await driver.fill('[data-testid="create-password-confirm"]', password);
|
|
|
|
await driver.clickElement('[data-testid="create-password-terms"]');
|
|
|
|
await driver.clickElement('[data-testid="create-password-import"]');
|
|
|
|
|
|
|
|
// complete
|
|
|
|
await driver.clickElement('[data-testid="onboarding-complete-done"]');
|
|
|
|
|
|
|
|
// pin extension
|
|
|
|
await driver.clickElement('[data-testid="pin-extension-next"]');
|
|
|
|
await driver.clickElement('[data-testid="pin-extension-done"]');
|
|
|
|
} else {
|
|
|
|
// clicks the continue button on the welcome screen
|
|
|
|
await driver.findElement('.welcome-page__header');
|
|
|
|
await driver.clickElement({
|
|
|
|
text: enLocaleMessages.getStarted.message,
|
|
|
|
tag: 'button',
|
|
|
|
});
|
|
|
|
|
|
|
|
// clicks the "Import Wallet" option
|
|
|
|
await driver.clickElement({ text: 'Import wallet', tag: 'button' });
|
|
|
|
|
|
|
|
// clicks the "No thanks" option on the metametrics opt-in screen
|
|
|
|
await driver.clickElement('.btn-secondary');
|
|
|
|
|
|
|
|
// Import Secret Recovery Phrase
|
2022-03-16 22:43:21 +01:00
|
|
|
await driver.pasteIntoField(
|
2022-03-21 20:09:26 +01:00
|
|
|
'[data-testid="import-srp__srp-word-0"]',
|
2022-02-23 18:27:13 +01:00
|
|
|
seedPhrase,
|
|
|
|
);
|
|
|
|
|
|
|
|
await driver.fill('#password', password);
|
|
|
|
await driver.fill('#confirm-password', password);
|
|
|
|
|
|
|
|
await driver.clickElement(
|
|
|
|
'[data-testid="create-new-vault__terms-checkbox"]',
|
|
|
|
);
|
|
|
|
|
|
|
|
await driver.clickElement({ text: 'Import', tag: 'button' });
|
|
|
|
|
|
|
|
// clicks through the success screen
|
|
|
|
await driver.findElement({ text: 'Congratulations', tag: 'div' });
|
|
|
|
await driver.clickElement({
|
|
|
|
text: enLocaleMessages.endOfFlowMessage10.message,
|
|
|
|
tag: 'button',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-25 03:17:26 +02:00
|
|
|
module.exports = {
|
2022-01-19 19:34:43 +01:00
|
|
|
getWindowHandles,
|
2022-01-19 00:08:41 +01:00
|
|
|
convertToHexValue,
|
2019-09-07 09:13:58 +02:00
|
|
|
tinyDelayMs,
|
|
|
|
regularDelayMs,
|
|
|
|
largeDelayMs,
|
2022-03-09 15:38:12 +01:00
|
|
|
veryLargeDelayMs,
|
2020-01-20 19:50:25 +01:00
|
|
|
withFixtures,
|
2022-01-19 19:34:43 +01:00
|
|
|
connectDappWithExtensionPopup,
|
2022-02-23 18:27:13 +01:00
|
|
|
completeImportSRPOnboardingFlow,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|