Method: Appium::Core::Driver#start_driver

Defined in:
lib/appium_lib_core/driver.rb

#start_driver(server_url: nil, http_client_ops: { http_client: nil, open_timeout: 999_999, read_timeout: 999_999 }) ⇒ Selenium::WebDriver

Creates a new global driver and quits the old one if it exists. You can customise http_client as the following

Examples:


require 'rubygems'
require 'appium_lib_core'

# platformName takes a string or a symbol.

# Start iOS driver
opts = {
         capabilities: {
           platformName: :ios,
           platformVersion: '11.0',
           deviceName: 'iPhone Simulator',
           automationName: 'XCUITest',
           app: '/path/to/MyiOS.app'
         },
         appium_lib: {
           wait: 20,
           wait_timeout: 20,
           wait_interval: 0.3,
         }
       }

@core = Appium::Core.for(opts) # create a core driver with 'opts' and extend methods into 'self'
@driver = @core.start_driver server_url: "http://127.0.0.1:8000"

# Attach custom HTTP client
@driver = @core.start_driver server_url: "http://127.0.0.1:8000",
                             http_client_ops: { http_client: Your:Http:Client.new,
                                                open_timeout: 1_000,
                                                read_timeout: 1_000 }

Parameters:

  • server_url (String) (defaults to: nil)

    Custom server url to send to requests. Default is “127.0.0.1:4723”.

  • http_client_ops (Hash) (defaults to: { http_client: nil, open_timeout: 999_999, read_timeout: 999_999 })

    Options for http client

Options Hash (http_client_ops:):

  • :http_client (Hash)

    Custom HTTP Client

  • :open_timeout (Hash)

    Custom open timeout for http client.

  • :read_timeout (Hash)

    Custom read timeout for http client.

Returns:

  • (Selenium::WebDriver)

    A new driver instance


395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/appium_lib_core/driver.rb', line 395

def start_driver(server_url: nil,
                 http_client_ops: { http_client: nil, open_timeout: 999_999, read_timeout: 999_999 })
  @custom_url ||= "http://127.0.0.1:#{@port}"
  @custom_url = server_url unless server_url.nil?

  @http_client = get_http_client http_client: http_client_ops.delete(:http_client),
                                 open_timeout: http_client_ops.delete(:open_timeout),
                                 read_timeout: http_client_ops.delete(:read_timeout)

  if @enable_idempotency_header
    if @http_client.instance_variable_defined? :@additional_headers
      @http_client.set_additional_header Appium::Core::Base::Http::RequestHeaders::KEYS[:idempotency], SecureRandom.uuid
    else
      ::Appium::Logger.warn 'No additional_headers attribute in this http client instance'
    end
  end

  begin
    @driver = ::Appium::Core::Base::Driver.new(listener: @listener,
                                               http_client: @http_client,
                                               capabilities: @caps, # ::Appium::Core::Base::Capabilities
                                               url: @custom_url,
                                               wait_timeout: @wait_timeout,
                                               wait_interval: @wait_interval)

    if @direct_connect
      d_c = DirectConnections.new(@driver.capabilities)
      @driver.update_sending_request_to(protocol: d_c.protocol, host: d_c.host, port: d_c.port, path: d_c.path)
    end
  rescue Errno::ECONNREFUSED
    raise "ERROR: Unable to connect to Appium. Is the server running on #{@custom_url}?"
  end

  if @http_client.instance_variable_defined? :@additional_headers
    # We only need the key for a new session request. Should remove it for other following commands.
    @http_client.delete_additional_header Appium::Core::Base::Http::RequestHeaders::KEYS[:idempotency]
  end

  set_implicit_wait_by_default(@default_wait)

  @driver
end