Skip to content

Commit

Permalink
Support GeckoDriver addon install/uninstall commands
Browse files Browse the repository at this point in the history
Adds support for the following commands introduced in GeckoDriver 0.17:

  * POST /session/{session id}/moz/addon/install
  * POST /session/{session id}/moz/addon/uninstall

For Ruby, implemented as a role-based interface (though I'm not sure
it was really needed in this case since it's unlikely that any other
driver will ever support these commands).

For Python, implemented directly in Firefox webdriver.

Tests are missing because all the extension
files available for Selenium build (legacy Firefox driver extension,
Firebug in third_party) fail to install on latest versions of Firefox.

Related to SeleniumHQ#4215.
  • Loading branch information
p0deje committed Jun 24, 2017
1 parent 78d7a3b commit 26fa936
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 0 deletions.
4 changes: 4 additions & 0 deletions py/selenium/webdriver/firefox/remote_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ def __init__(self, remote_server_addr, keep_alive=True):
("POST", "/session/$sessionId/moz/xbl/$id/anonymous_children")
self._commands["ELEMENT_FIND_ANONYMOUS_ELEMENTS_BY_ATTRIBUTE"] = \
("POST", "/session/$sessionId/moz/xbl/$id/anonymous_by_attribute")
self._commands["INSTALL_ADDON"] = \
("POST", "/session/$sessionId/moz/addon/install")
self._commands["UNINSTALL_ADDON"] = \
("POST", "/session/$sessionId/moz/addon/uninstall")
24 changes: 24 additions & 0 deletions py/selenium/webdriver/firefox/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,27 @@ def context(self, context):
yield
finally:
self.set_context(initial_context)

def install_addon(self, path, temporary=None):
"""
Installs Firefox addon.
Returns identifier of installed addon. This identifier can later
be used to uninstall addon.
:Usage:
driver.install_addon('firebug.xpi')
"""
payload = {"path": path}
if temporary is not None:
payload["temporary"] = temporary
return self.execute("INSTALL_ADDON", payload)["value"]

def uninstall_addon(self, identifier):
"""
Uninstalls Firefox addon using its identifier.
:Usage:
driver.uninstall_addon('addon@foo.com')
"""
self.execute("UNINSTALL_ADDON", {"id": identifier})
1 change: 1 addition & 0 deletions rb/lib/selenium/webdriver/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
require 'selenium/webdriver/common/driver_extensions/has_remote_status'
require 'selenium/webdriver/common/driver_extensions/has_network_connection'
require 'selenium/webdriver/common/driver_extensions/uploads_files'
require 'selenium/webdriver/common/driver_extensions/has_addons'
require 'selenium/webdriver/common/interactions/interactions'
require 'selenium/webdriver/common/interactions/input_device'
require 'selenium/webdriver/common/interactions/interaction'
Expand Down
50 changes: 50 additions & 0 deletions rb/lib/selenium/webdriver/common/driver_extensions/has_addons.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# encoding: utf-8
#
# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

module Selenium
module WebDriver
module DriverExtensions
module HasAddons

#
# Installs addon.
#
# @param [String] path Full path to addon file
# @param [Boolean] temporary
# @return [String] identifier of installed addon
#

def install_addon(path, temporary = nil)
@bridge.install_addon(path, temporary)
end

#
# Uninstalls addon.
#
# @param [String] id Identifier of installed addon
#

def uninstall_addon(id)
@bridge.uninstall_addon(id)
end

end # HasAddons
end # DriverExtensions
end # WebDriver
end # Selenium
1 change: 1 addition & 0 deletions rb/lib/selenium/webdriver/firefox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
require 'selenium/webdriver/firefox/launcher'
require 'selenium/webdriver/firefox/legacy/driver'

require 'selenium/webdriver/firefox/marionette/bridge'
require 'selenium/webdriver/firefox/marionette/driver'
require 'selenium/webdriver/firefox/options'
require 'selenium/webdriver/firefox/service'
Expand Down
49 changes: 49 additions & 0 deletions rb/lib/selenium/webdriver/firefox/marionette/bridge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# encoding: utf-8
#
# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

module Selenium
module WebDriver
module Firefox
module Marionette
module Bridge

COMMANDS = {
install_addon: [:post, 'session/:session_id/moz/addon/install'.freeze],
uninstall_addon: [:post, 'session/:session_id/moz/addon/uninstall'.freeze]
}.freeze

def commands(command)
COMMANDS[command] || super
end

def install_addon(path, temporary)
payload = {path: path}
payload.merge!(temporary: temporary) unless temporary.nil?
execute :install_addon, {}, payload
end

def uninstall_addon(id)
execute :uninstall_addon, {}, {id: id}
end

end # Bridge
end # Marionette
end # Firefox
end # WebDriver
end # Selenium
2 changes: 2 additions & 0 deletions rb/lib/selenium/webdriver/firefox/marionette/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module Marionette
#

class Driver < WebDriver::Driver
include DriverExtensions::HasAddons
include DriverExtensions::HasWebStorage
include DriverExtensions::TakesScreenshot

Expand Down Expand Up @@ -55,6 +56,7 @@ def initialize(opts = {})
bridge = Remote::Bridge.new(opts)
capabilities = bridge.create_session(desired_capabilities)
@bridge = Remote::W3C::Bridge.new(capabilities, bridge.session_id, opts)
@bridge.extend Marionette::Bridge

super(@bridge, listener: listener)
end
Expand Down

0 comments on commit 26fa936

Please sign in to comment.