How to use _add_chrome_options_to_capabilities method in toolium

Best Python code snippet using toolium_python

test_config_driver.py

Source:test_config_driver.py Github

copy

Full Screen

1# -*- coding: utf-8 -*-2"""3Copyright 2016 Telefónica Investigación y Desarrollo, S.A.U.4This file is part of Toolium.5Licensed under the Apache License, Version 2.0 (the "License");6you may not use this file except in compliance with the License.7You may obtain a copy of the License at8 http://www.apache.org/licenses/LICENSE-2.09Unless required by applicable law or agreed to in writing, software10distributed under the License is distributed on an "AS IS" BASIS,11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12See the License for the specific language governing permissions and13limitations under the License.14"""15import os16import mock17import pytest18from selenium.webdriver.common.desired_capabilities import DesiredCapabilities19from selenium.webdriver.firefox.options import Options20from toolium.config_driver import ConfigDriver21from toolium.config_parser import ExtendedConfigParser22from toolium.driver_wrappers_pool import DriverWrappersPool23@pytest.fixture24def config():25 config_parser = ExtendedConfigParser()26 config_parser.add_section('Server')27 config_parser.add_section('Driver')28 return config_parser29@pytest.fixture30def utils():31 utils = mock.MagicMock()32 utils.get_driver_name.return_value = 'firefox'33 return utils34def test_create_driver_local_not_configured(config, utils):35 config.set('Driver', 'type', 'firefox')36 utils.get_driver_name.return_value = 'firefox'37 config_driver = ConfigDriver(config, utils)38 config_driver._create_local_driver = lambda: 'local driver mock'39 config_driver._create_remote_driver = lambda: 'remote driver mock'40 driver = config_driver.create_driver()41 assert driver == 'local driver mock'42def test_create_driver_local(config, utils):43 config.set('Server', 'enabled', 'false')44 config.set('Driver', 'type', 'firefox')45 utils.get_driver_name.return_value = 'firefox'46 config_driver = ConfigDriver(config, utils)47 config_driver._create_local_driver = lambda: 'local driver mock'48 config_driver._create_remote_driver = lambda: 'remote driver mock'49 driver = config_driver.create_driver()50 assert driver == 'local driver mock'51def test_create_driver_remote(config, utils):52 config.set('Server', 'enabled', 'true')53 config.set('Driver', 'type', 'firefox')54 utils.get_driver_name.return_value = 'firefox'55 config_driver = ConfigDriver(config, utils)56 config_driver._create_local_driver = lambda: 'local driver mock'57 config_driver._create_remote_driver = lambda: 'remote driver mock'58 driver = config_driver.create_driver()59 assert driver == 'remote driver mock'60@mock.patch('toolium.config_driver.FirefoxOptions')61@mock.patch('toolium.config_driver.webdriver')62def test_create_local_driver_firefox(webdriver_mock, options, config, utils):63 config.set('Driver', 'type', 'firefox')64 config.add_section('Capabilities')65 config.set('Capabilities', 'marionette', 'false')66 utils.get_driver_name.return_value = 'firefox'67 config_driver = ConfigDriver(config, utils)68 config_driver._create_firefox_profile = lambda: 'firefox profile'69 DriverWrappersPool.output_directory = ''70 config_driver._create_local_driver()71 expected_capabilities = DesiredCapabilities.FIREFOX.copy()72 expected_capabilities['marionette'] = False73 webdriver_mock.Firefox.assert_called_once_with(capabilities=expected_capabilities,74 firefox_profile='firefox profile', executable_path=None,75 firefox_options=options(), log_path='geckodriver.log')76@mock.patch('toolium.config_driver.FirefoxOptions')77@mock.patch('toolium.config_driver.webdriver')78def test_create_local_driver_firefox_gecko(webdriver_mock, options, config, utils):79 config.set('Driver', 'type', 'firefox')80 config.add_section('Capabilities')81 config.set('Capabilities', 'marionette', 'true')82 config.set('Driver', 'gecko_driver_path', '/tmp/driver')83 utils.get_driver_name.return_value = 'firefox'84 config_driver = ConfigDriver(config, utils)85 config_driver._create_firefox_profile = lambda: 'firefox profile'86 DriverWrappersPool.output_directory = ''87 config_driver._create_local_driver()88 expected_capabilities = DesiredCapabilities.FIREFOX.copy()89 expected_capabilities['marionette'] = True90 webdriver_mock.Firefox.assert_called_once_with(capabilities=expected_capabilities,91 firefox_profile='firefox profile', executable_path='/tmp/driver',92 firefox_options=options(), log_path='geckodriver.log')93@mock.patch('toolium.config_driver.webdriver')94def test_create_local_driver_firefox_binary(webdriver_mock, config, utils):95 config.set('Driver', 'type', 'firefox')96 config.add_section('Capabilities')97 config.set('Capabilities', 'marionette', 'false')98 config.add_section('Firefox')99 config.set('Firefox', 'binary', '/tmp/firefox')100 utils.get_driver_name.return_value = 'firefox'101 config_driver = ConfigDriver(config, utils)102 config_driver._create_firefox_profile = lambda: 'firefox profile'103 DriverWrappersPool.output_directory = ''104 config_driver._create_local_driver()105 # Check that firefox options contain the firefox binary106 args, kwargs = webdriver_mock.Firefox.call_args107 firefox_options = kwargs['firefox_options']108 assert isinstance(firefox_options, Options)109 if isinstance(firefox_options.binary, str):110 assert firefox_options.binary == '/tmp/firefox' # Selenium 2111 else:112 assert firefox_options.binary._start_cmd == '/tmp/firefox' # Selenium 3113@mock.patch('toolium.config_driver.webdriver')114def test_create_local_driver_chrome(webdriver_mock, config, utils):115 config.set('Driver', 'type', 'chrome')116 config.set('Driver', 'chrome_driver_path', '/tmp/driver')117 utils.get_driver_name.return_value = 'chrome'118 config_driver = ConfigDriver(config, utils)119 config_driver._create_chrome_options = lambda: 'chrome options'120 config_driver._add_chrome_options_to_capabilities = lambda x: None121 config_driver._create_local_driver()122 webdriver_mock.Chrome.assert_called_once_with('/tmp/driver', desired_capabilities=DesiredCapabilities.CHROME)123@mock.patch('toolium.config_driver.webdriver')124def test_create_local_driver_chrome_multiple_options(webdriver_mock, config, utils):125 # From goog:chromeOptions in Capabilities section126 options_from_capabilities = {127 'excludeSwitches': ['enable-automation'], 'useAutomationExtension': False,128 'prefs': {'download.default_directory': '/this_value_will_be_overwritten',129 'download.prompt_for_download': False}130 }131 # From ChromePreferences, ChromeMobileEmulation, ChromeArguments and Chrome sections132 options_from_sections = {133 'prefs': {'download.default_directory': '/tmp'},134 'mobileEmulation': {'deviceName': 'Google Nexus 5'},135 'args': ['user-data-dir=C:\\Users\\USERNAME\\AppData\\Local\\Google\\Chrome\\User Data'],136 'binary': '/usr/local/chrome_beta/chrome'137 }138 # Merged chrome options139 final_chrome_options = {140 'excludeSwitches': ['enable-automation'], 'useAutomationExtension': False,141 'prefs': {'download.default_directory': '/tmp', 'download.prompt_for_download': False},142 'mobileEmulation': {'deviceName': 'Google Nexus 5'},143 'args': ['user-data-dir=C:\\Users\\USERNAME\\AppData\\Local\\Google\\Chrome\\User Data'],144 'binary': '/usr/local/chrome_beta/chrome'145 }146 config.set('Driver', 'type', 'chrome')147 config.set('Driver', 'chrome_driver_path', '/tmp/driver')148 config.add_section('Capabilities')149 config.set('Capabilities', 'goog:chromeOptions', str(options_from_capabilities))150 utils.get_driver_name.return_value = 'chrome'151 config_driver = ConfigDriver(config, utils)152 # Chrome options mock153 chrome_options = mock.MagicMock()154 chrome_options.to_capabilities.return_value = {'goog:chromeOptions': options_from_sections}155 config_driver._create_chrome_options = mock.MagicMock(return_value=chrome_options)156 config_driver._create_local_driver()157 capabilities = DesiredCapabilities.CHROME.copy()158 capabilities['goog:chromeOptions'] = final_chrome_options159 webdriver_mock.Chrome.assert_called_once_with('/tmp/driver', desired_capabilities=capabilities)160@mock.patch('toolium.config_driver.webdriver')161def test_create_local_driver_safari(webdriver_mock, config, utils):162 config.set('Driver', 'type', 'safari')163 utils.get_driver_name.return_value = 'safari'164 config_driver = ConfigDriver(config, utils)165 config_driver._create_local_driver()166 webdriver_mock.Safari.assert_called_once_with(desired_capabilities=DesiredCapabilities.SAFARI)167@mock.patch('toolium.config_driver.webdriver')168def test_create_local_driver_opera(webdriver_mock, config, utils):169 config.set('Driver', 'type', 'opera')170 config.set('Driver', 'opera_driver_path', '/tmp/driver')171 utils.get_driver_name.return_value = 'opera'172 config_driver = ConfigDriver(config, utils)173 config_driver._create_local_driver()174 webdriver_mock.Opera.assert_called_once_with(desired_capabilities=DesiredCapabilities.OPERA,175 executable_path='/tmp/driver')176@mock.patch('toolium.config_driver.webdriver')177def test_create_local_driver_iexplore(webdriver_mock, config, utils):178 config.set('Driver', 'type', 'iexplore')179 config.set('Driver', 'explorer_driver_path', '/tmp/driver')180 utils.get_driver_name.return_value = 'iexplore'181 config_driver = ConfigDriver(config, utils)182 config_driver._create_local_driver()183 webdriver_mock.Ie.assert_called_once_with('/tmp/driver', capabilities=DesiredCapabilities.INTERNETEXPLORER)184@mock.patch('toolium.config_driver.webdriver')185def test_create_local_driver_edge(webdriver_mock, config, utils):186 config.set('Driver', 'type', 'edge')187 config.set('Driver', 'edge_driver_path', '/tmp/driver')188 utils.get_driver_name.return_value = 'edge'189 config_driver = ConfigDriver(config, utils)190 config_driver._create_local_driver()191 webdriver_mock.Edge.assert_called_once_with('/tmp/driver', capabilities=DesiredCapabilities.EDGE)192@mock.patch('toolium.config_driver.webdriver')193def test_create_local_driver_phantomjs(webdriver_mock, config, utils):194 config.set('Driver', 'type', 'phantomjs')195 config.set('Driver', 'phantomjs_driver_path', '/tmp/driver')196 utils.get_driver_name.return_value = 'phantomjs'197 config_driver = ConfigDriver(config, utils)198 config_driver._create_local_driver()199 webdriver_mock.PhantomJS.assert_called_once_with(desired_capabilities=DesiredCapabilities.PHANTOMJS,200 executable_path='/tmp/driver')201def test_create_local_driver_android(config, utils):202 config.set('Driver', 'type', 'android')203 utils.get_driver_name.return_value = 'android'204 config_driver = ConfigDriver(config, utils)205 config_driver._create_remote_driver = lambda: 'remote driver mock'206 driver = config_driver._create_local_driver()207 assert driver == 'remote driver mock'208def test_create_local_driver_ios(config, utils):209 config.set('Driver', 'type', 'ios')210 utils.get_driver_name.return_value = 'ios'211 config_driver = ConfigDriver(config, utils)212 config_driver._create_remote_driver = lambda: 'remote driver mock'213 driver = config_driver._create_local_driver()214 assert driver == 'remote driver mock'215def test_create_local_driver_iphone(config, utils):216 config.set('Driver', 'type', 'iphone')217 utils.get_driver_name.return_value = 'iphone'218 config_driver = ConfigDriver(config, utils)219 config_driver._create_remote_driver = lambda: 'remote driver mock'220 driver = config_driver._create_local_driver()221 assert driver == 'remote driver mock'222def test_create_local_driver_unknown_driver(config, utils):223 config.set('Driver', 'type', 'unknown')224 utils.get_driver_name.return_value = 'unknown'225 config_driver = ConfigDriver(config, utils)226 with pytest.raises(Exception) as excinfo:227 config_driver._create_local_driver()228 assert 'Unknown driver unknown' == str(excinfo.value)229@mock.patch('toolium.config_driver.FirefoxOptions')230@mock.patch('toolium.config_driver.webdriver')231def test_create_local_driver_capabilities(webdriver_mock, options, config, utils):232 config.set('Driver', 'type', 'firefox')233 config.add_section('Capabilities')234 config.set('Capabilities', 'marionette', 'false')235 config.set('Capabilities', 'version', '45')236 utils.get_driver_name.return_value = 'firefox'237 config_driver = ConfigDriver(config, utils)238 config_driver._create_firefox_profile = lambda: 'firefox profile'239 DriverWrappersPool.output_directory = ''240 config_driver._create_local_driver()241 expected_capabilities = DesiredCapabilities.FIREFOX.copy()242 expected_capabilities['marionette'] = False243 expected_capabilities['version'] = '45'244 webdriver_mock.Firefox.assert_called_once_with(capabilities=expected_capabilities,245 firefox_profile='firefox profile', executable_path=None,246 firefox_options=options(), log_path='geckodriver.log')247@mock.patch('toolium.config_driver.webdriver')248def test_create_remote_driver_firefox(webdriver_mock, config, utils):249 config.set('Driver', 'type', 'firefox')250 server_url = 'http://10.20.30.40:5555'251 utils.get_server_url.return_value = server_url252 utils.get_driver_name.return_value = 'firefox'253 config_driver = ConfigDriver(config, utils)254 # Firefox profile mock255 class ProfileMock(object):256 encoded = 'encoded profile'257 config_driver._create_firefox_profile = mock.MagicMock(return_value=ProfileMock())258 config_driver._create_remote_driver()259 capabilities = DesiredCapabilities.FIREFOX.copy()260 capabilities['firefox_profile'] = 'encoded profile'261 webdriver_mock.Remote.assert_called_once_with(command_executor='%s/wd/hub' % server_url,262 desired_capabilities=capabilities)263@mock.patch('toolium.config_driver.webdriver')264def test_create_remote_driver_chrome(webdriver_mock, config, utils):265 config.set('Driver', 'type', 'chrome')266 server_url = 'http://10.20.30.40:5555'267 utils.get_server_url.return_value = server_url268 utils.get_driver_name.return_value = 'chrome'269 config_driver = ConfigDriver(config, utils)270 # Chrome options mock271 chrome_options = mock.MagicMock()272 chrome_options.to_capabilities.return_value = {'goog:chromeOptions': 'chrome options'}273 config_driver._create_chrome_options = mock.MagicMock(return_value=chrome_options)274 config_driver._create_remote_driver()275 capabilities = DesiredCapabilities.CHROME.copy()276 capabilities['goog:chromeOptions'] = 'chrome options'277 webdriver_mock.Remote.assert_called_once_with(command_executor='%s/wd/hub' % server_url,278 desired_capabilities=capabilities)279@mock.patch('toolium.config_driver.webdriver')280def test_create_remote_driver_chrome_multiple_options(webdriver_mock, config, utils):281 # From goog:chromeOptions in Capabilities section282 options_from_capabilities = {283 'excludeSwitches': ['enable-automation'], 'useAutomationExtension': False,284 'prefs': {'download.default_directory': '/this_value_will_be_overwritten',285 'download.prompt_for_download': False}286 }287 # From ChromePreferences, ChromeMobileEmulation, ChromeArguments and Chrome sections288 options_from_sections = {289 'prefs': {'download.default_directory': '/tmp'},290 'mobileEmulation': {'deviceName': 'Google Nexus 5'},291 'args': ['user-data-dir=C:\\Users\\USERNAME\\AppData\\Local\\Google\\Chrome\\User Data'],292 'binary': '/usr/local/chrome_beta/chrome'293 }294 # Merged chrome options295 final_chrome_options = {296 'excludeSwitches': ['enable-automation'], 'useAutomationExtension': False,297 'prefs': {'download.default_directory': '/tmp', 'download.prompt_for_download': False},298 'mobileEmulation': {'deviceName': 'Google Nexus 5'},299 'args': ['user-data-dir=C:\\Users\\USERNAME\\AppData\\Local\\Google\\Chrome\\User Data'],300 'binary': '/usr/local/chrome_beta/chrome'301 }302 config.set('Driver', 'type', 'chrome')303 config.add_section('Capabilities')304 config.set('Capabilities', 'goog:chromeOptions', str(options_from_capabilities))305 server_url = 'http://10.20.30.40:5555'306 utils.get_server_url.return_value = server_url307 utils.get_driver_name.return_value = 'chrome'308 config_driver = ConfigDriver(config, utils)309 # Chrome options mock310 chrome_options = mock.MagicMock()311 chrome_options.to_capabilities.return_value = {'goog:chromeOptions': options_from_sections}312 config_driver._create_chrome_options = mock.MagicMock(return_value=chrome_options)313 config_driver._create_remote_driver()314 capabilities = DesiredCapabilities.CHROME.copy()315 capabilities['goog:chromeOptions'] = final_chrome_options316 webdriver_mock.Remote.assert_called_once_with(command_executor='%s/wd/hub' % server_url,317 desired_capabilities=capabilities)318@mock.patch('toolium.config_driver.webdriver')319def test_create_remote_driver_chrome_old_selenium(webdriver_mock, config, utils):320 config.set('Driver', 'type', 'chrome')321 server_url = 'http://10.20.30.40:5555'322 utils.get_server_url.return_value = server_url323 utils.get_driver_name.return_value = 'chrome'324 config_driver = ConfigDriver(config, utils)325 # Chrome options mock326 chrome_options = mock.MagicMock()327 chrome_options.to_capabilities.return_value = {'chromeOptions': 'chrome options'}328 config_driver._create_chrome_options = mock.MagicMock(return_value=chrome_options)329 config_driver._create_remote_driver()330 capabilities = DesiredCapabilities.CHROME.copy()331 capabilities['chromeOptions'] = 'chrome options'332 webdriver_mock.Remote.assert_called_once_with(command_executor='%s/wd/hub' % server_url,333 desired_capabilities=capabilities)334@mock.patch('toolium.config_driver.webdriver')335def test_create_remote_driver_safari(webdriver_mock, config, utils):336 config.set('Driver', 'type', 'safari')337 server_url = 'http://10.20.30.40:5555'338 utils.get_server_url.return_value = server_url339 utils.get_driver_name.return_value = 'safari'340 config_driver = ConfigDriver(config, utils)341 config_driver._create_remote_driver()342 webdriver_mock.Remote.assert_called_once_with(command_executor='%s/wd/hub' % server_url,343 desired_capabilities=DesiredCapabilities.SAFARI)344@mock.patch('toolium.config_driver.webdriver')345def test_create_remote_driver_opera(webdriver_mock, config, utils):346 config.set('Driver', 'type', 'opera')347 server_url = 'http://10.20.30.40:5555'348 utils.get_server_url.return_value = server_url349 utils.get_driver_name.return_value = 'opera'350 config_driver = ConfigDriver(config, utils)351 config_driver._create_remote_driver()352 capabilities = DesiredCapabilities.OPERA353 capabilities['opera.autostart'] = True354 capabilities['opera.arguments'] = '-fullscreen'355 webdriver_mock.Remote.assert_called_once_with(command_executor='%s/wd/hub' % server_url,356 desired_capabilities=capabilities)357@mock.patch('toolium.config_driver.webdriver')358def test_create_remote_driver_iexplore(webdriver_mock, config, utils):359 config.set('Driver', 'type', 'iexplore')360 server_url = 'http://10.20.30.40:5555'361 utils.get_server_url.return_value = server_url362 utils.get_driver_name.return_value = 'iexplore'363 config_driver = ConfigDriver(config, utils)364 config_driver._create_remote_driver()365 webdriver_mock.Remote.assert_called_once_with(command_executor='%s/wd/hub' % server_url,366 desired_capabilities=DesiredCapabilities.INTERNETEXPLORER)367@mock.patch('toolium.config_driver.webdriver')368def test_create_remote_driver_edge(webdriver_mock, config, utils):369 config.set('Driver', 'type', 'edge')370 server_url = 'http://10.20.30.40:5555'371 utils.get_server_url.return_value = server_url372 utils.get_driver_name.return_value = 'edge'373 config_driver = ConfigDriver(config, utils)374 config_driver._create_remote_driver()375 webdriver_mock.Remote.assert_called_once_with(command_executor='%s/wd/hub' % server_url,376 desired_capabilities=DesiredCapabilities.EDGE)377@mock.patch('toolium.config_driver.webdriver')378def test_create_remote_driver_phantomjs(webdriver_mock, config, utils):379 config.set('Driver', 'type', 'phantomjs')380 server_url = 'http://10.20.30.40:5555'381 utils.get_server_url.return_value = server_url382 utils.get_driver_name.return_value = 'phantomjs'383 config_driver = ConfigDriver(config, utils)384 config_driver._create_remote_driver()385 webdriver_mock.Remote.assert_called_once_with(command_executor='%s/wd/hub' % server_url,386 desired_capabilities=DesiredCapabilities.PHANTOMJS)387@mock.patch('toolium.config_driver.appiumdriver')388def test_create_remote_driver_android(appiumdriver_mock, config, utils):389 config.set('Driver', 'type', 'android')390 config.add_section('AppiumCapabilities')391 config.set('AppiumCapabilities', 'automationName', 'Appium')392 config.set('AppiumCapabilities', 'platformName', 'Android')393 server_url = 'http://10.20.30.40:5555'394 utils.get_server_url.return_value = server_url395 utils.get_driver_name.return_value = 'android'396 config_driver = ConfigDriver(config, utils)397 config_driver._create_remote_driver()398 capabilities = {'automationName': 'Appium', 'platformName': 'Android'}399 appiumdriver_mock.Remote.assert_called_once_with(command_executor='%s/wd/hub' % server_url,400 desired_capabilities=capabilities)401@mock.patch('toolium.config_driver.appiumdriver')402def test_create_remote_driver_ios(appiumdriver_mock, config, utils):403 config.set('Driver', 'type', 'ios')404 config.add_section('AppiumCapabilities')405 config.set('AppiumCapabilities', 'automationName', 'Appium')406 config.set('AppiumCapabilities', 'platformName', 'iOS')407 server_url = 'http://10.20.30.40:5555'408 utils.get_server_url.return_value = server_url409 utils.get_driver_name.return_value = 'ios'410 config_driver = ConfigDriver(config, utils)411 config_driver._create_remote_driver()412 capabilities = {'automationName': 'Appium', 'platformName': 'iOS'}413 appiumdriver_mock.Remote.assert_called_once_with(command_executor='%s/wd/hub' % server_url,414 desired_capabilities=capabilities)415@mock.patch('toolium.config_driver.appiumdriver')416def test_create_remote_driver_iphone(appiumdriver_mock, config):417 config.set('Driver', 'type', 'iphone')418 config.add_section('AppiumCapabilities')419 config.set('AppiumCapabilities', 'automationName', 'Appium')420 config.set('AppiumCapabilities', 'platformName', 'iOS')421 server_url = 'http://10.20.30.40:5555'422 utils = mock.MagicMock()423 utils.get_server_url.return_value = server_url424 utils.get_driver_name.return_value = 'iphone'425 config_driver = ConfigDriver(config, utils)426 config_driver._create_remote_driver()427 capabilities = {'automationName': 'Appium', 'platformName': 'iOS'}428 appiumdriver_mock.Remote.assert_called_once_with(command_executor='%s/wd/hub' % server_url,429 desired_capabilities=capabilities)430@mock.patch('toolium.config_driver.webdriver')431def test_create_remote_driver_version_platform(webdriver_mock, config, utils):432 config.set('Driver', 'type', 'iexplore-11-on-WIN10')433 server_url = 'http://10.20.30.40:5555'434 utils.get_server_url.return_value = server_url435 utils.get_driver_name.return_value = 'iexplore'436 config_driver = ConfigDriver(config, utils)437 config_driver._create_remote_driver()438 capabilities = DesiredCapabilities.INTERNETEXPLORER439 capabilities['version'] = '11'440 capabilities['platform'] = 'WIN10'441 webdriver_mock.Remote.assert_called_once_with(command_executor='%s/wd/hub' % server_url,442 desired_capabilities=capabilities)443@mock.patch('toolium.config_driver.webdriver')444def test_create_remote_driver_version(webdriver_mock, config, utils):445 config.set('Driver', 'type', 'iexplore-11')446 server_url = 'http://10.20.30.40:5555'447 utils.get_server_url.return_value = server_url448 utils.get_driver_name.return_value = 'iexplore'449 config_driver = ConfigDriver(config, utils)450 config_driver._create_remote_driver()451 capabilities = DesiredCapabilities.INTERNETEXPLORER.copy()452 capabilities['version'] = '11'453 webdriver_mock.Remote.assert_called_once_with(command_executor='%s/wd/hub' % server_url,454 desired_capabilities=capabilities)455@mock.patch('toolium.config_driver.webdriver')456def test_create_remote_driver_capabilities(webdriver_mock, config, utils):457 config.set('Driver', 'type', 'iexplore-11')458 config.add_section('Capabilities')459 config.set('Capabilities', 'version', '11')460 server_url = 'http://10.20.30.40:5555'461 utils.get_server_url.return_value = server_url462 utils.get_driver_name.return_value = 'iexplore'463 config_driver = ConfigDriver(config, utils)464 config_driver._create_remote_driver()465 capabilities = DesiredCapabilities.INTERNETEXPLORER.copy()466 capabilities['version'] = '11'467 webdriver_mock.Remote.assert_called_once_with(command_executor='%s/wd/hub' % server_url,468 desired_capabilities=capabilities)469def test_convert_property_type_true(config, utils):470 config_driver = ConfigDriver(config, utils)471 value = 'True'472 assert config_driver._convert_property_type(value) is True473def test_convert_property_type_false(config, utils):474 config_driver = ConfigDriver(config, utils)475 value = 'False'476 assert config_driver._convert_property_type(value) is False477def test_convert_property_type_dict(config, utils):478 config_driver = ConfigDriver(config, utils)479 value = "{'a': 5}"480 assert config_driver._convert_property_type(value) == {'a': 5}481def test_convert_property_type_int(config, utils):482 config_driver = ConfigDriver(config, utils)483 value = '5'484 assert config_driver._convert_property_type(value) == 5485def test_convert_property_type_str(config, utils):486 config_driver = ConfigDriver(config, utils)487 value = 'string'488 assert config_driver._convert_property_type(value) == value489def test_convert_property_type_list(config, utils):490 config_driver = ConfigDriver(config, utils)491 value = "[1, 2, 3]"492 assert config_driver._convert_property_type(value) == [1, 2, 3]493@mock.patch('toolium.config_driver.webdriver')494def test_create_firefox_profile(webdriver_mock, config, utils):495 config.add_section('Firefox')496 config.set('Firefox', 'profile', '/tmp')497 config.add_section('FirefoxPreferences')498 config.set('FirefoxPreferences', 'browser.download.folderList', '2')499 config.add_section('FirefoxExtensions')500 config.set('FirefoxExtensions', 'firebug', 'resources/firebug-3.0.0-beta.3.xpi')501 config_driver = ConfigDriver(config, utils)502 config_driver._create_firefox_profile()503 webdriver_mock.FirefoxProfile.assert_called_once_with(profile_directory='/tmp')504 webdriver_mock.FirefoxProfile().set_preference.assert_called_once_with('browser.download.folderList', 2)505 webdriver_mock.FirefoxProfile().update_preferences.assert_called_once_with()506 webdriver_mock.FirefoxProfile().add_extension.assert_called_once_with('resources/firebug-3.0.0-beta.3.xpi')507def test_add_firefox_arguments(config, utils):508 config.add_section('FirefoxArguments')509 config.set('FirefoxArguments', '-private', '')510 config_driver = ConfigDriver(config, utils)511 firefox_options = Options()512 config_driver._add_firefox_arguments(firefox_options)513 assert firefox_options.arguments == ['-private']514@mock.patch('toolium.config_driver.webdriver')515def test_create_chrome_options(webdriver_mock, config, utils):516 config.add_section('ChromePreferences')517 config.set('ChromePreferences', 'download.default_directory', '/tmp')518 config.add_section('ChromeMobileEmulation')519 config.set('ChromeMobileEmulation', 'deviceName', 'Google Nexus 5')520 config.add_section('ChromeArguments')521 config.set('ChromeArguments', 'lang', 'es')522 config_driver = ConfigDriver(config, utils)523 config_driver._create_chrome_options()524 webdriver_mock.ChromeOptions.assert_called_once_with()525 webdriver_mock.ChromeOptions().add_experimental_option.assert_has_calls(526 [mock.call('prefs', {'download.default_directory': '/tmp'}),527 mock.call('mobileEmulation', {'deviceName': 'Google Nexus 5'})]528 )529 webdriver_mock.ChromeOptions().add_argument.assert_called_once_with('lang=es')530@mock.patch('toolium.config_driver.webdriver')531def test_create_chrome_options_headless(webdriver_mock, config, utils):532 config.set('Driver', 'headless', 'true')533 config_driver = ConfigDriver(config, utils)534 config_driver._create_chrome_options()535 webdriver_mock.ChromeOptions.assert_called_once_with()536 if os.name == 'nt':537 webdriver_mock.ChromeOptions().add_argument.assert_has_calls([mock.call('--headless'),538 mock.call('--disable-gpu')])539 else:...

