Class: Appium::Core::Driver

Inherits:
Object
  • Object
show all
Includes:
Waitable
Defined in:
lib/appium_lib_core/driver.rb

Constant Summary collapse

DEFAULT_IMPLICIT_WAIT =
0
DEFAULT_APPIUM_PORT =
4723

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Waitable

#wait, #wait_true

Constructor Details

#initialize(opts = {}) ⇒ Driver

Returns a new instance of Driver.



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/appium_lib_core/driver.rb', line 232

def initialize(opts = {})
  @delegate_target = self # for testing purpose
  @automation_name = nil # initialise before `set_automation_name`

  opts = Appium.symbolize_keys opts
  validate_keys(opts)

  @custom_url = opts.delete :url
  @caps = get_caps(opts)

  set_appium_lib_specific_values(get_appium_lib_opts(opts))
  set_app_path
  set_appium_device
  set_automation_name

  extend_for(device: @device, automation_name: @automation_name)

  self # rubocop:disable Lint/Void
end

Instance Attribute Details

#automation_nameHash (readonly)

Automation name sent to appium server or received by server.
If automation_name is nil, it is not set both client side and server side.

Returns:

  • (Hash)


68
69
70
# File 'lib/appium_lib_core/driver.rb', line 68

def automation_name
  @automation_name
end

#capsCore::Base::Capabilities (readonly)

Selenium webdriver capabilities



55
56
57
# File 'lib/appium_lib_core/driver.rb', line 55

def caps
  @caps
end

#custom_urlString (readonly)

Custom URL for the selenium server. If set this attribute, ruby_lib_core try to handshake to the custom url.
Defaults to false. Then try to connect to <code>127.0.0.1:##port/wd/hub<code>.

Returns:

  • (String)


73
74
75
# File 'lib/appium_lib_core/driver.rb', line 73

def custom_url
  @custom_url
end

#default_waitInteger (readonly)

Default wait time for elements to appear in Appium server side. Defaults to DEFAULT_IMPLICIT_WAIT.
Provide { appium_lib: { wait: 30 } } to Appium::Core.for

Returns:

  • (Integer)


85
86
87
# File 'lib/appium_lib_core/driver.rb', line 85

def default_wait
  @default_wait
end

#deviceSymbol (readonly)

Device type to request from the appium server

Returns:

  • (Symbol)

    :android and :ios, for example



63
64
65
# File 'lib/appium_lib_core/driver.rb', line 63

def device
  @device
end

#direct_connectBool (readonly)

[Experimental feature]
Enable an experimental feature updating Http client endpoint following below keys by Appium/Selenium server.
This works with Base::Http::Default.

If your Selenium/Appium server decorates the new session capabilities response with the following keys:

  • directConnectProtocol

  • directConnectHost

  • directConnectPort

  • directConnectPath

Ignore them if this parameter is false. Defaults to false.

Returns:

  • (Bool)


127
128
129
# File 'lib/appium_lib_core/driver.rb', line 127

def direct_connect
  @direct_connect
end

#driverAppium::Core::Base::Driver (readonly)



112
113
114
# File 'lib/appium_lib_core/driver.rb', line 112

def driver
  @driver
end

#export_sessionBoolean (readonly)

Export session id to textfile in /tmp for 3rd party tools. False bu default.

Returns:

  • (Boolean)


77
78
79
# File 'lib/appium_lib_core/driver.rb', line 77

def export_session
  @export_session
end

#export_session_pathString (readonly)

Returns By default, session id is exported in ‘/tmp/appium_lib_session’.

Returns:

  • (String)

    By default, session id is exported in ‘/tmp/appium_lib_session’



79
80
81
# File 'lib/appium_lib_core/driver.rb', line 79

def export_session_path
  @export_session_path
end

#http_clientAppium::Core::Base::Http::Default (readonly)

Return http client called in start_driver()

Returns:



59
60
61
# File 'lib/appium_lib_core/driver.rb', line 59

def http_client
  @http_client
end

#listenerObject (readonly)

instance of AbstractEventListener for logging support Nil by default



109
110
111
# File 'lib/appium_lib_core/driver.rb', line 109

