def test_get_connection_manager_with_proxy(mock_proxy_settings): remote_connection = RemoteConnection('http://remote', keep_alive=False) conn = remote_connection._get_connection_manager() assert type(conn) == urllib3.ProxyManager assert conn.proxy.scheme == 'http' assert conn.proxy.host == 'http_proxy.com' assert conn.proxy.port == 8080 remote_connection_https = RemoteConnection('https://remote', keep_alive=False) conn = remote_connection_https._get_connection_manager() assert type(conn) == urllib3.ProxyManager assert conn.proxy.scheme == 'http' assert conn.proxy.host == 'https_proxy.com' assert conn.proxy.port == 8080
def __init__(self, session_id=None, browser_name=''): command_executor = 'http://localhost:4444/wd/hub' platform = version = '' javascript_enabled = True self.command_executor = command_executor if type(self.command_executor) is str: self.command_executor = RemoteConnection(command_executor) self.command_executor._commands['GET_SESSION'] = ( 'GET', '/session/$sessionId') self.session_id = session_id self.capabilities = {} self.error_handler = ErrorHandler() if session_id: self.connect_to_session(browser_name=browser_name, platform=platform, version=version, javascript_enabled=javascript_enabled) else: self.start_session(browser_name=browser_name, platform=platform, version=version, javascript_enabled=javascript_enabled)
def browser(request, browser_config): caps = {} caps.update(browser_config) build_tag = "nerodia-build" username = environ.get('SAUCE_USERNAME', None) access_key = environ.get('SAUCE_ACCESS_KEY', None) selenium_endpoint = "http://ondemand.saucelabs.com/wd/hub" caps['username'] = username caps['accesskey'] = access_key caps['name'] = request.node.name caps['build'] = build_tag executor = RemoteConnection(selenium_endpoint, resolve_ip=False) remote = webdriver.Remote(command_executor=executor, desired_capabilities=caps) browser = Browser(browser=remote, desired_capabilities=caps) yield browser sauce_result = "failed" if request.node.rep_call.failed else "passed" browser.execute_script("sauce:job-result={}".format(sauce_result)) browser.quit()
def __init__(self, options=None, capabilities=None, service_url=None, session_id=None): if options == None: self.service = False else: self.service = True self.address = options[0] self.who = options[1] # super(Chrome_Remote, self).__init__(port=port) if service_url is None and session_id is None: raise NameError if capabilities is None: capabilities = DesiredCapabilities.CHROME.copy() self.capabilities = dict(capabilities) self.w3c = True executor = ChromeRemoteConnection(remote_server_addr=service_url) self.session_id = session_id self.command_executor = executor self.command_executor.w3c = self.w3c if type(self.command_executor) is bytes or isinstance( self.command_executor, str): self.command_executor = RemoteConnection(self.command_executor, keep_alive=True) self._is_remote = True self.error_handler = ErrorHandler() self._switch_to = SwitchTo(self) self._mobile = Mobile(self) self.file_detector = LocalFileDetector()
def driver(request): # if the assignment below does not make sense to you please read up on object assignments. # The point is to make a copy and not mess with the original test spec. desired_caps = {} browser = { "platform": "Windows 10", "browserName": "chrome", "version": "latest", "extendedDebugging": True } desired_caps.update(browser) test_name = request.node.name username = environ.get('SAUCE_USERNAME', None) access_key = environ.get('SAUCE_ACCESS_KEY', None) selenium_endpoint = "http://{}:{}@ondemand.saucelabs.com:80/wd/hub".format(username, access_key) desired_caps['name'] = test_name executor = RemoteConnection(selenium_endpoint, resolve_ip=False) browser = webdriver.Remote( command_executor=executor, desired_capabilities=desired_caps ) yield browser def fin(): browser.execute_script("sauce:job-result={}".format(str(not request.node.rep_call.failed).lower())) browser.quit() request.addfinalizer(fin)
def _update_command_executor(self, keep_alive): """Update command executor following directConnect feature""" direct_protocol = 'directConnectProtocol' direct_host = 'directConnectHost' direct_port = 'directConnectPort' direct_path = 'directConnectPath' if (not {direct_protocol, direct_host, direct_port, direct_path}.issubset(set(self.capabilities))): message = 'Direct connect capabilities from server were:\n' for key in [direct_protocol, direct_host, direct_port, direct_path]: message += '{}: \'{}\'\n'.format(key, self.capabilities.get(key, '')) logger.warning(message) return protocol = self.capabilities[direct_protocol] hostname = self.capabilities[direct_host] port = self.capabilities[direct_port] path = self.capabilities[direct_path] executor = '{scheme}://{hostname}:{port}{path}'.format( scheme=protocol, hostname=hostname, port=port, path=path ) logger.info('Updated request endpoint to %s', executor) # Override command executor self.command_executor = RemoteConnection(executor, keep_alive=keep_alive) self._addCommands()
def setup(self, runner): """Connect to browser via Selenium's WebDriver implementation.""" self.runner = runner self.logger.debug("Connecting to Selenium on URL: %s" % self.url) session_started = False try: self.webdriver = webdriver.Remote(command_executor=RemoteConnection(self.url.strip("/"), resolve_ip=False), desired_capabilities=self.capabilities) except: self.logger.warning( "Connecting to Selenium failed:\n%s" % traceback.format_exc()) else: self.logger.debug("Selenium session started") session_started = True if not session_started: self.logger.warning("Failed to connect to Selenium") self.executor.runner.send_message("init_failed") else: try: self.after_connect() except: print >> sys.stderr, traceback.format_exc() self.logger.warning( "Failed to connect to navigate initial page") self.executor.runner.send_message("init_failed") else: self.executor.runner.send_message("init_succeeded")
def __init__(self, executable_path='MicrosoftWebDriver.exe', capabilities=None, port=0, verbose=False, log_path=None): self.port = port if self.port == 0: self.port = utils.free_port() self.edge_service = Service(executable_path, port=self.port, verbose=verbose, log_path=log_path) self.edge_service.start() if capabilities is None: capabilities = DesiredCapabilities.EDGE RemoteWebDriver.__init__(self, command_executor=RemoteConnection( 'http://localhost:%d' % self.port, resolve_ip=False), desired_capabilities=capabilities) self._is_remote = False
def test_get_connection_manager_for_certs_and_timeout(): remote_connection = RemoteConnection('http://remote', keep_alive=False) remote_connection.set_timeout(10) conn = remote_connection._get_connection_manager() assert conn.connection_pool_kw['timeout'] == 10 assert conn.connection_pool_kw['cert_reqs'] == 'CERT_REQUIRED' assert 'certifi/cacert.pem' in conn.connection_pool_kw['ca_certs']
def connect(self): """Connect to browser via Selenium's WebDriver implementation.""" self.logger.debug("Connecting to Selenium on URL: %s" % self.url) self.webdriver = webdriver.Remote(command_executor=RemoteConnection(self.url.strip("/"), resolve_ip=False), desired_capabilities=self.capabilities)
def __get_remote_driver(cls, browser_name, host, port, desired_capabilities=None, **kwargs): """ Return remote WebDriver instance. Parameters: see `__new__()` Return: <Remote> WebDriver instance Throws: KeyError - wrong browser name """ command_executor = RemoteConnection('http://%s:%s/wd/hub' % (host, port)) try: capabilities = cls.__desired_capabilities_map[browser_name].copy() except KeyError as ex: ex.args = ("unknown browser: '%s' (valid browsers: %s)"\ % (browser_name, ', '.join(cls.__desired_capabilities_map.keys())),) raise ex if desired_capabilities: capabilities.update(desired_capabilities) return Remote(command_executor=command_executor, desired_capabilities=capabilities, **kwargs)
def test_get_connection_manager_for_certs_and_timeout(monkeypatch): monkeypatch.setattr(RemoteConnection, "get_timeout", lambda _: 10) # Class state; leaks into subsequent tests. remote_connection = RemoteConnection('http://remote', keep_alive=False) conn = remote_connection._get_connection_manager() assert conn.connection_pool_kw['timeout'] == 10 assert conn.connection_pool_kw['cert_reqs'] == 'CERT_REQUIRED' assert 'certifi/cacert.pem' in conn.connection_pool_kw['ca_certs']
def test_get_proxy_url_https_auth(mock_proxy_auth_settings): remote_connection = RemoteConnection('https://remote', keep_alive=False) proxy_url = remote_connection._get_proxy_url() raw_proxy_url, basic_auth_string = remote_connection._seperate_http_proxy_auth() assert proxy_url == "https://*****:*****@https_proxy.com:8080" assert raw_proxy_url == "https://https_proxy.com:8080" assert basic_auth_string == "user:password"
def driver(request, browser_config): # if the assignment below does not make sense to you please read up on object assignments. # The point is to make a copy and not mess with the original test spec. desired_caps = dict() desired_caps.update(browser_config) test_name = request.node.name username = environ.get('KOBITON_USERNAME', None) access_key = environ.get('KOBITON_ACCESS_KEY', None) selenium_endpoint = "https://%s:%[email protected]/wd/hub" % (username, access_key) desired_caps['name'] = test_name executor = RemoteConnection(selenium_endpoint, resolve_ip=False) browser = webdriver.Remote(command_executor=executor, desired_capabilities=desired_caps) # In case test fails after selenium session creation having this here will help track it down. # creates one file per test non ideal but xdist is awful if browser is not None: with open("%s.testlog" % browser.session_id, 'w') as f: f.write("SessionID=%s job-name=%s\n" % (browser.session_id, test_name)) else: raise WebDriverException("Never created!") yield browser # Teardown starts here # report results try: browser.quit() except WebDriverException: # we can ignore the exceptions of WebDriverException type -> We're done with tests. print( 'Warning: The driver failed to quit properly. Check test and server side logs.' )
def init_connection(self): try: self.conn = RemoteConnection(self.args.host) except Exception: exc_class, exc, tb = sys.exc_info() new_exc = ConnectionException( "Error connecting to Selenium server") raise new_exc.__class__, new_exc, tb
def __init__(self, remoteUrl, capabilities): ### @todo refactor me self.command_executor = RemoteConnection(remoteUrl) self.error_handler = ErrorHandler() self._is_remote = True self.start_client() self.session_id = capabilities["webdriver.remote.sessionid"] self.capabilities = capabilities
def test_get_connection_manager_with_auth_proxy(mock_proxy_auth_settings): proxy_auth_header = urllib3.make_headers(proxy_basic_auth="user:password") remote_connection = RemoteConnection('http://remote', keep_alive=False) conn = remote_connection._get_connection_manager() assert type(conn) == urllib3.ProxyManager assert conn.proxy.scheme == 'http' assert conn.proxy.host == 'http_proxy.com' assert conn.proxy.port == 8080 assert conn.proxy_headers == proxy_auth_header remote_connection_https = RemoteConnection('https://remote', keep_alive=False) conn = remote_connection_https._get_connection_manager() assert type(conn) == urllib3.ProxyManager assert conn.proxy.scheme == 'https' assert conn.proxy.host == 'https_proxy.com' assert conn.proxy.port == 8080 assert conn.proxy_headers == proxy_auth_header
def __init__(self, executable_path='MicrosoftWebDriver.exe', capabilities=None, port=DEFAULT_PORT, verbose=False, service_log_path=None, log_path=DEFAULT_SERVICE_LOG_PATH, service=None, options=None, keep_alive=False): """ Creates a new instance of the chrome driver. Starts the service and then creates new instance of chrome driver. :Args: - executable_path - path to the executable. If the default is used it assumes the executable is in the $PATH - capabilities - Dictionary object with non-browser specific capabilities only, such as "proxy" or "loggingPref". - port - port you would like the service to run, if left as 0, a free port will be found. - verbose - whether to set verbose logging in the service - service_log_path - Where to log information from the driver. - keep_alive - Whether to configure EdgeRemoteConnection to use HTTP keep-alive. """ if port != DEFAULT_PORT: warnings.warn('port has been deprecated, please pass in a Service object', DeprecationWarning, stacklevel=2) self.port = port if service_log_path != DEFAULT_SERVICE_LOG_PATH: warnings.warn('service_log_path has been deprecated, please pass in a Service object', DeprecationWarning, stacklevel=2) if capabilities is not None: warnings.warn('capabilities has been deprecated, please pass in a Service object', DeprecationWarning, stacklevel=2) if service_log_path != DEFAULT_SERVICE_LOG_PATH: warnings.warn('service_log_path has been deprecated, please pass in a Service object', DeprecationWarning, stacklevel=2) if verbose: warnings.warn('verbose has been deprecated, please pass in a Service object', DeprecationWarning, stacklevel=2) if service: self.edge_service = service else: self.edge_service = Service(executable_path, port=self.port, verbose=verbose, log_path=service_log_path) self.edge_service.start() if capabilities is None: capabilities = DesiredCapabilities.EDGE RemoteWebDriver.__init__( self, command_executor=RemoteConnection(self.service.service_url, resolve_ip=False, keep_alive=keep_alive), desired_capabilities=capabilities) self._is_remote = False
def setUp(self): self._command_executor = RemoteConnection(configs.kobitonServerUrl, resolve_ip=False) self._command_executor.set_timeout(configs.session_timeout) self.driver = webdriver.Remote(self._command_executor, configs.desired_caps_android_app) self.driver.implicitly_wait(configs.session_timeout) kobitonSessionId = self.driver.desired_capabilities.get( 'kobitonSessionId') print("https://portal.kobiton.com/sessions/%s" % (kobitonSessionId))
def browser(request): """Return a selenium driver to browse for testing """ try: from selenium import webdriver except ImportError: pytest.skip("selenium not available") from . import browser remote_url = request.config.getoption("--selenium-hub") if not remote_url: raise pytest.skip( "no hub specified to run the tests (use --selenium-hub)") ssdir = request.config.getoption("--screenshots-dir") if ssdir: request.node._screenshots_dir = ssdir driver_name = request.config.getoption("--selenium-driver") optmodule = "selenium.webdriver.%s.options" % driver_name.lower() try: optmodule = import_module(optmodule) except ImportError: raise pytest.fail("unknown selenium driver: %s" % driver_name) opt = optmodule.Options() # TODO: only tested with Chrome opt.add_argument("--window-size=1280,1024") # Sometimes we get an error with this request: try a few times r = None exc = None for i in range(3): try: r = webdriver.Remote(RemoteConnection(remote_url, resolve_ip=False), options=opt) break except Exception as e: exc = e if r is None: pytest.fail("error opening remote browser: %s" % exc) browser.on_pytest += 1 b = browser.Browser(r) request.node._browser = b yield b browser.on_pytest -= 1 b.quit()
def edge_driver(request: "SubRequest") -> Union[Remote, Edge]: """Fixture for receiving selenium controlled Edge instance""" if request.cls.test_type == "edge-local": driver = Edge() else: executor = RemoteConnection(SAUCE_HUB_URL, resolve_ip=False) driver = Remote(desired_capabilities=SAUCE_EDGE, command_executor=executor) set_selenium_driver_timeouts(driver) request.cls.driver = driver yield driver driver.close()
def test_basic_auth(mocker): def check(request, timeout): assert request.headers['Authorization'] == 'Basic dXNlcjpwYXNz' try: method = mocker.patch('urllib.request.OpenerDirector.open') except ImportError: method = mocker.patch('urllib2.OpenerDirector.open') method.side_effect = check with pytest.raises(AttributeError): RemoteConnection('http://*****:*****@remote', resolve_ip=False) \ .execute('status', {})
def start(self): if not self._username: raise KeyError("Cannot start session, Sauce Username is not set.") elif not self._access_key: raise KeyError( "Cannot start session, Sauce Access Key is not set.") executor = RemoteConnection(self.remote_url, resolve_ip=False) self.driver = webdriver.Remote( command_executor=executor, desired_capabilities=self.options.to_capabilities(), keep_alive=True) return self.driver
def _remote(self): log.debug(f'Remote driver url {self._config.driver}') connection = RemoteConnection(remote_server_addr=self._config.driver, resolve_ip=False) capabilities = { 'browserName': 'chrome', 'browserVersion': '94.0', 'moon:options': { 'enableVNC': True, 'enableVideo': False } } return webdriver.Remote(connection, desired_capabilities=capabilities)
def __init__(self, executable_path='MicrosoftWebDriver.exe', capabilities=None, port=0, verbose=False, service_log_path=None, log_path=None, keep_alive=False): """ Creates a new instance of the chrome driver. Starts the service and then creates new instance of chrome driver. :Args: - executable_path - path to the executable. If the default is used it assumes the executable is in the $PATH - capabilities - Dictionary object with non-browser specific capabilities only, such as "proxy" or "loggingPref". - port - port you would like the service to run, if left as 0, a free port will be found. - verbose - whether to set verbose logging in the service - service_log_path - Where to log information from the driver. - log_path: Deprecated argument for service_log_path - keep_alive - Whether to configure ChromeRemoteConnection to use HTTP keep-alive. """ if log_path: warnings.warn('use service_log_path instead of log_path', DeprecationWarning, stacklevel=2) service_log_path = log_path self.port = port if self.port == 0: self.port = utils.free_port() self.edge_service = Service(executable_path, port=self.port, verbose=verbose, log_path=service_log_path) self.edge_service.start() if capabilities is None: capabilities = DesiredCapabilities.EDGE RemoteWebDriver.__init__(self, command_executor=RemoteConnection( 'http://localhost:%d' % self.port, resolve_ip=False, keep_alive=keep_alive), desired_capabilities=capabilities) self._is_remote = False
def setUp(self): name = self.id().split('.') self.desired_capabilities['name'] = name[-3] + '.' + name[-1] if BaseTest.tunnel_identifier: self.desired_capabilities[ 'tunnel-identifier'] = BaseTest.tunnel_identifier if BaseTest.build_tag: self.desired_capabilities['build'] = BaseTest.build_tag #Generate complete remote connection string complete_connection_string = "%s%s:%s@%s:%s/wd/hub" % ( BaseTest.connection_protocol, BaseTest.username, BaseTest.access_key, BaseTest.selenium_host, BaseTest.selenium_port) #due to a bug in the python version of selenium, when using an encrypted endpoint we must disable IP resolution or else it fails with an SSL name mismatch error if self.connection_protocol == "https://": executor = RemoteConnection(complete_connection_string, resolve_ip=False) else: executor = RemoteConnection(complete_connection_string) self.driver = webdriver.Remote(executor, self.desired_capabilities) self.driver.implicitly_wait(60)
def init_lambdatest(self, test_name): # Lambda Test tool code username = readProp.get_property_value('LAMBDA_TEST', "username") access_key = readProp.get_property_value('LAMBDA_TEST', "access_key") selenium_endpoint = "http://{}:{}@hub.lambdatest.com/wd/hub".format( username, access_key) executor = RemoteConnection(selenium_endpoint, resolve_ip=False) driver = webdriver.Remote( command_executor=executor, desired_capabilities=self.desired_capabilities(test_name)) # Borwoserstack code # driver = webdriver.Remote( # command_executor='https://*****:*****@hub-cloud.browserstack.com/wd/hub', # desired_capabilities=self.desired_capabilities(test_name)) return driver
def _get_driver(capabilities): logger.debug('Creating web driver') start_time = datetime.now() # set resolve_ip to false to make it work in cases when remote driver is running in OpenShift command_executor = RemoteConnection(cfg.selenium.web_driver, resolve_ip=False) # increase the timeout because we are waiting for new pod to be created which takes some time command_executor.set_timeout(120) driver = webdriver.Remote(command_executor, desired_capabilities=capabilities) # reset the timeout to default, only driver creation is taking so much time command_executor.reset_timeout() _delta = datetime.now() - start_time logger.debug('Web driver created successfully. Time taken: {} ms'.format( int(_delta.total_seconds() * 1000))) return driver
def driver(request, browser_config): if browser_config["platform"] == "local": browser = webdriver.Chrome() yield browser browser.quit() else: # if the assignment below does not make sense to you please read up on object assignments. # The point is to make a copy and not mess with the original test spec. desired_caps = dict() desired_caps.update(browser_config) test_name = request.node.name build_tag = environ.get('BUILD_TAG', None) tunnel_id = environ.get('TUNNEL_IDENTIFIER', None) username = environ.get('SAUCE_USERNAME', None) access_key = environ.get('SAUCE_ACCESS_KEY', None) selenium_endpoint = "https://%s:%[email protected]:443/wd/hub" % (username, access_key) desired_caps['build'] = build_tag desired_caps['tunnelIdentifier'] = tunnel_id desired_caps['name'] = test_name executor = RemoteConnection(selenium_endpoint, resolve_ip=False) browser = webdriver.Remote( command_executor=executor, desired_capabilities=desired_caps ) # This is specifically for SauceLabs plugin. # In case test fails after selenium session creation having this here will help track it down. # creates one file per test non ideal but xdist is awful if browser is not None: with open("%s.testlog" % browser.session_id, 'w') as f: f.write("SauceOnDemandSessionID=%s job-name=%s\n" % (browser.session_id, test_name)) else: raise WebDriverException("Never created!") yield browser # Teardown starts here # report results try: browser.execute_script("sauce:job-result=%s" % str(not request.node.rep_call.failed).lower()) browser.quit() except WebDriverException: # we can ignore the exceptions of WebDriverException type -> We're done with tests. print('Warning: The driver failed to quit properly. Check test and server side logs.')
def chrome_webdriver(callisto_endpoint) -> WebDriver: capabilities = { "browserName": "chrome", "goog:chromeOptions": { "args": ["--no-sandbox"] # chrome crashes in kind without this option } } chrome_webdriver = webdriver.Remote( command_executor=RemoteConnection(callisto_endpoint, resolve_ip=False), desired_capabilities=capabilities, ) yield chrome_webdriver chrome_webdriver.quit()