2021-02-04 19:15:23 +01:00
|
|
|
const path = require('path');
|
2022-08-09 20:36:32 +02:00
|
|
|
const { promises: fs } = require('fs');
|
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-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');
|
2022-05-06 00:28:48 +02:00
|
|
|
const PhishingWarningPageServer = require('./phishing-warning-page-server');
|
2021-02-04 19:15:23 +01:00
|
|
|
const { buildWebDriver } = require('./webdriver');
|
2022-12-08 13:58:34 +01:00
|
|
|
const { PAGES } = require('./webdriver/driver');
|
2021-09-01 18:40:40 +02:00
|
|
|
const { ensureXServerIsRunning } = require('./x-server');
|
2022-07-21 19:08:01 +02:00
|
|
|
const GanacheSeeder = require('./seeder/ganache-seeder');
|
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-08-09 20:36:32 +02:00
|
|
|
const createDownloadFolder = async (downloadsFolder) => {
|
|
|
|
await fs.rm(downloadsFolder, { recursive: true, force: true });
|
|
|
|
await fs.mkdir(downloadsFolder, { recursive: true });
|
|
|
|
};
|
|
|
|
|
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,
|
2022-07-21 19:08:01 +02:00
|
|
|
smartContract,
|
2020-12-01 22:24:56 +01:00
|
|
|
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-05-06 00:28:48 +02:00
|
|
|
dappPaths,
|
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 = [];
|
2022-05-06 00:28:48 +02:00
|
|
|
const phishingPageServer = new PhishingWarningPageServer();
|
2020-01-20 19:50:25 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let webDriver;
|
2022-11-14 15:35:08 +01:00
|
|
|
let driver;
|
|
|
|
const errors = [];
|
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);
|
2022-07-21 19:08:01 +02:00
|
|
|
let contractRegistry;
|
|
|
|
|
|
|
|
if (smartContract) {
|
2022-08-09 15:59:20 +02:00
|
|
|
const ganacheSeeder = new GanacheSeeder(ganacheServer.getProvider());
|
2022-07-21 19:08:01 +02:00
|
|
|
await ganacheSeeder.deploySmartContract(smartContract);
|
|
|
|
contractRegistry = ganacheSeeder.getContractRegistry();
|
|
|
|
}
|
|
|
|
|
2021-06-04 15:52:07 +02:00
|
|
|
if (ganacheOptions?.concurrent) {
|
2023-01-31 13:58:39 +01:00
|
|
|
const { port, chainId, ganacheOptions2 } = ganacheOptions.concurrent;
|
2021-06-04 15:52:07 +02:00
|
|
|
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,
|
2023-01-31 13:58:39 +01:00
|
|
|
...ganacheOptions2,
|
2021-06-04 15:52:07 +02:00
|
|
|
});
|
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
await fixtureServer.start();
|
2022-10-28 10:42:12 +02:00
|
|
|
fixtureServer.loadJsonState(fixtures);
|
2022-05-06 00:28:48 +02:00
|
|
|
await phishingPageServer.start();
|
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;
|
2022-05-06 00:28:48 +02:00
|
|
|
if (dappPath || (dappPaths && dappPaths[i])) {
|
|
|
|
dappDirectory = path.resolve(__dirname, dappPath || dappPaths[i]);
|
2022-03-29 17:47:45 +02:00
|
|
|
} 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();
|
|
|
|
}
|
2022-11-14 15:35:08 +01:00
|
|
|
driver = (await buildWebDriver(driverOptions)).driver;
|
|
|
|
webDriver = driver.driver;
|
|
|
|
|
|
|
|
if (process.env.SELENIUM_BROWSER === 'chrome') {
|
|
|
|
await driver.checkBrowserForExceptions();
|
|
|
|
}
|
2020-01-20 19:50:25 +01:00
|
|
|
|
2022-12-13 20:30:47 +01:00
|
|
|
let driverProxy;
|
|
|
|
if (process.env.E2E_DEBUG === 'true') {
|
|
|
|
driverProxy = new Proxy(driver, {
|
|
|
|
get(target, prop, receiver) {
|
|
|
|
const originalProperty = target[prop];
|
|
|
|
if (typeof originalProperty === 'function') {
|
|
|
|
return (...args) => {
|
|
|
|
console.log(
|
|
|
|
`[driver] Called '${prop}' with arguments ${JSON.stringify(
|
|
|
|
args,
|
|
|
|
)}`,
|
|
|
|
);
|
|
|
|
return originalProperty.bind(target)(...args);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return Reflect.get(target, prop, receiver);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-21 20:24:51 +02:00
|
|
|
await testSuite({
|
2022-12-13 20:30:47 +01:00
|
|
|
driver: driverProxy ?? driver,
|
2022-02-16 15:21:41 +01:00
|
|
|
mockServer,
|
2022-07-21 19:08:01 +02:00
|
|
|
contractRegistry,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-07-09 17:01:39 +02:00
|
|
|
|
|
|
|
if (process.env.SELENIUM_BROWSER === 'chrome') {
|
2022-11-14 15:35:08 +01:00
|
|
|
errors.concat(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 {
|
2022-11-14 15:35:08 +01:00
|
|
|
await driver.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
|
|
|
}
|
2022-11-14 15:35:08 +01:00
|
|
|
if (
|
|
|
|
errors.length === 0 &&
|
|
|
|
driver.exceptions.length > 0 &&
|
|
|
|
failOnConsoleError
|
|
|
|
) {
|
2022-12-08 13:58:34 +01:00
|
|
|
/**
|
|
|
|
* Navigate to the background
|
|
|
|
* forcing background exceptions to be captured
|
|
|
|
* proving more helpful context
|
|
|
|
*/
|
|
|
|
await driver.navigate(PAGES.BACKGROUND);
|
|
|
|
const errorMessage = `Errors found in browser console including the background:\n${driver.exceptions.join(
|
2022-11-14 15:35:08 +01:00
|
|
|
'\n',
|
|
|
|
)}`;
|
|
|
|
throw Error(errorMessage);
|
|
|
|
}
|
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) {
|
2022-11-14 15:35:08 +01:00
|
|
|
await driver.quit();
|
2021-06-16 17:12:20 +02:00
|
|
|
}
|
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-05-06 00:28:48 +02:00
|
|
|
if (phishingPageServer.isRunning()) {
|
|
|
|
await phishingPageServer.quit();
|
|
|
|
}
|
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 };
|
|
|
|
};
|
|
|
|
|
2023-01-31 13:58:39 +01:00
|
|
|
const importSRPOnboardingFlow = async (driver, seedPhrase, password) => {
|
2023-01-13 17:25:01 +01:00
|
|
|
// welcome
|
|
|
|
await driver.clickElement('[data-testid="onboarding-import-wallet"]');
|
2022-02-23 18:27:13 +01:00
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// metrics
|
|
|
|
await driver.clickElement('[data-testid="metametrics-no-thanks"]');
|
2022-02-23 18:27:13 +01:00
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// import with recovery phrase
|
|
|
|
await driver.pasteIntoField(
|
|
|
|
'[data-testid="import-srp__srp-word-0"]',
|
|
|
|
seedPhrase,
|
|
|
|
);
|
|
|
|
await driver.clickElement('[data-testid="import-srp-confirm"]');
|
2022-02-23 18:27:13 +01:00
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// 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"]');
|
2023-01-31 13:58:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const completeImportSRPOnboardingFlow = async (
|
|
|
|
driver,
|
|
|
|
seedPhrase,
|
|
|
|
password,
|
|
|
|
) => {
|
|
|
|
await importSRPOnboardingFlow(driver, seedPhrase, password);
|
2022-02-23 18:27:13 +01:00
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// complete
|
|
|
|
await driver.clickElement('[data-testid="onboarding-complete-done"]');
|
2022-02-23 18:27:13 +01:00
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// pin extension
|
|
|
|
await driver.clickElement('[data-testid="pin-extension-next"]');
|
|
|
|
await driver.clickElement('[data-testid="pin-extension-done"]');
|
2022-02-23 18:27:13 +01:00
|
|
|
};
|
|
|
|
|
2022-05-09 14:46:20 +02:00
|
|
|
const completeImportSRPOnboardingFlowWordByWord = async (
|
|
|
|
driver,
|
|
|
|
seedPhrase,
|
|
|
|
password,
|
|
|
|
) => {
|
2023-01-13 17:25:01 +01:00
|
|
|
// welcome
|
|
|
|
await driver.clickElement('[data-testid="onboarding-import-wallet"]');
|
2022-05-09 14:46:20 +02:00
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// metrics
|
|
|
|
await driver.clickElement('[data-testid="metametrics-no-thanks"]');
|
2022-07-22 22:39:48 +02:00
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// import with recovery phrase, word by word
|
2022-05-09 14:46:20 +02:00
|
|
|
const words = seedPhrase.split(' ');
|
|
|
|
for (const word of words) {
|
|
|
|
await driver.pasteIntoField(
|
|
|
|
`[data-testid="import-srp__srp-word-${words.indexOf(word)}"]`,
|
|
|
|
word,
|
|
|
|
);
|
|
|
|
}
|
2023-01-13 17:25:01 +01:00
|
|
|
await driver.clickElement('[data-testid="import-srp-confirm"]');
|
2022-05-09 14:46:20 +02:00
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// 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"]');
|
2022-05-09 14:46:20 +02:00
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// complete
|
|
|
|
await driver.clickElement('[data-testid="onboarding-complete-done"]');
|
2022-05-09 14:46:20 +02:00
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// pin extension
|
|
|
|
await driver.clickElement('[data-testid="pin-extension-next"]');
|
|
|
|
await driver.clickElement('[data-testid="pin-extension-done"]');
|
2022-05-09 14:46:20 +02:00
|
|
|
};
|
|
|
|
|
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,
|
2023-01-31 13:58:39 +01:00
|
|
|
importSRPOnboardingFlow,
|
2022-02-23 18:27:13 +01:00
|
|
|
completeImportSRPOnboardingFlow,
|
2022-05-09 14:46:20 +02:00
|
|
|
completeImportSRPOnboardingFlowWordByWord,
|
2022-08-09 20:36:32 +02:00
|
|
|
createDownloadFolder,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|