Created
September 22, 2019 04:24
-
-
Save asartalo/047d38a2aa1fe13fbe4e9b88fed9b2ef to your computer and use it in GitHub Desktop.
Hacky Testing Firefox extension with Selenium Webdriver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { Builder, By, Key, until } = require('selenium-webdriver'); | |
const firefox = require('selenium-webdriver/firefox'); | |
const Command = require('selenium-webdriver/lib/command').Command; | |
const path = require('path'); | |
const options = new firefox.Options() | |
.setPreference('extensions.firebug.showChromeErrors', true); | |
(async function example() { | |
let driver = await new Builder() | |
.forBrowser('firefox') | |
.usingServer('http://localhost:4444/wd/hub') | |
.setFirefoxOptions(options) | |
.build(); | |
const addonId = await installWebExt(driver, '/path/to/extension.xpi'); | |
try { | |
const baseUrl = await getBaseUrl(driver, addonId); | |
const newTabUrl = baseUrl + 'index.html'; | |
await driver.get(newTabUrl); | |
// I can now interact with the newtab page at this point | |
await driver.sleep(5000); | |
} finally { | |
await driver.quit(); | |
} | |
})(); | |
async function installWebExt(driver, extension) { | |
let session = await driver.session_; | |
let cmd = new Command('moz-install-web-ext') | |
.setParameter('path', path.resolve(extension)) | |
.setParameter('sessionId', session.getId()) | |
.setParameter('temporary', true); | |
let executor = driver.getExecutor(); | |
executor.defineCommand(cmd.getName(), 'POST', '/session/:sessionId/moz/addon/install'); | |
return executor.execute(cmd); | |
} | |
async function getBaseUrl(driver, addonId) { | |
await driver.get('about:memory'); | |
const button = await driver.findElement(By.id('measureButton')); | |
button.click(); | |
await driver.sleep(3000); | |
const elements = await driver.findElements(By.css('[title="WebExtensions that are active in this session"]')) | |
const text = await Promise | |
.all(elements.map(element => element.getText())) | |
.then(texts => | |
texts.find(text => text.indexOf(addonId) > 0) | |
); | |
if (text) { | |
return text.match(/baseURL=([^\), ]+)/)[1]; | |
} | |
return null; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"manifest_version": 2, | |
"short_name": "Foo", | |
"name": "Foo", | |
"description": "Foo", | |
"version": "0.1.0", | |
"chrome_url_overrides" : { | |
"newtab": "index.html" | |
}, | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment