Best Python code snippet using toolium_python
test_config_driver.py
Source:test_config_driver.py  
...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 mock...config_driver.py
Source:config_driver.py  
...46                self.logger.info("Creating remote driver (type = %s)", driver_type)47                driver = self._create_remote_driver()48            else:49                self.logger.info("Creating local driver (type = %s)", driver_type)50                driver = self._create_local_driver()51        except Exception as exc:52            error_message = get_error_message_from_exception(exc)53            self.logger.error("%s driver can not be launched: %s", driver_type.capitalize(), error_message)54            raise55        return driver56    def _create_remote_driver(self):57        """Create a driver in a remote server58        View valid capabilities in https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities59        :returns: a new remote selenium driver60        """61        # Get server url62        server_url = '{}/wd/hub'.format(self.utils.get_server_url())63        # Get driver capabilities64        driver_name = self.utils.get_driver_name()65        capabilities = self._get_capabilities_from_driver_type(driver_name)66        # Add version and platform capabilities67        self._add_capabilities_from_driver_type(capabilities)68        if driver_name == 'opera':69            capabilities['opera.autostart'] = True70            capabilities['opera.arguments'] = '-fullscreen'71        elif driver_name == 'firefox':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,...driver.py
Source:driver.py  
...65        logger.info('Creating driver')66        if driver_type == self.TYPE_REMOTE:67            instance = self._create_remote_driver(driver, **kwargs)68        else:69            instance = self._create_local_driver(driver, **kwargs)70        try:71            instance.maximize_window()72        except:73            datetime.time.sleep(10)74            try:75                instance.maximize_window()76            except Exception:77                raise78        return instance79    def _create_local_driver(self, driver, **kwargs):80        logger.debug('Creating local driver "%s"', driver)81        try:82            return self.LOCAL_DRIVERS[driver](self)(**kwargs)83        except KeyError:84            raise TypeError("Unsupported Driver Type {0}".format(driver))85    def _create_remote_driver(self, driver, **kwargs):86        if not 'remote_url' in kwargs:87            raise ValueError('Remote drivers require the declaration of a remote_url')88        remote_url = kwargs.get('remote_url')89        logger.info('Creating remot driver "%s" (remote_url=%s)', driver, remote_url)90        try:91            # Get a copy of the desired capabilities object. (to avoid overwriting the global.)92            capabilities = self.DRIVER_CAPABILITIES[driver].copy()93        except KeyError:...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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
