Class: Appium::Core::Driver

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, opts = {}) ⇒ Driver

Returns a new instance of Driver.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/appium_lib_core/driver.rb', line 99

def initialize(target, opts = {})
  opts = Appium.symbolize_keys opts
  validate_keys(opts)

  @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, target: target)

  self # rubocop:disable Lint/Void
end

Instance Attribute Details

#automation_nameHash (readonly)

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

Returns:

  • (Hash)


19
20
21
# File 'lib/appium_lib_core/driver.rb', line 19

def automation_name
  @automation_name
end

#capsCore::Base::Capabilities (readonly)

Selenium webdriver capabilities



6
7
8
# File 'lib/appium_lib_core/driver.rb', line 6

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. False is by default and then “127.0.0.1:##port/wd/hub” is used.

Returns:

  • (String)


24
25
26
# File 'lib/appium_lib_core/driver.rb', line 24

def custom_url
  @custom_url
end

#default_waitInteger (readonly)

Default wait time for elements to appear Returns the default client side wait. 20 seconds is by default. Provide Appium::Drive like { appium_lib: { wait: 30 } }

Returns:

  • (Integer)


36
37
38
# File 'lib/appium_lib_core/driver.rb', line 36

def default_wait
  @default_wait
end

#deviceSymbol (readonly)

Device type to request from the appium server

Returns:

  • (Symbol)

    :android and :ios, for example



14
15
16
# File 'lib/appium_lib_core/driver.rb', line 14

def device
  @device
end

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



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

def driver
  @driver
end

#export_sessionBoolean (readonly)

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

Returns:

  • (Boolean)


28
29
30
# File 'lib/appium_lib_core/driver.rb', line 28

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’



30
31
32
# File 'lib/appium_lib_core/driver.rb', line 30

def export_session_path
  @export_session_path
end

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

Return http client called in start_driver()

Returns:



10
11
12
# File 'lib/appium_lib_core/driver.rb', line 10

def http_client
  @http_client
end

#listenerObject (readonly)

instance of AbstractEventListener for logging support Nil by default



57
58
59
# File 'lib/appium_lib_core/driver.rb', line 57

def listener
  @listener
end

#portInteger (readonly)

Appium’s server port. 4723 is by default. Provide Appium::Drive like { appium_lib: { port: 8080 } }

Returns:

  • (Integer)


41
42
43
# File 'lib/appium_lib_core/driver.rb', line 41

def port
  @port
end

#wait_intervalInteger (readonly)

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

Returns:

  • (Integer)


53
54
55
# File 'lib/appium_lib_core/driver.rb', line 53

def wait_interval
  @wait_interval
end

#wait_timeoutInteger (readonly)

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

Returns:

  • (Integer)


47
48
49
# File 'lib/appium_lib_core/driver.rb', line 47

def wait_timeout
  @wait_timeout
end

Class Method Details

.for(target, opts = {}) ⇒ Driver

Creates a new global driver and extend particular methods to ‘target`

Examples:


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: {
           server_url: "http://custom-host:8080/wd/hub.com",
           export_session: false,
           port: 8080,
           wait: 0,
           wait_timeout: 20,
           wait_interval: 0.3,
           listener: nil,
         }
       }
@core = Appium::Core.for(self, opts) # create a core driver with `opts` and extend methods into `self`
@core.start_driver(server_url: server_url) # start driver

Parameters:

  • target (Class)

    Extend particular methods to this target.

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

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

Returns:



94
95
96
# File 'lib/appium_lib_core/driver.rb', line 94

def self.for(target, opts = {})
  new(target, 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" => "0.18.1",
          "revision" => "d242ebcfd92046a974347ccc3a28f0e898595198"
      }
  }

@core.appium_server_version #=> {}

Returns:

  • (Hash)


245
246
247
248
249
250
251
# File 'lib/appium_lib_core/driver.rb', line 245

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


260
261
262
263
# File 'lib/appium_lib_core/driver.rb', line 260

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

#quit_drivervoid

This method returns an undefined value.

Quits the driver

Examples:


@core.quit_driver


220
221
222
223
224
# File 'lib/appium_lib_core/driver.rb', line 220

def quit_driver
  @driver.quit
rescue # rubocop:disable Lint/RescueWithoutErrorClass
  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)


275
276
277
278
# File 'lib/appium_lib_core/driver.rb', line 275

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(self, opts) # create a core driver with `opts` and extend methods into `self`
@driver = @core.start_driver

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



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/appium_lib_core/driver.rb', line 152

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

  # open_timeout and read_timeout are explicit wait.
  open_timeout = http_client_ops.delete(:open_timeout)
  read_timeout = http_client_ops.delete(:read_timeout)

  http_client = http_client_ops.delete(:http_client)
  @http_client ||= http_client ? http_client : Appium::Core::Base::Http::Default.new

  @http_client.open_timeout = open_timeout if open_timeout
  @http_client.read_timeout = read_timeout if 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: server_url,
                                               listener: @listener)

    # 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 #{server_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

#wait(opts = {}) ⇒ Object

Check every interval seconds to see if yield doesn’t raise an exception. Give up after timeout seconds.

Wait code from the selenium Ruby gem github.com/SeleniumHQ/selenium/blob/cf501dda3f0ed12233de51ce8170c0e8090f0c20/rb/lib/selenium/webdriver/common/wait.rb

If only a number is provided then it’s treated as the timeout value.

Examples:


@core.wait { @driver.find_element :accessibility_id, 'something' }

Parameters:

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

    Options

Options Hash (opts):

  • :timeout (Numeric)

    Seconds to wait before timing out. Set default by ‘appium_wait_timeout` (30).

  • :interval (Numeric)

    Seconds to sleep between polls. Set default by ‘appium_wait_interval` (0.5).

  • :message (String)

    Exception message if timed out.

  • :ignore (Array, Exception)

    Exceptions to ignore while polling (default: Exception)



328
329
330
331
332
333
334
335
336
# File 'lib/appium_lib_core/driver.rb', line 328

def wait(opts = {})
  opts = process_wait_opts(opts).merge(return_if_true: false)

  opts[:timeout] ||= @wait_timeout
  opts[:interval] ||= @wait_interval

  wait = ::Appium::Core::Base::Wait.new opts
  wait.until { yield }
end

#wait_true(opts = {}) ⇒ Object

Check every interval seconds to see if yield returns a truthy value. Note this isn’t a strict boolean true, any truthy value is accepted. false and nil are considered failures. Give up after timeout seconds.

Wait code from the selenium Ruby gem github.com/SeleniumHQ/selenium/blob/cf501dda3f0ed12233de51ce8170c0e8090f0c20/rb/lib/selenium/webdriver/common/wait.rb

If only a number is provided then it’s treated as the timeout value.

Examples:


@core.wait_true { @driver.find_element :accessibility_id, 'something' }

Parameters:

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

    Options

Options Hash (opts):

  • :timeout (Numeric)

    Seconds to wait before timing out. Set default by ‘appium_wait_timeout` (30).

  • :interval (Numeric)

    Seconds to sleep between polls. Set default by ‘appium_wait_interval` (0.5).

  • :message (String)

    Exception message if timed out.

  • :ignore (Array, Exception)

    Exceptions to ignore while polling (default: Exception)



300
301
302
303
304
305
306
307
308
# File 'lib/appium_lib_core/driver.rb', line 300

def wait_true(opts = {})
  opts = process_wait_opts(opts).merge(return_if_true: true)

  opts[:timeout]  ||= @wait_timeout
  opts[:interval] ||= @wait_interval

  wait = ::Appium::Core::Base::Wait.new opts
  wait.until { yield }
end