def listener
  @listener
end

#portInteger (readonly)

Appium’s server port. 4723 is by default. Defaults to DEFAULT_APPIUM_PORT.
Provide { appium_lib: { port: 8080 } } to Appium::Core.for. :custom_url is prior than :port if :custom_url is set.

Returns:

  • (Integer)


92
93
94
# File 'lib/appium_lib_core/driver.rb', line 92

def port
  @port
end

#wait_intervalInteger (readonly)

Return a time to wait interval. 0.5 seconds is by default Wait::DEFAULT_INTERVAL.
Wait interval time for Base::Wait, wait and wait_true.
Provide { appium_lib: { wait_interval: 0.1 } } to Appium::Core.for.

Returns:

  • (Integer)


105
106
107
# File 'lib/appium_lib_core/driver.rb', line 105

def wait_interval
  @wait_interval
end

#wait_timeoutInteger (readonly)

Return a time wait timeout. 30 seconds is by default Wait::DEFAULT_TIMEOUT.
Wait time for Base::Wait, wait and wait_true.
Provide { appium_lib: { wait_timeout: 20 } } to Appium::Core.for.

Returns:

  • (Integer)


99
100
101
# File 'lib/appium_lib_core/driver.rb', line 99

def wait_timeout
  @wait_timeout
end

Class Method Details

.for(opts = {}) ⇒ Driver

Creates a new driver and extend particular methods

Examples:


# format 1
@core = Appium::Core.for caps: {...}, appium_lib: {...}
# format 2. `desired_capabilities:` is also available instead of `caps:`. Either is fine.
@core = Appium::Core.for url: "http://127.0.0.1:8080/wd/hub", desired_capabilities: {...}, appium_lib: {...}
# format 3. `appium_lib: {...}` can be blank
@core = Appium::Core.for url: "http://127.0.0.1:8080/wd/hub", desired_capabilities: {...}

require 'rubygems'
require 'appium_lib_core'

# Start iOS driver
opts = {
         caps: {
           platformName: :ios,
           platformVersion: '11.0',
           deviceName: 'iPhone Simulator',
           automationName: 'XCUITest',
           app: '/path/to/MyiOS.app'
         },
         appium_lib: {
           export_session: false,
           port: 8080,
           wait: 0,
           wait_timeout: 20,
           wait_interval: 0.3,
           listener: nil,
         }
       }
@core = Appium::Core.for(opts) # create a core driver with `opts` and extend methods into `self`
@core.start_driver # Connect to `http://127.0.0.1:8080/wd/hub` because of `port: 8080`

# Start iOS driver with .zip file over HTTP
#  `desired_capabilities:` is also available instead of `caps:`. Either is fine.
opts = {
         desired_capabilities: {
           platformName: :ios,
           platformVersion: '11.0',
           deviceName: 'iPhone Simulator',
           automationName: 'XCUITest',
           app: 'http://example.com/path/to/MyiOS.app.zip'
         },
         appium_lib: {
           server_url: 'http://custom-host:8080/wd/hub.com',
           export_session: false,
           wait: 0,
           wait_timeout: 20,
           wait_interval: 0.3,
           listener: nil,
         }
       }
@core = Appium::Core.for(opts)
@core.start_driver # Connect to `http://custom-host:8080/wd/hub.com`

# Start iOS driver as another format. `url` is available like below
opts = {
         url: "http://custom-host:8080/wd/hub.com",
         desired_capabilities: {
           platformName: :ios,
           platformVersion: '11.0',
           deviceName: 'iPhone Simulator',
           automationName: 'XCUITest',
           app: '/path/to/MyiOS.app'
         },
         appium_lib: {
           export_session: false,
           wait: 0,
           wait_timeout: 20,
           wait_interval: 0.3,
           listener: nil,
         }
       }
@core = Appium::Core.for(opts) # create a core driver with `opts` and extend methods into `self`
@core.start_driver # start driver with `url`. Connect to `http://custom-host:8080/wd/hub.com`

Parameters:

  • opts (Hash) (defaults to: {})

    A options include capabilities for the Appium Server and for the client.

