2021-02-04 19:15:23 +01:00
|
|
|
const fs = require('fs');
|
|
|
|
const os = require('os');
|
|
|
|
const path = require('path');
|
|
|
|
const { Builder, By, until } = require('selenium-webdriver');
|
|
|
|
const firefox = require('selenium-webdriver/firefox');
|
2022-02-16 15:21:41 +01:00
|
|
|
const proxy = require('selenium-webdriver/proxy');
|
2019-12-11 20:14:58 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The prefix for temporary Firefox profiles. All Firefox profiles used for e2e tests
|
|
|
|
* will be created as random directories inside this.
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2019-12-11 20:14:58 +01:00
|
|
|
* @type {string}
|
|
|
|
*/
|
2021-02-04 19:15:23 +01:00
|
|
|
const TEMP_PROFILE_PATH_PREFIX = path.join(os.tmpdir(), 'MetaMask-Fx-Profile');
|
2019-12-11 20:14:58 +01:00
|
|
|
|
2022-02-16 15:21:41 +01:00
|
|
|
/**
|
|
|
|
* Proxy host to use for HTTPS requests
|
|
|
|
*
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
const HTTPS_PROXY_HOST = '127.0.0.1:8000';
|
|
|
|
|
2019-12-11 20:14:58 +01:00
|
|
|
/**
|
|
|
|
* A wrapper around a {@code WebDriver} instance exposing Firefox-specific functionality
|
|
|
|
*/
|
|
|
|
class FirefoxDriver {
|
|
|
|
/**
|
|
|
|
* Builds a {@link FirefoxDriver} instance
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2022-07-27 15:28:05 +02:00
|
|
|
* @param {object} options - the options for the build
|
2022-01-07 16:57:33 +01:00
|
|
|
* @param options.responsive
|
|
|
|
* @param options.port
|
2020-01-13 19:36:36 +01:00
|
|
|
* @returns {Promise<{driver: !ThenableWebDriver, extensionUrl: string, extensionId: string}>}
|
2019-12-11 20:14:58 +01:00
|
|
|
*/
|
2022-07-18 10:22:23 +02:00
|
|
|
static async build({ responsive, port }) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const templateProfile = fs.mkdtempSync(TEMP_PROFILE_PATH_PREFIX);
|
|
|
|
const options = new firefox.Options().setProfile(templateProfile);
|
2022-02-16 15:21:41 +01:00
|
|
|
options.setProxy(proxy.manual({ https: HTTPS_PROXY_HOST }));
|
|
|
|
options.setAcceptInsecureCerts(true);
|
2022-07-01 21:46:12 +02:00
|
|
|
options.setPreference('browser.download.folderList', 2);
|
|
|
|
options.setPreference(
|
|
|
|
'browser.download.dir',
|
|
|
|
`${process.cwd()}/test-artifacts/downloads`,
|
|
|
|
);
|
2023-05-02 17:10:53 +02:00
|
|
|
if (process.env.CI === 'true') {
|
|
|
|
options.setBinary('/opt/firefox/firefox');
|
|
|
|
}
|
2020-01-07 15:01:06 +01:00
|
|
|
const builder = new Builder()
|
2019-12-11 20:14:58 +01:00
|
|
|
.forBrowser('firefox')
|
2021-02-04 19:15:23 +01:00
|
|
|
.setFirefoxOptions(options);
|
2020-01-07 15:01:06 +01:00
|
|
|
if (port) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const service = new firefox.ServiceBuilder().setPort(port);
|
|
|
|
builder.setFirefoxService(service);
|
2020-01-07 15:01:06 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
const driver = builder.build();
|
|
|
|
const fxDriver = new FirefoxDriver(driver);
|
2019-12-11 20:14:58 +01:00
|
|
|
|
2022-07-18 10:22:23 +02:00
|
|
|
const extensionId = await fxDriver.installExtension('dist/firefox');
|
2021-02-04 19:15:23 +01:00
|
|
|
const internalExtensionId = await fxDriver.getInternalId();
|
2019-12-11 20:14:58 +01:00
|
|
|
|
|
|
|
if (responsive) {
|
2021-02-04 19:15:23 +01:00
|
|
|
await driver.manage().window().setRect({ width: 320, height: 600 });
|
2019-12-11 20:14:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
driver,
|
|
|
|
extensionId,
|
2020-01-20 18:03:07 +01:00
|
|
|
extensionUrl: `moz-extension://${internalExtensionId}`,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-12-11 20:14:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {!ThenableWebDriver} driver - a {@code WebDriver} instance
|
2019-12-11 20:14:58 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
constructor(driver) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this._driver = driver;
|
2019-12-11 20:14:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Installs the extension at the given path
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {string} addonPath - the path to the unpacked extension or XPI
|
2020-11-10 18:30:41 +01:00
|
|
|
* @returns {Promise<string>} the extension ID
|
2019-12-11 20:14:58 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
async installExtension(addonPath) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return await this._driver.installAddon(addonPath, true);
|
2019-12-11 20:14:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the Internal UUID for the given extension
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2020-11-10 18:30:41 +01:00
|
|
|
* @returns {Promise<string>} the Internal UUID for the given extension
|
2019-12-11 20:14:58 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
async getInternalId() {
|
2021-02-04 19:15:23 +01:00
|
|
|
await this._driver.get('about:debugging#addons');
|
2020-11-03 00:41:28 +01:00
|
|
|
return await this._driver
|
|
|
|
.wait(
|
2021-06-16 14:24:36 +02:00
|
|
|
until.elementLocated(By.xpath("//dl/div[contains(., 'UUID')]/dd")),
|
2020-11-03 00:41:28 +01:00
|
|
|
1000,
|
|
|
|
)
|
2021-02-04 19:15:23 +01:00
|
|
|
.getText();
|
2019-12-11 20:14:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
module.exports = FirefoxDriver;
|