Class: Appium::Driver
Instance Attribute Summary collapse
-
#appium_debug ⇒ Object
Boolean debug mode for the Appium Ruby bindings.
-
#appium_device ⇒ Object
Device type to request from the appium server.
-
#appium_port ⇒ Object
Appium’s server port.
-
#caps ⇒ Object
Selenium webdriver capabilities.
-
#custom_url ⇒ Object
Custom URL for the selenium server.
-
#default_wait ⇒ Integer
Default wait time for elements to appear Returns the default client side wait.
-
#driver ⇒ Driver
readonly
Returns the driver.
-
#export_session ⇒ Object
Export session id to textfile in /tmp for 3rd party tools.
-
#global_webdriver_http_sleep ⇒ Object
The amount to sleep in seconds before every webdriver http call.
-
#last_waits ⇒ Object
Array of previous wait time values.
-
#sauce_access_key ⇒ Object
Access Key for use on Sauce Labs.
-
#sauce_username ⇒ Object
Username for use on Sauce Labs.
Class Method Summary collapse
-
.absolute_app_path(opts) ⇒ String
Converts app_path to an absolute path.
Instance Method Summary collapse
-
#appium_server_version ⇒ Hash
Returns the server’s version info.
- #device_is_android? ⇒ Boolean
-
#driver_attributes ⇒ Object
Returns a hash of the driver attributes.
-
#driver_quit ⇒ void
Quits the driver.
-
#execute_script(script, *args) ⇒ Object
The same as @driver.execute_script.
-
#exists(pre_check = 0, post_check = @default_wait, &search_block) ⇒ Boolean
Returns existence of element.
-
#find_element(*args) ⇒ Element
Calls @driver.find_elements.
-
#find_elements(*args) ⇒ Array<Element>
Calls @driver.find_elements.
-
#initialize(opts = {}) ⇒ Driver
constructor
Creates a new driver.
-
#no_wait ⇒ Object
Set implicit wait and default_wait to zero.
-
#restart ⇒ Driver
Restarts the driver.
-
#screenshot(png_save_path) ⇒ nil
Takes a png screenshot and saves to the target path.
-
#server_url ⇒ String
Get the server url.
-
#set_location(opts = {}) ⇒ Selenium::WebDriver::Location
Calls @driver.set_location.
-
#set_wait(timeout = nil) ⇒ void
Set implicit wait and default_wait to timeout, defaults to 30.
-
#start_driver ⇒ Selenium::WebDriver
Creates a new global driver and quits the old one if it exists.
-
#x ⇒ void
Quit the driver and Pry.
Constructor Details
#initialize(opts = {}) ⇒ Driver
Creates a new driver
“‘ruby require ’rubygems’ require ‘appium_lib’
# platformName takes a string or a symbol.
# Start iOS driver opts = { caps: { platformName: :ios, app: ‘/path/to/MyiOS.app’ } } Appium::Driver.new(opts).start_driver
# Start Android driver opts = { caps: { platformName: :android, app: ‘/path/to/my.apk’ } } Appium::Driver.new(opts).start_driver “‘
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 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/appium_lib/driver.rb', line 290 def initialize(opts = {}) # quit last driver $driver.driver_quit if $driver fail 'opts must be a hash' unless opts.is_a? Hash opts = Appium.symbolize_keys opts # default to {} to prevent nil.fetch and other nil errors @caps = opts[:caps] || {} appium_lib_opts = opts[:appium_lib] || {} # appium_lib specific values @custom_url = appium_lib_opts.fetch :server_url, false @export_session = appium_lib_opts.fetch :export_session, false @default_wait = appium_lib_opts.fetch :wait, 0 @last_waits = [@default_wait] @sauce_username = appium_lib_opts.fetch :sauce_username, ENV['SAUCE_USERNAME'] @sauce_username = nil if !@sauce_username || (@sauce_username.is_a?(String) && @sauce_username.empty?) @sauce_access_key = appium_lib_opts.fetch :sauce_access_key, ENV['SAUCE_ACCESS_KEY'] @sauce_access_key = nil if !@sauce_access_key || (@sauce_access_key.is_a?(String) && @sauce_access_key.empty?) @appium_port = appium_lib_opts.fetch :port, 4723 # Path to the .apk, .app or .app.zip. # The path can be local or remote for Sauce. if @caps && @caps[:app] && ! @caps[:app].empty? @caps[:app] = self.class.absolute_app_path opts end # https://code.google.com/p/selenium/source/browse/spec-draft.md?repo=mobile @appium_device = @caps[:platformName] @appium_device = @appium_device.is_a?(Symbol) ? @appium_device : @appium_device.downcase.strip.intern if @appium_device # load common methods extend Appium::Common extend Appium::Device if device_is_android? # load Android specific methods extend Appium::Android else # load iOS specific methods extend Appium::Ios end # apply os specific patches patch_webdriver_element # enable debug patch # !!'constant' == true @appium_debug = appium_lib_opts.fetch :debug, !!defined?(Pry) if @appium_debug Appium::Logger.ap_debug opts unless opts.empty? Appium::Logger.debug "Debug is: #{@appium_debug}" Appium::Logger.debug "Device is: #{@appium_device}" patch_webdriver_bridge end # Save global reference to last created Appium driver for top level methods. $driver = self self # return newly created driver end |
Instance Attribute Details
#appium_debug ⇒ Object
Boolean debug mode for the Appium Ruby bindings
265 266 267 |
# File 'lib/appium_lib/driver.rb', line 265 def appium_debug @appium_debug end |
#appium_device ⇒ Object
Device type to request from the appium server
263 264 265 |
# File 'lib/appium_lib/driver.rb', line 263 def appium_device @appium_device end |
#appium_port ⇒ Object
Appium’s server port
261 262 263 |
# File 'lib/appium_lib/driver.rb', line 261 def appium_port @appium_port end |
#caps ⇒ Object
Selenium webdriver capabilities
244 245 246 |
# File 'lib/appium_lib/driver.rb', line 244 def caps @caps end |
#custom_url ⇒ Object
Custom URL for the selenium server
246 247 248 |
# File 'lib/appium_lib/driver.rb', line 246 def custom_url @custom_url end |
#default_wait ⇒ Integer
Default wait time for elements to appear Returns the default client side wait. This value is independent of what the server is using
253 254 255 |
# File 'lib/appium_lib/driver.rb', line 253 def default_wait @default_wait end |
#driver ⇒ Driver (readonly)
Returns the driver
269 270 271 |
# File 'lib/appium_lib/driver.rb', line 269 def driver @driver end |
#export_session ⇒ Object
Export session id to textfile in /tmp for 3rd party tools
248 249 250 |
# File 'lib/appium_lib/driver.rb', line 248 def export_session @export_session end |
#global_webdriver_http_sleep ⇒ Object
The amount to sleep in seconds before every webdriver http call.
242 243 244 |
# File 'lib/appium_lib/driver.rb', line 242 def global_webdriver_http_sleep @global_webdriver_http_sleep end |
#last_waits ⇒ Object
Array of previous wait time values
255 256 257 |
# File 'lib/appium_lib/driver.rb', line 255 def last_waits @last_waits end |
#sauce_access_key ⇒ Object
Access Key for use on Sauce Labs
259 260 261 |
# File 'lib/appium_lib/driver.rb', line 259 def sauce_access_key @sauce_access_key end |
#sauce_username ⇒ Object
Username for use on Sauce Labs
257 258 259 |
# File 'lib/appium_lib/driver.rb', line 257 def sauce_username @sauce_username end |
Class Method Details
.absolute_app_path(opts) ⇒ String
Converts app_path to an absolute path.
opts is the full options hash (caps and appium_lib). If server_url is set then the app path is used as is.
if app isn’t set then an error is raised.
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 |
# File 'lib/appium_lib/driver.rb', line 402 def self.absolute_app_path(opts) fail 'opts must be a hash' unless opts.is_a? Hash caps = opts[:caps] || {} appium_lib_opts = opts[:appium_lib] || {} server_url = appium_lib_opts.fetch :server_url, false app_path = caps[:app] fail 'absolute_app_path invoked and app is not set!' if app_path.nil? || app_path.empty? # may be absolute path to file on remote server. # if the file is on the remote server then we can't check if it exists return app_path if server_url # Sauce storage API. http://saucelabs.com/docs/rest#storage return app_path if app_path.start_with? 'sauce-storage:' return app_path if app_path.match(/^http/) # public URL for Sauce if app_path.match(/^(\/|[a-zA-Z]:)/) # absolute file path app_path = File. app_path unless File.exist? app_path fail "App doesn't exist. #{app_path}" unless File.exist? app_path return app_path end # if it doesn't contain a slash then it's a bundle id return app_path unless app_path.match(/[\/\\]/) # relative path that must be expanded. # absolute_app_path is called from load_appium_txt # and the txt file path is the base of the app path in that case. app_path = File. app_path fail "App doesn't exist #{app_path}" unless File.exist? app_path app_path end |
Instance Method Details
#appium_server_version ⇒ Hash
Returns the server’s version info
“‘ruby {
"build" => {
"version" => "0.18.1",
"revision" => "d242ebcfd92046a974347ccc3a28f0e898595198"
}
} “‘
390 391 392 |
# File 'lib/appium_lib/driver.rb', line 390 def appium_server_version driver.remote_status end |
#device_is_android? ⇒ Boolean
374 375 376 |
# File 'lib/appium_lib/driver.rb', line 374 def device_is_android? @appium_device == :android end |
#driver_attributes ⇒ Object
Returns a hash of the driver attributes
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/appium_lib/driver.rb', line 354 def driver_attributes attributes = { caps: @caps, custom_url: @custom_url, export_session: @export_session, default_wait: @default_wait, last_waits: @last_waits, sauce_username: @sauce_username, sauce_access_key: @sauce_access_key, port: @appium_port, device: @appium_device, debug: @appium_debug } # Return duplicates so attributes are immutable attributes.each do |key, value| attributes[key] = value.duplicable? ? value.dup : value end attributes end |
#driver_quit ⇒ void
This method returns an undefined value.
Quits the driver
464 465 466 467 468 |
# File 'lib/appium_lib/driver.rb', line 464 def driver_quit # rescue NoSuchDriverError or nil driver # rubocop:disable Style/RescueModifier @driver.quit rescue nil end |
#execute_script(script, *args) ⇒ Object
The same as @driver.execute_script
571 572 573 |
# File 'lib/appium_lib/driver.rb', line 571 def execute_script(script, *args) @driver.execute_script script, *args end |
#exists(pre_check = 0, post_check = @default_wait, &search_block) ⇒ Boolean
Returns existence of element.
Example:
exists { button(‘sign in’) } ? puts(‘true’) : puts(‘false’)
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 |
# File 'lib/appium_lib/driver.rb', line 547 def exists(pre_check = 0, post_check = @default_wait, &search_block) # do not uset set_wait here. # it will cause problems with other methods reading the default_wait of 0 # which then gets converted to a 1 second wait. @driver.manage.timeouts.implicit_wait = pre_check # the element exists unless an error is raised. exists = true begin search_block.call # search for element rescue exists = false # error means it's not there end # restore wait @driver.manage.timeouts.implicit_wait = post_check if post_check != pre_check exists end |
#find_element(*args) ⇒ Element
Calls @driver.find_elements
587 588 589 |
# File 'lib/appium_lib/driver.rb', line 587 def find_element(*args) @driver.find_element(*args) end |
#find_elements(*args) ⇒ Array<Element>
Calls @driver.find_elements
579 580 581 |
# File 'lib/appium_lib/driver.rb', line 579 def find_elements(*args) @driver.find_elements(*args) end |
#no_wait ⇒ Object
Set implicit wait and default_wait to zero.
501 502 503 504 505 |
# File 'lib/appium_lib/driver.rb', line 501 def no_wait @last_waits = [@default_wait, 0] @default_wait = 0 @driver.manage.timeouts.implicit_wait = 0 end |
#restart ⇒ Driver
Restarts the driver
446 447 448 449 |
# File 'lib/appium_lib/driver.rb', line 446 def restart driver_quit start_driver end |
#screenshot(png_save_path) ⇒ nil
Takes a png screenshot and saves to the target path.
Example: screenshot ‘/tmp/hi.png’
457 458 459 460 |
# File 'lib/appium_lib/driver.rb', line 457 def screenshot(png_save_path) @driver.save_screenshot png_save_path nil end |
#server_url ⇒ String
Get the server url
435 436 437 438 439 440 441 442 |
# File 'lib/appium_lib/driver.rb', line 435 def server_url return @custom_url if @custom_url if !@sauce_username.nil? && !@sauce_access_key.nil? "http://#{@sauce_username}:#{@sauce_access_key}@ondemand.saucelabs.com:80/wd/hub" else "http://127.0.0.1:#{@appium_port}/wd/hub" end end |
#set_location(opts = {}) ⇒ Selenium::WebDriver::Location
This method does not work on real devices.
Calls @driver.set_location
600 601 602 603 604 605 |
# File 'lib/appium_lib/driver.rb', line 600 def set_location(opts = {}) latitude = opts.fetch(:latitude) longitude = opts.fetch(:longitude) altitude = opts.fetch(:altitude, 75) @driver.set_location(latitude, longitude, altitude) end |
#set_wait(timeout = nil) ⇒ void
This method returns an undefined value.
Set implicit wait and default_wait to timeout, defaults to 30. if set_wait is called without a param then the second to last wait will be used.
“‘ruby` set_wait 2 set_wait 3 set_wait # 2
““
520 521 522 523 524 525 526 527 528 529 530 531 532 533 |
# File 'lib/appium_lib/driver.rb', line 520 def set_wait(timeout = nil) if timeout.nil? # Appium::Logger.info "timeout = @default_wait = @last_wait" # Appium::Logger.info "timeout = @default_wait = #{@last_waits}" timeout = @default_wait = @last_waits.first else @default_wait = timeout # Appium::Logger.info "last waits before: #{@last_waits}" @last_waits = [@last_waits.last, @default_wait] # Appium::Logger.info "last waits after: #{@last_waits}" end @driver.manage.timeouts.implicit_wait = timeout end |
#start_driver ⇒ Selenium::WebDriver
Creates a new global driver and quits the old one if it exists.
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 |
# File 'lib/appium_lib/driver.rb', line 473 def start_driver @client ||= Selenium::WebDriver::Remote::Http::Default.new @client.timeout = 999_999 begin driver_quit @driver = Selenium::WebDriver.for :remote, http_client: @client, desired_capabilities: @caps, url: server_url # Load touch methods. @driver.extend Selenium::WebDriver::DriverExtensions::HasTouchScreen @driver.extend Selenium::WebDriver::DriverExtensions::HasLocation # export session if @export_session # rubocop:disable Style/RescueModifier File.open('/tmp/appium_lib_session', 'w') do |f| f.puts @driver.session_id end rescue nil end rescue Errno::ECONNREFUSED raise 'ERROR: Unable to connect to Appium. Is the server running?' end @driver.manage.timeouts.implicit_wait = @default_wait @driver end |
#x ⇒ void
This method returns an undefined value.
Quit the driver and Pry. quit and exit are reserved by Pry.
610 611 612 613 |
# File 'lib/appium_lib/driver.rb', line 610 def x driver_quit exit # exit pry end |