2021-02-04 19:15:23 +01:00
|
|
|
const { Builder } = require('selenium-webdriver');
|
|
|
|
const chrome = require('selenium-webdriver/chrome');
|
2022-02-16 15:21:41 +01:00
|
|
|
const proxy = require('selenium-webdriver/proxy');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 Chrome-specific functionality
|
|
|
|
*/
|
|
|
|
class ChromeDriver {
|
2020-12-11 17:24:17 +01:00
|
|
|
static async build({ responsive, port }) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const args = [`load-extension=dist/chrome`];
|
2019-12-11 20:14:58 +01:00
|
|
|
if (responsive) {
|
2021-02-04 19:15:23 +01:00
|
|
|
args.push('--auto-open-devtools-for-tabs');
|
2019-12-11 20:14:58 +01:00
|
|
|
}
|
2022-03-18 18:45:23 +01:00
|
|
|
args.push('--log-level=3');
|
2021-02-04 19:15:23 +01:00
|
|
|
const options = new chrome.Options().addArguments(args);
|
2022-02-16 15:21:41 +01:00
|
|
|
options.setProxy(proxy.manual({ https: HTTPS_PROXY_HOST }));
|
|
|
|
options.setAcceptInsecureCerts(true);
|
2021-02-04 19:15:23 +01:00
|
|
|
const builder = new Builder()
|
|
|
|
.forBrowser('chrome')
|
|
|
|
.setChromeOptions(options);
|
2021-06-15 19:48:21 +02:00
|
|
|
const service = new chrome.ServiceBuilder();
|
|
|
|
|
2021-09-28 00:39:31 +02:00
|
|
|
// Enables Chrome logging. Default: enabled
|
2021-06-15 19:48:21 +02:00
|
|
|
// Especially useful for discovering why Chrome has crashed, but can also
|
|
|
|
// be useful for revealing console errors (from the page or background).
|
2021-09-28 00:39:31 +02:00
|
|
|
if (process.env.ENABLE_CHROME_LOGGING !== 'false') {
|
2021-06-15 19:48:21 +02:00
|
|
|
service.setStdio('inherit').enableChromeLogging();
|
|
|
|
}
|
2020-01-07 15:01:06 +01:00
|
|
|
if (port) {
|
2021-06-15 19:48:21 +02:00
|
|
|
service.setPort(port);
|
2020-01-07 15:01:06 +01:00
|
|
|
}
|
2021-06-15 19:48:21 +02:00
|
|
|
builder.setChromeService(service);
|
2021-02-04 19:15:23 +01:00
|
|
|
const driver = builder.build();
|
|
|
|
const chromeDriver = new ChromeDriver(driver);
|
|
|
|
const extensionId = await chromeDriver.getExtensionIdByName('MetaMask');
|
2019-12-11 20:14:58 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
driver,
|
2020-01-20 18:03:07 +01:00
|
|
|
extensionUrl: `chrome-extension://${extensionId}`,
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the extension ID for the given extension name
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {string} extensionName - the extension name
|
2020-11-10 18:30:41 +01:00
|
|
|
* @returns {Promise<string|undefined>} the extension ID
|
2019-12-11 20:14:58 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
async getExtensionIdByName(extensionName) {
|
2021-02-04 19:15:23 +01:00
|
|
|
await this._driver.get('chrome://extensions');
|
2019-12-11 20:14:58 +01:00
|
|
|
return await this._driver.executeScript(`
|
|
|
|
const extensions = document.querySelector("extensions-manager").shadowRoot
|
|
|
|
.querySelector("extensions-item-list").shadowRoot
|
|
|
|
.querySelectorAll("extensions-item")
|
|
|
|
|
|
|
|
for (let i = 0; i < extensions.length; i++) {
|
|
|
|
const extension = extensions[i].shadowRoot
|
|
|
|
const name = extension.querySelector('#name').textContent
|
2022-03-10 23:01:55 +01:00
|
|
|
if (name.startsWith("${extensionName}")) {
|
2019-12-11 20:14:58 +01:00
|
|
|
return extensions[i].getAttribute("id")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined
|
2021-02-04 19:15:23 +01:00
|
|
|
`);
|
2019-12-11 20:14:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
module.exports = ChromeDriver;
|