Skip to content

Commit 0e5b18a

Browse files
committed
[py] Allow Firefox preferences to be set directly in Options
1 parent dcc80b8 commit 0e5b18a

3 files changed

Lines changed: 66 additions & 0 deletions

File tree

py/selenium/webdriver/firefox/options.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Options(object):
3434

3535
def __init__(self):
3636
self._binary = None
37+
self._preferences = {}
3738
self._profile = None
3839
self._arguments = []
3940
self.log = Log()
@@ -61,6 +62,15 @@ def binary_location(self):
6162
def binary_location(self, value):
6263
self.binary = value
6364

65+
@property
66+
def preferences(self):
67+
"""Returns a dict of preferences."""
68+
return self._preferences
69+
70+
def set_preference(self, name, value):
71+
"""Sets a preference."""
72+
self._preferences[name] = value
73+
6474
@property
6575
def profile(self):
6676
"""Returns the Firefox profile to use."""
@@ -100,6 +110,8 @@ def to_capabilities(self):
100110

101111
if self._binary is not None:
102112
opts["binary"] = self._binary._start_cmd
113+
if len(self._preferences) > 0:
114+
opts["prefs"] = self._preferences
103115
if self._profile is not None:
104116
opts["profile"] = self._profile.encoded
105117
if len(self._arguments) > 0:

py/test/selenium/webdriver/marionette/mn_options_tests.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class TestUnit(object):
4343
def test_ctor(self):
4444
opts = Options()
4545
assert opts._binary is None
46+
assert opts._preferences == {}
4647
assert opts._profile is None
4748
assert opts._arguments == []
4849
assert isinstance(opts.log, Log)
@@ -61,6 +62,19 @@ def test_binary(self):
6162
assert isinstance(opts.binary, FirefoxBinary)
6263
assert opts.binary._start_cmd == path
6364

65+
def test_prefs(self):
66+
opts = Options()
67+
assert len(opts.preferences) == 0
68+
assert isinstance(opts.preferences, dict)
69+
70+
opts.set_preference("spam", "ham")
71+
assert len(opts.preferences) == 1
72+
opts.set_preference("eggs", True)
73+
assert len(opts.preferences) == 2
74+
opts.set_preference("spam", "spam")
75+
assert len(opts.preferences) == 2
76+
assert opts.preferences == {"spam": "spam", "eggs": True}
77+
6478
def test_profile(self, tmpdir_factory):
6579
opts = Options()
6680
assert opts.profile is None
@@ -108,3 +122,10 @@ def test_to_capabilities(self):
108122
assert "binary" in caps["moz:firefoxOptions"]
109123
assert isinstance(caps["moz:firefoxOptions"]["binary"], basestring)
110124
assert caps["moz:firefoxOptions"]["binary"] == binary._start_cmd
125+
126+
opts.set_preference("spam", "ham")
127+
caps = opts.to_capabilities()
128+
assert "moz:firefoxOptions" in caps
129+
assert "prefs" in caps["moz:firefoxOptions"]
130+
assert isinstance(caps["moz:firefoxOptions"]["prefs"], dict)
131+
assert caps["moz:firefoxOptions"]["prefs"]["spam"] == "ham"
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Licensed to the Software Freedom Conservancy (SFC) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The SFC licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
import pytest
19+
20+
from selenium.webdriver.firefox.options import Options
21+
22+
23+
@pytest.fixture
24+
def driver_kwargs(request, driver_kwargs):
25+
options = Options()
26+
options.set_preference('browser.startup.homepage_override.mstone', '')
27+
options.set_preference('startup.homepage_welcome_url', 'about:')
28+
driver_kwargs['firefox_options'] = options
29+
return driver_kwargs
30+
31+
32+
def test_preferences_are_used(driver):
33+
assert 'about:' == driver.current_url

0 commit comments

Comments
 (0)