Full Screen

Full Screen

config_driver.py

Source:config_driver.py Github

copy

Full Screen

...72 capabilities['firefox_profile'] = self._create_firefox_profile().encoded73 # Add custom driver capabilities74 self._add_capabilities_from_properties(capabilities, 'Capabilities')75 if driver_name == 'chrome':76 self._add_chrome_options_to_capabilities(capabilities)77 if driver_name in ('android', 'ios', 'iphone'):78 # Create remote appium driver79 self._add_capabilities_from_properties(capabilities, 'AppiumCapabilities')80 return appiumdriver.Remote(command_executor=server_url, desired_capabilities=capabilities)81 else:82 # Create remote web driver83 return webdriver.Remote(command_executor=server_url, desired_capabilities=capabilities)84 def _create_local_driver(self):85 """Create a driver in local machine86 :returns: a new local selenium driver87 """88 driver_name = self.utils.get_driver_name()89 if driver_name in ('android', 'ios', 'iphone'):90 # Create local appium driver91 driver = self._setup_appium()92 else:93 driver_setup = {94 'firefox': self._setup_firefox,95 'chrome': self._setup_chrome,96 'safari': self._setup_safari,97 'opera': self._setup_opera,98 'iexplore': self._setup_explorer,99 'edge': self._setup_edge,100 'phantomjs': self._setup_phantomjs101 }102 try:103 driver_setup_method = driver_setup[driver_name]104 except KeyError:105 raise Exception('Unknown driver {0}'.format(driver_name))106 # Get driver capabilities107 capabilities = self._get_capabilities_from_driver_type(driver_name)108 self._add_capabilities_from_properties(capabilities, 'Capabilities')109 # Create local selenium driver110 driver = driver_setup_method(capabilities)111 return driver112 @staticmethod113 def _get_capabilities_from_driver_type(driver_name):114 """Create initial driver capabilities115 :params driver_name: name of selected driver116 :returns: capabilities dictionary117 """118 if driver_name == 'firefox':119 capabilities = DesiredCapabilities.FIREFOX.copy()120 elif driver_name == 'chrome':121 capabilities = DesiredCapabilities.CHROME.copy()122 elif driver_name == 'safari':123 capabilities = DesiredCapabilities.SAFARI.copy()124 elif driver_name == 'opera':125 capabilities = DesiredCapabilities.OPERA.copy()126 elif driver_name == 'iexplore':127 capabilities = DesiredCapabilities.INTERNETEXPLORER.copy()128 elif driver_name == 'edge':129 capabilities = DesiredCapabilities.EDGE.copy()130 elif driver_name == 'phantomjs':131 capabilities = DesiredCapabilities.PHANTOMJS.copy()132 elif driver_name in ('android', 'ios', 'iphone'):133 capabilities = {}134 else:135 raise Exception('Unknown driver {0}'.format(driver_name))136 return capabilities137 def _add_capabilities_from_driver_type(self, capabilities):138 """Extract version and platform from driver type and add them to capabilities139 :param capabilities: capabilities dict140 """141 driver_type = self.config.get('Driver', 'type')142 try:143 capabilities['version'] = driver_type.split('-')[1]144 except IndexError:145 pass146 try:147 platforms_list = {'xp': 'XP',148 'windows_7': 'VISTA',149 'windows_8': 'WIN8',150 'windows_10': 'WIN10',151 'linux': 'LINUX',152 'android': 'ANDROID',153 'mac': 'MAC'}154 capabilities['platform'] = platforms_list.get(driver_type.split('-')[3], driver_type.split('-')[3])155 except IndexError:156 pass157 def _add_capabilities_from_properties(self, capabilities, section):158 """Add capabilities from properties file159 :param capabilities: capabilities object160 :param section: properties section161 """162 cap_type = {'Capabilities': 'server', 'AppiumCapabilities': 'Appium server'}163 try:164 for cap, cap_value in dict(self.config.items(section)).items():165 self.logger.debug("Added %s capability: %s = %s", cap_type[section], cap, cap_value)166 cap_value = cap_value if cap == 'version' else self._convert_property_type(cap_value)167 self._update_dict(capabilities, {cap: cap_value}, initial_key=cap)168 except NoSectionError:169 pass170 def _setup_firefox(self, capabilities):171 """Setup Firefox webdriver172 :param capabilities: capabilities object173 :returns: a new local Firefox driver174 """175 if capabilities.get("marionette"):176 gecko_driver = self.config.get('Driver', 'gecko_driver_path')177 self.logger.debug("Gecko driver path given in properties: %s", gecko_driver)178 else:179 gecko_driver = None180 # Get Firefox binary181 firefox_binary = self.config.get_optional('Firefox', 'binary')182 firefox_options = FirefoxOptions()183 if self.config.getboolean_optional('Driver', 'headless'):184 self.logger.debug("Running Firefox in headless mode")185 firefox_options.add_argument('-headless')186 self._add_firefox_arguments(firefox_options)187 if firefox_binary:188 firefox_options.binary = firefox_binary189 log_path = os.path.join(DriverWrappersPool.output_directory, 'geckodriver.log')190 try:191 # Selenium 3192 return webdriver.Firefox(firefox_profile=self._create_firefox_profile(), capabilities=capabilities,193 executable_path=gecko_driver, firefox_options=firefox_options, log_path=log_path)194 except TypeError:195 # Selenium 2196 return webdriver.Firefox(firefox_profile=self._create_firefox_profile(), capabilities=capabilities,197 executable_path=gecko_driver, firefox_options=firefox_options)198 def _add_firefox_arguments(self, options):199 """Add Firefox arguments from properties file200 :param options: Firefox options object201 """202 try:203 for pref, pref_value in dict(self.config.items('FirefoxArguments')).items():204 pref_value = '={}'.format(pref_value) if pref_value else ''205 self.logger.debug("Added Firefox argument: %s%s", pref, pref_value)206 options.add_argument('{}{}'.format(pref, self._convert_property_type(pref_value)))207 except NoSectionError:208 pass209 def _create_firefox_profile(self):210 """Create and configure a firefox profile211 :returns: firefox profile212 """213 # Get Firefox profile214 profile_directory = self.config.get_optional('Firefox', 'profile')215 if profile_directory:216 self.logger.debug("Using firefox profile: %s", profile_directory)217 # Create Firefox profile218 profile = webdriver.FirefoxProfile(profile_directory=profile_directory)219 profile.native_events_enabled = True220 # Add Firefox preferences221 try:222 for pref, pref_value in dict(self.config.items('FirefoxPreferences')).items():223 self.logger.debug("Added firefox preference: %s = %s", pref, pref_value)224 profile.set_preference(pref, self._convert_property_type(pref_value))225 profile.update_preferences()226 except NoSectionError:227 pass228 # Add Firefox extensions229 try:230 for pref, pref_value in dict(self.config.items('FirefoxExtensions')).items():231 self.logger.debug("Added firefox extension: %s = %s", pref, pref_value)232 profile.add_extension(pref_value)233 except NoSectionError:234 pass235 return profile236 @staticmethod237 def _convert_property_type(value):238 """Converts the string value in a boolean, integer or string239 :param value: string value240 :returns: boolean, integer or string value241 """242 if value in ('true', 'True'):243 formatted_value = True244 elif value in ('false', 'False'):245 formatted_value = False246 elif ((str(value).startswith('{') and str(value).endswith('}'))247 or (str(value).startswith('[') and str(value).endswith(']'))):248 formatted_value = ast.literal_eval(value)249 else:250 try:251 formatted_value = int(value)252 except ValueError:253 formatted_value = value254 return formatted_value255 def _setup_chrome(self, capabilities):256 """Setup Chrome webdriver257 :param capabilities: capabilities object258 :returns: a new local Chrome driver259 """260 chrome_driver = self.config.get('Driver', 'chrome_driver_path')261 self.logger.debug("Chrome driver path given in properties: %s", chrome_driver)262 self._add_chrome_options_to_capabilities(capabilities)263 return webdriver.Chrome(chrome_driver, desired_capabilities=capabilities)264 def _create_chrome_options(self):265 """Create and configure a chrome options object266 :returns: chrome options object267 """268 # Get Chrome binary269 chrome_binary = self.config.get_optional('Chrome', 'binary')270 # Create Chrome options271 options = webdriver.ChromeOptions()272 if self.config.getboolean_optional('Driver', 'headless'):273 self.logger.debug("Running Chrome in headless mode")274 options.add_argument('--headless')275 if os.name == 'nt': # Temporarily needed if running on Windows.276 options.add_argument('--disable-gpu')277 if chrome_binary is not None:278 options.binary_location = chrome_binary279 # Add Chrome preferences, mobile emulation options and chrome arguments280 self._add_chrome_options(options, 'prefs')281 self._add_chrome_options(options, 'mobileEmulation')282 self._add_chrome_arguments(options)283 return options284 def _add_chrome_options(self, options, option_name):285 """Add Chrome options from properties file286 :param options: chrome options object287 :param option_name: chrome option name288 """289 options_conf = {'prefs': {'section': 'ChromePreferences', 'message': 'preference'},290 'mobileEmulation': {'section': 'ChromeMobileEmulation', 'message': 'mobile emulation option'}}291 option_value = dict()292 try:293 for key, value in dict(self.config.items(options_conf[option_name]['section'])).items():294 self.logger.debug("Added chrome %s: %s = %s", options_conf[option_name]['message'], key, value)295 option_value[key] = self._convert_property_type(value)296 if len(option_value) > 0:297 options.add_experimental_option(option_name, option_value)298 except NoSectionError:299 pass300 def _add_chrome_arguments(self, options):301 """Add Chrome arguments from properties file302 :param options: chrome options object303 """304 try:305 for pref, pref_value in dict(self.config.items('ChromeArguments')).items():306 pref_value = '={}'.format(pref_value) if pref_value else ''307 self.logger.debug("Added chrome argument: %s%s", pref, pref_value)308 options.add_argument('{}{}'.format(pref, self._convert_property_type(pref_value)))309 except NoSectionError:310 pass311 def _add_chrome_options_to_capabilities(self, capabilities):312 """Add Chrome options to capabilities313 :param capabilities: dictionary with driver capabilities314 """315 chrome_capabilities = self._create_chrome_options().to_capabilities()316 options_key = None317 if 'goog:chromeOptions' in chrome_capabilities:318 options_key = 'goog:chromeOptions'319 elif 'chromeOptions' in chrome_capabilities:320 # Selenium 3.5.3 and older321 options_key = 'chromeOptions'322 if options_key:323 self._update_dict(capabilities, chrome_capabilities, initial_key=options_key)324 def _update_dict(self, initial, update, initial_key=None):325 """ Update a initial dict with another dict values recursively...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run toolium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful