@@ -54,17 +54,18 @@ def element_class
5454 # Initializes the bridge with the given server URL
5555 # @param [String, URI] url url for the remote server
5656 # @param [Object] http_client an HTTP client instance that implements the same protocol as Http::Default
57+ # @param [ClientConfig] client_config configuration for the HTTP client
5758 # @api private
5859 #
5960
60- def initialize ( url :, http_client : nil )
61- uri = url . is_a? ( URI ) ? url : URI . parse ( url )
62- uri . path += '/' unless uri . path . end_with? ( '/' )
61+ def initialize ( url : nil , http_client : nil , client_config : nil )
62+ if http_client && client_config
63+ raise Error ::WebDriverError , 'Cannot specify both http_client and client_config'
64+ end
6365
64- @http = http_client || Http ::Default . new
65- @http . server_url = uri
66- @file_detector = nil
66+ @http = http_client || create_http_client ( client_config , url : url )
6767
68+ @file_detector = nil
6869 @locator_converter = self . class . locator_converter
6970 end
7071
@@ -93,6 +94,8 @@ def create_session(capabilities)
9394 extend ( WebDriver ::Safari ::Features )
9495 when 'internet explorer'
9596 extend ( WebDriver ::IE ::Features )
97+ else
98+ raise Error ::WebDriverError , "Unknown browser name: #{ @capabilities [ :browser_name ] } "
9699 end
97100 end
98101
@@ -662,6 +665,41 @@ def prepare_capabilities_payload(capabilities)
662665 capabilities = { alwaysMatch : capabilities } if !capabilities [ 'alwaysMatch' ] && !capabilities [ 'firstMatch' ]
663666 { capabilities : capabilities }
664667 end
668+
669+ def create_http_client ( client_config , url : nil )
670+ http = build_http_client ( client_config )
671+ validate_server_url_args ( url , client_config )
672+ http . server_url = normalize_url ( url || client_config &.server_url )
673+ http
674+ end
675+
676+ def build_http_client ( client_config )
677+ if client_config
678+ http = Http ::Default . new ( open_timeout : client_config . open_timeout ,
679+ read_timeout : client_config . read_timeout )
680+ http . proxy = client_config . proxy if client_config . proxy
681+
682+ Http ::Common . extra_headers = client_config . extra_headers if client_config . extra_headers
683+ Http ::Common . user_agent = client_config . user_agent if client_config . user_agent
684+ else
685+ http = Http ::Default . new
686+ end
687+ http
688+ end
689+
690+ def validate_server_url_args ( url , client_config )
691+ if url && client_config &.server_url
692+ raise Error ::WebDriverError , 'Cannot specify url in both keyword and http_client'
693+ elsif url . nil? && !client_config &.server_url
694+ raise Error ::WebDriverError , 'No server URL provided'
695+ end
696+ end
697+
698+ def normalize_url ( url )
699+ url = URI . parse ( url ) if url && !url . is_a? ( URI )
700+ url &.path += '/' unless url &.path &.end_with? ( '/' )
701+ url
702+ end
665703 end # Bridge
666704 end # Remote
667705 end # WebDriver
0 commit comments