Options Hash (opts):

  • :caps (Hash)

    Appium capabilities. Prior than :desired_capabilities

  • :desired_capabilities (Hash)

    The same as :caps. This param is for compatibility with Selenium WebDriver format

  • :appium_lib (Appium::Core::Options)

    Capabilities affect only ruby client

  • :url (String)

    The same as :custom_url in :appium_lib. This param is for compatibility with Selenium WebDriver format

Returns:



217
218
219
# File 'lib/appium_lib_core/driver.rb', line 217

def self.for(opts = {})
  new(opts)
end

Instance Method Details

#appium_server_versionHash

Returns the server’s version info Returns blank hash for Selenium Grid since ‘remote_status` gets 500 error

Examples:


@core.appium_server_version
  {
      "build" => {
          "version" => "1.9.2",
          "git-sha" => "fd7c7fd19d3896719616cd970229d73e63b5271a",
          "built" => "2018-11-08 12:24:02 +0900"
      }
  }

@core.appium_server_version #=> {}

Returns:

  • (Hash)


383
384
385
386
387
388
389
390
# File 'lib/appium_lib_core/driver.rb', line 383

def appium_server_version
  @driver.remote_status
rescue Selenium::WebDriver::Error::ServerError => e
  raise ::Appium::Core::Error::ServerError unless e.message.include?('status code 500')

  # driver.remote_status returns 500 error for using selenium grid
  {}
end

#platform_versionArray<Integer>

Return the platform version as an array of integers

Examples:


@core.platform_version #=> [10,1,1]

Returns:

  • (Array<Integer>)


399
400
401
402
# File 'lib/appium_lib_core/driver.rb', line 399

def platform_version
  p_version = @driver.capabilities['platformVersion'] || @driver.session_capabilities['platformVersion']
  p_version.split('.').map(&:to_i)
end

#quit_drivervoid

This method returns an undefined value.

Quits the driver

Examples:


@core.quit_driver


357
358
359
360
361
# File 'lib/appium_lib_core/driver.rb', line 357

def quit_driver
  @driver.quit
rescue # rubocop:disable Style/RescueStandardError
  nil
end

#screenshot(png_save_path) ⇒ File

Takes a png screenshot and saves to the target path.

Examples:


@core.screenshot '/tmp/hi.png' #=> nil
# same as `@driver.save_screenshot png_save_path`

Parameters:

  • png_save_path (String)

    the full path to save the png

Returns:

  • (File)


414
415
416
417
# File 'lib/appium_lib_core/driver.rb', line 414

def screenshot(png_save_path)
  warn '[DEPRECATION] screenshot will be removed. Please use driver.save_screenshot instead.'
  @driver.save_screenshot png_save_path
end

#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 = {
         caps: {
           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/wd/hub"

Parameters:

  • server_url (String) (defaults to: nil)

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

  • 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)

    the new global driver



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/appium_lib_core/driver.rb', line 289

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

  create_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)

  begin
    # included https://github.com/SeleniumHQ/selenium/blob/43f8b3f66e7e01124eff6a5805269ee441f65707/rb/lib/selenium/webdriver/remote/driver.rb#L29
    @driver = ::Appium::Core::Base::Driver.new(http_client: @http_client,
                                               desired_capabilities: @caps,
                                               url: @custom_url,
                                               listener: @listener)

    if @direct_connect
      @driver.update_sending_request_to(protocol: @driver.capabilities['directConnectProtocol'],
                                        host: @driver.capabilities['directConnectHost'],
                                        port: @driver.capabilities['directConnectPort'],
                                        path: @driver.capabilities['directConnectPath'])
    end

    # export session
    write_session_id(@driver.session_id, @export_session_path) if @export_session
  rescue Errno::ECONNREFUSED
    raise "ERROR: Unable to connect to Appium. Is the server running on #{@custom_url}?"
  end

  # If "automationName" is set only server side, this method set "automationName" attribute into @automation_name.
  # Since @automation_name is set only client side before start_driver is called.
  set_automation_name_if_nil

  set_implicit_wait_by_default(@default_wait)

  @driver
end