From 39f2e4e05f561f8c795fa40e668b17a1485e66e6 Mon Sep 17 00:00:00 2001 From: lmtierney Date: Thu, 16 Nov 2017 10:03:53 -0600 Subject: [PATCH] [py] add unit tests for Firefox Options --- py/selenium/webdriver/firefox/options.py | 8 +- .../webdriver/chrome/chrome_options_tests.py | 13 +- .../firefox/firefox_options_tests.py | 152 ++++++++++++++++++ 3 files changed, 165 insertions(+), 8 deletions(-) create mode 100644 py/test/unit/selenium/webdriver/firefox/firefox_options_tests.py diff --git a/py/selenium/webdriver/firefox/options.py b/py/selenium/webdriver/firefox/options.py index 69ef7e23eee55..e68d027c31725 100644 --- a/py/selenium/webdriver/firefox/options.py +++ b/py/selenium/webdriver/firefox/options.py @@ -43,7 +43,7 @@ def __init__(self): @property def binary(self): - """Returns the location of the binary.""" + """Returns the FirefoxBinary instance""" return self._binary @binary.setter @@ -58,10 +58,12 @@ def binary(self, new_binary): @property def binary_location(self): - return self.binary + """Returns the location of the binary.""" + return self.binary._start_cmd - @binary.setter # noqa + @binary_location.setter # noqa def binary_location(self, value): + """ Sets the location of the browser binary by string """ self.binary = value @property diff --git a/py/test/unit/selenium/webdriver/chrome/chrome_options_tests.py b/py/test/unit/selenium/webdriver/chrome/chrome_options_tests.py index 3fac7aeed73d2..489d4ed7e9d26 100644 --- a/py/test/unit/selenium/webdriver/chrome/chrome_options_tests.py +++ b/py/test/unit/selenium/webdriver/chrome/chrome_options_tests.py @@ -134,9 +134,12 @@ def test_creates_capabilities(options): options._binary_location = '/bar' options._extensions = ['baz'] options._debugger_address = '/foo/bar' + options._experimental_options = {'foo': 'bar'} caps = options.to_capabilities() - assert Options.KEY in caps - assert 'foo' in caps[Options.KEY]['args'] - assert caps[Options.KEY]['binary'] == '/bar' - assert 'baz' in caps[Options.KEY]['extensions'] - assert caps[Options.KEY]['debuggerAddress'] == '/foo/bar' + opts = caps.get(Options.KEY) + assert opts + assert 'foo' in opts['args'] + assert opts['binary'] == '/bar' + assert 'baz' in opts['extensions'] + assert opts['debuggerAddress'] == '/foo/bar' + assert opts['foo'] == 'bar' diff --git a/py/test/unit/selenium/webdriver/firefox/firefox_options_tests.py b/py/test/unit/selenium/webdriver/firefox/firefox_options_tests.py new file mode 100644 index 0000000000000..0919f4d1ab5ca --- /dev/null +++ b/py/test/unit/selenium/webdriver/firefox/firefox_options_tests.py @@ -0,0 +1,152 @@ +# 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.common.exceptions import InvalidArgumentException +from selenium.webdriver.common.proxy import Proxy, ProxyType +from selenium.webdriver.firefox.firefox_binary import FirefoxBinary +from selenium.webdriver.firefox.firefox_profile import FirefoxProfile +from selenium.webdriver.firefox.options import Options + + +@pytest.fixture +def options(): + yield Options() + + +def test_set_binary_with_firefox_binary(options): + binary = FirefoxBinary('foo') + options.binary = binary + assert options._binary == binary + + +def test_set_binary_with_path(options): + options.binary = '/foo' + assert options._binary._start_cmd == '/foo' + + +def test_get_binary(options): + options.binary = '/foo' + assert options.binary._start_cmd == '/foo' + + +def test_set_binary_location(options): + options.binary_location = '/foo' + assert options._binary._start_cmd == '/foo' + + +def test_get_binary_location(options): + options._binary = FirefoxBinary('/foo') + assert options.binary_location == '/foo' + + +def test_set_preference(options): + options.set_preference('foo', 'bar') + assert options._preferences['foo'] == 'bar' + + +def test_get_preferences(options): + options._preferences = {'foo': 'bar'} + assert options.preferences['foo'] == 'bar' + + +def test_set_proxy(options): + proxy = Proxy({'proxyType': ProxyType.MANUAL}) + options.proxy = proxy + assert options._proxy == proxy + + +def test_raises_exception_if_proxy_is_not_proxy_object(options): + with pytest.raises(InvalidArgumentException): + options.proxy = 'foo' + + +def test_get_proxy(options): + options._proxy = 'foo' + assert options.proxy == 'foo' + + +def test_set_profile_with_firefox_profile(options): + profile = FirefoxProfile() + options.profile = profile + assert options._profile == profile + + +def test_set_profile_with_path(options): + options.profile = None + assert isinstance(options._profile, FirefoxProfile) + + +def test_get_profile(options): + options._profile = 'foo' + assert options.profile == 'foo' + + +def test_add_arguments(options): + options.add_argument('foo') + assert 'foo' in options._arguments + + +def test_get_arguments(options): + options._arguments = ['foo'] + assert 'foo' in options.arguments + + +def test_raises_exception_if_argument_is_falsy(options): + with pytest.raises(ValueError): + options.add_argument(None) + + +def test_set_log_level(options): + options.log.level = 'debug' + assert options.log.level == 'debug' + + +def test_set_headless(options): + options.set_headless() + assert '-headless' in options._arguments + + +def test_unset_headless(options): + options._arguments = ['-headless'] + options.set_headless(False) + assert '-headless' not in options._arguments + + +def test_get_headless(options): + options._arguments = ['-headless'] + assert options.headless + + +def test_creates_capabilities(options): + profile = FirefoxProfile() + options._arguments = ['foo'] + options._binary = FirefoxBinary('/bar') + options._preferences = {'foo': 'bar'} + options._proxy = Proxy({'proxyType': ProxyType.MANUAL}) + options._profile = profile + options.log.level = 'debug' + caps = options.to_capabilities() + opts = caps.get(Options.KEY) + assert opts + assert 'foo' in opts['args'] + assert opts['binary'] == '/bar' + assert opts['prefs']['foo'] == 'bar' + assert opts['profile'] == profile.encoded + assert opts['proxy']['proxyType'] == ProxyType.MANUAL['string'] + assert opts['log']['level'] == 'debug'