Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node: Add support for installing unpacked addons at runtime in Firefox webdriver #10216

Merged
merged 2 commits into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions common/extensions/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
filegroup(
name = "extensions",
srcs = glob([
"*",
"**/*",
]),
visibility = [
"//javascript/node/selenium-webdriver:__pkg__",
],
)
6 changes: 6 additions & 0 deletions common/extensions/webextensions-selenium-example/inject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
((function(document) {
var div = document.createElement('div');
div.id = 'webextensions-selenium-example'
div.textContent = "Content injected by webextensions-selenium-example";
document.body.appendChild(div);
})(document))
17 changes: 17 additions & 0 deletions common/extensions/webextensions-selenium-example/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"manifest_version": 2,
"name": "webextensions-selenium-example",
"description": "Inject a div with id webextensions-selenium-example to verify that WebExtensions work in Firefox/Selenium" ,
"version": "0.1",
"content_scripts": [
{
"matches": ["https://*/*","http://*/*"],
"js": ["inject.js"]
}
],
"applications": {
"gecko": {
"id": "webextensions-selenium-example@example.com"
}
}
}
1 change: 1 addition & 0 deletions javascript/node/selenium-webdriver/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ jasmine_node_test(
data = TEST_DATA + [
"tools/init_jasmine.js",
"//common/src/web",
"//common/extensions",
"@npm//express",
"@npm//multer",
"@npm//serve-index",
Expand Down
11 changes: 10 additions & 1 deletion javascript/node/selenium-webdriver/firefox.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@

'use strict'

const fs = require('fs')
const path = require('path')
const Symbols = require('./lib/symbols')
const command = require('./lib/command')
Expand Down Expand Up @@ -670,7 +671,15 @@ class Driver extends webdriver.WebDriver {
* @see #uninstallAddon
*/
async installAddon(path, temporary = false) {
let buf = await io.read(path)
let stats = fs.statSync(path)
let buf;
if (stats.isDirectory()) {
let zip = new Zip()
await zip.addDir(path)
buf = await zip.toBuffer('DEFLATE')
} else {
buf = await io.read(path)
}
return this.execute(
new command.Command(ExtensionCommand.INSTALL_ADDON)
.setParameter('addon', buf.toString('base64'))
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,18 @@ test.suite(
const dir = await io.tmpDir()
await driver.setDownloadPath(dir)

const url = fileServer.whereIs('/data/firefox/webextension.xpi')
const url = fileServer.whereIs('/data/chrome/download.bin')
await driver.get(`data:text/html,<!DOCTYPE html>
<div><a download="" href="${url}">Go!</a></div>`)

await driver.findElement({ css: 'a' }).click()

const downloadPath = path.join(dir, 'webextension.xpi')
const downloadPath = path.join(dir, 'download.bin')
await driver.wait(() => io.exists(downloadPath), 5000)

const goldenPath = path.join(
__dirname,
'../../lib/test/data/firefox/webextension.xpi'
'../../lib/test/data/chrome/download.bin'
)
assert.strictEqual(
fs.readFileSync(downloadPath, 'binary'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ const path = require('path')
const chrome = require('../../chrome')
const symbols = require('../../lib/symbols')
const test = require('../../lib/test')
const { locate } = require('../../lib/test/resources')

const WEBEXTENSION_CRX = path.join(
__dirname,
'../../lib/test/data/chrome/webextension.crx'
)
const WEBEXTENSION_CRX = locate(
'common/extensions/webextensions-selenium-example.crx')

describe('chrome.Options', function () {
describe('addArguments', function () {
Expand Down
7 changes: 3 additions & 4 deletions javascript/node/selenium-webdriver/test/edge/options_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ const path = require('path')
const edge = require('../../edge')
const symbols = require('../../lib/symbols')
const test = require('../../lib/test')
const { locate } = require('../../lib/test/resources')

const WEBEXTENSION_CRX = path.join(
__dirname,
'../../lib/test/data/chrome/webextension.crx'
)
const WEBEXTENSION_CRX = locate(
'common/extensions/webextensions-selenium-example.crx')

describe('edge.Options', function () {
describe('addArguments', function () {
Expand Down
56 changes: 37 additions & 19 deletions javascript/node/selenium-webdriver/test/firefox_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ const io = require('../io')
const { Browser } = require('../')
const { Context } = require('../firefox')
const { Pages, suite } = require('../lib/test')
const { locate } = require('../lib/test/resources')

const WEBEXTENSION_EXTENSION_XPI = path.join(
__dirname,
'../lib/test/data/firefox/webextension.xpi'
)
const WEBEXTENSION_EXTENSION_ZIP = path.join(
__dirname,
'../lib/test/data/firefox/webextension.zip'
)
const WEBEXTENSION_EXTENSION_XPI = locate(
'common/extensions/webextensions-selenium-example.xpi')
const WEBEXTENSION_EXTENSION_ZIP = locate(
'common/extensions/webextensions-selenium-example.zip')
const WEBEXTENSION_EXTENSION_DIR = locate(
'common/extensions/webextensions-selenium-example')

const WEBEXTENSION_EXTENSION_ID =
'webextensions-selenium-example@example.com.xpi'
Expand Down Expand Up @@ -220,21 +219,40 @@ suite(
})
})

it('addons can be installed and uninstalled at runtime', async function () {
driver = env.builder().build()
describe('installAddon', function() {
beforeEach(function () {
driver = env.builder().build()
})

it('addons can be installed and uninstalled at runtime', async function () {

await driver.get(Pages.echoPage)
await verifyWebExtensionNotInstalled()

let id = await driver.installAddon(WEBEXTENSION_EXTENSION_XPI)
await driver.sleep(1000) // Give extension time to install (yuck).

await driver.get(Pages.echoPage)
await verifyWebExtensionNotInstalled()
await driver.get(Pages.echoPage)
await verifyWebExtensionWasInstalled()

let id = await driver.installAddon(WEBEXTENSION_EXTENSION_XPI)
await driver.sleep(1000) // Give extension time to install (yuck).
await driver.uninstallAddon(id)
await driver.get(Pages.echoPage)
await verifyWebExtensionNotInstalled()
})

await driver.get(Pages.echoPage)
await verifyWebExtensionWasInstalled()
it('unpacked addons can be installed and uninstalled at runtime', async function () {
await driver.get(Pages.echoPage)
await verifyWebExtensionNotInstalled()

await driver.uninstallAddon(id)
await driver.get(Pages.echoPage)
await verifyWebExtensionNotInstalled()
let id = await driver.installAddon(WEBEXTENSION_EXTENSION_DIR, true)

await driver.get(Pages.echoPage)
await verifyWebExtensionWasInstalled()

await driver.uninstallAddon(id)
await driver.get(Pages.echoPage)
await verifyWebExtensionNotInstalled()
})
})

async function verifyUserAgentWasChanged() {
Expand Down
7 changes: 3 additions & 4 deletions javascript/node/selenium-webdriver/test/io/zip_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ const path = require('path')
const io = require('../../io')
const zip = require('../../io/zip')
const { InvalidArgumentError } = require('../../lib/error')
const { locate } = require('../../lib/test/resources')

const XPI_PATH = path.join(
__dirname,
'../../lib/test/data/firefox/webextension.xpi'
)
const XPI_PATH = locate(
'common/extensions/webextensions-selenium-example.xpi')

describe('io/zip', function () {
describe('unzip', function () {
Expand Down