Skip to content

Commit

Permalink
[py] Allow Firefox preferences to be set directly in Options
Browse files Browse the repository at this point in the history
  • Loading branch information
davehunt committed Jan 13, 2017
1 parent dcc80b8 commit 0e5b18a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
12 changes: 12 additions & 0 deletions py/selenium/webdriver/firefox/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Options(object):

def __init__(self):
self._binary = None
self._preferences = {}
self._profile = None
self._arguments = []
self.log = Log()
Expand Down Expand Up @@ -61,6 +62,15 @@ def binary_location(self):
def binary_location(self, value):
self.binary = value

@property
def preferences(self):
"""Returns a dict of preferences."""
return self._preferences

def set_preference(self, name, value):
"""Sets a preference."""
self._preferences[name] = value

@property
def profile(self):
"""Returns the Firefox profile to use."""
Expand Down Expand Up @@ -100,6 +110,8 @@ def to_capabilities(self):

if self._binary is not None:
opts["binary"] = self._binary._start_cmd
if len(self._preferences) > 0:
opts["prefs"] = self._preferences
if self._profile is not None:
opts["profile"] = self._profile.encoded
if len(self._arguments) > 0:
Expand Down
21 changes: 21 additions & 0 deletions py/test/selenium/webdriver/marionette/mn_options_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class TestUnit(object):
def test_ctor(self):
opts = Options()
assert opts._binary is None
assert opts._preferences == {}
assert opts._profile is None
assert opts._arguments == []
assert isinstance(opts.log, Log)
Expand All @@ -61,6 +62,19 @@ def test_binary(self):
assert isinstance(opts.binary, FirefoxBinary)
assert opts.binary._start_cmd == path

def test_prefs(self):
opts = Options()
assert len(opts.preferences) == 0
assert isinstance(opts.preferences, dict)

opts.set_preference("spam", "ham")
assert len(opts.preferences) == 1
opts.set_preference("eggs", True)
assert len(opts.preferences) == 2
opts.set_preference("spam", "spam")
assert len(opts.preferences) == 2
assert opts.preferences == {"spam": "spam", "eggs": True}

def test_profile(self, tmpdir_factory):
opts = Options()
assert opts.profile is None
Expand Down Expand Up @@ -108,3 +122,10 @@ def test_to_capabilities(self):
assert "binary" in caps["moz:firefoxOptions"]
assert isinstance(caps["moz:firefoxOptions"]["binary"], basestring)
assert caps["moz:firefoxOptions"]["binary"] == binary._start_cmd

opts.set_preference("spam", "ham")
caps = opts.to_capabilities()
assert "moz:firefoxOptions" in caps
assert "prefs" in caps["moz:firefoxOptions"]
assert isinstance(caps["moz:firefoxOptions"]["prefs"], dict)
assert caps["moz:firefoxOptions"]["prefs"]["spam"] == "ham"
33 changes: 33 additions & 0 deletions py/test/selenium/webdriver/marionette/mn_preferences_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# 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.

import pytest

from selenium.webdriver.firefox.options import Options


@pytest.fixture
def driver_kwargs(request, driver_kwargs):
options = Options()
options.set_preference('browser.startup.homepage_override.mstone', '')
options.set_preference('startup.homepage_welcome_url', 'about:')
driver_kwargs['firefox_options'] = options
return driver_kwargs


def test_preferences_are_used(driver):
assert 'about:' == driver.current_url

0 comments on commit 0e5b18a

Please sign in to comment.