Method: Appium::Driver#initialize

Defined in:
lib/appium_lib/driver.rb

#initialize(opts = {}, global_driver = false) ⇒ Driver

Creates a new driver. The driver is defined as global scope by default. We can avoid defining global driver.

Examples:


require 'rubygems'
require 'appium_lib'

# platformName takes a string or a symbol.
# Start iOS driver with global scope
opts = {
         caps: {
           platformName: :ios,
           app: '/path/to/MyiOS.app'
         },
         appium_lib: {
           server_url: 'http://127.0.0.1:4723'
           wait_timeout: 30
         }
       }
appium_driver = Appium::Driver.new(opts, true)
appium_driver.start_driver

# Start Android driver with global scope
opts = {
         caps: {
           platformName: :android,
           app: '/path/to/my.apk'
         },
         appium_lib: {
           wait_timeout: 30,
           wait_interval: 1
         }
       }
appium_driver = Appium::Driver.new(opts, true)
appium_driver.start_driver

# Start iOS driver without global scope
opts = {
         caps: {
           platformName: :ios,
           app: '/path/to/MyiOS.app'
         },
         appium_lib: {
           wait_timeout: 30
         }
       }
appium_driver = Appium::Driver.new(opts, false)
appium_driver.start_driver

# Start iOS driver without global scope
opts = {
         caps: {
           platformName: :ios,
           app: '/path/to/MyiOS.app'
         },
         appium_lib: {
           wait_timeout: 30
         },
         global_driver: false
       }
appium_driver = Appium::Driver.new(opts)
appium_driver.start_driver

Parameters:

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

    A hash containing various options.

  • global_driver (Bool) (defaults to: false)

    A bool require global driver before initialize.

Raises:



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/appium_lib/driver.rb', line 173

def initialize(opts = {}, global_driver = false)
  # Capybara can't put `global_driver` as the 2nd argument.
  global_driver = opts.delete :global_driver if global_driver.nil?

  $driver&.driver_quit if global_driver

  raise ArgumentError, 'opts must be a hash' unless opts.is_a? Hash

  @core = ::Appium::Core.for(opts)
  extend ::Appium::Core::Device

  opts = Appium.symbolize_keys opts
  appium_lib_opts = opts[:appium_lib] || {}

  @caps = @core.caps
  @custom_url = @core.custom_url
  @default_wait = @core.default_wait || 0
  @appium_port = @core.port
  @appium_wait_timeout = @core.wait_timeout
  @appium_wait_interval = @core.wait_interval
  @listener = @core.listener
  @appium_device = @core.device
  @automation_name = @core.automation_name

  # Arrange the app capability. This must be after @core = ::Appium::Core.for(opts)
  set_app_path(opts)

  # enable debug patch
  @appium_debug = appium_lib_opts.fetch :debug, !!defined?(Pry) # rubocop:disable Style/DoubleNegation
  set_sauce_related_values(appium_lib_opts)

  # Extend Common methods
  extend Appium::Common
  extend Appium::Device

  # Extend each driver's methods
  extend_for(device: @core.device, automation_name: @core.automation_name)

  # for command

  if @appium_debug
    Appium::Logger.debug opts unless opts.empty?
    Appium::Logger.debug "Debug is: #{@appium_debug}"
    Appium::Logger.debug "Device is: #{@core.device}"
  end

  # Save global reference to last created Appium driver for top level methods.
  $driver = self if global_driver

  self # rubocop:disable Lint/Void # return newly created driver
end