Class: Appium::Driver
- Defined in:
- lib/appium_lib/driver.rb,
lib/appium_lib/driver.rb,
lib/appium_lib/common/command.rb,
lib/appium_lib/common/search_context.rb
Defined Under Namespace
Modules: Capabilities, Commands, SearchContext
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.
-
#appium_server_status ⇒ Object
readonly
Appium’s server version.
-
#appium_wait_interval ⇒ Integer
readonly
Return a time wait timeout Wait interval time for ::Appium::Common.wait or ::Appium::Common.wait_true.
-
#appium_wait_timeout ⇒ Integer
readonly
Return a time wait timeout Wait time for ::Appium::Common.wait or ::Appium::Common.wait_true.
-
#automation_name ⇒ Object
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.
-
#caps ⇒ Object
Selenium webdriver capabilities.
-
#custom_url ⇒ Object
Custom URL for the selenium server.
-
#default_wait ⇒ Integer
readonly
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.
-
#http_client ⇒ Selenium::WebDriver::Remote::Http::Default
readonly
Return http client called in start_driver().
-
#listener ⇒ Object
instance of AbstractEventListener for logging support.
-
#sauce_access_key ⇒ Object
Access Key for use on Sauce Labs.
-
#sauce_endpoint ⇒ Object
Override the Sauce Appium endpoint to allow e.g.
-
#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_client_version ⇒ Hash
Returns the client’s version info.
-
#appium_server_version ⇒ Hash
Returns the server’s version info.
-
#automation_name_is_xcuitest? ⇒ Boolean
Return true if automationName is ‘XCUITest’.
-
#check_server_version_xcuitest ⇒ Boolean
Return true if the target Appium server is over REQUIRED_VERSION_XCUITEST.
- #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) { ... } ⇒ Boolean
Returns existence of element.
-
#find_element(*args) ⇒ Element
Calls @driver.find_element.
-
#find_elements(*args) ⇒ Array<Element>
Calls @driver.find_elements_with_appium.
-
#initialize(opts = {}) ⇒ Driver
constructor
Creates a new driver.
-
#no_wait ⇒ Object
Set implicit 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.
-
#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
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 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 437 |
# File 'lib/appium_lib/driver.rb', line 362 def initialize(opts = {}) # quit last driver $driver.driver_quit if $driver raise 'opts must be a hash' unless opts.is_a? Hash opts = Appium.symbolize_keys opts @caps = Capabilities.init_caps_for_appium(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 @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?) @sauce_endpoint = appium_lib_opts.fetch :sauce_endpoint, ENV['SAUCE_ENDPOINT'] @sauce_endpoint = 'ondemand.saucelabs.com:443/wd/hub' if !@sauce_endpoint || (@sauce_endpoint.is_a?(String) && @sauce_endpoint.empty?) @appium_port = appium_lib_opts.fetch :port, 4723 # timeout and interval used in ::Appium::Comm.wait/wait_true @appium_wait_timeout = appium_lib_opts.fetch :wait_timeout, 30 @appium_wait_interval = appium_lib_opts.fetch :wait_interval, 0.5 # to pass it in Selenium.new. # `listener = opts.delete(:listener)` is called in Selenium::Driver.new @listener = appium_lib_opts.fetch :listener, nil # 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 @automation_name = @caps[:automationName] if @caps[:automationName] # 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 # for command patch_remote_driver_commands # 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
304 305 306 |
# File 'lib/appium_lib/driver.rb', line 304 def appium_debug @appium_debug end |
#appium_device ⇒ Object
Device type to request from the appium server
297 298 299 |
# File 'lib/appium_lib/driver.rb', line 297 def appium_device @appium_device end |
#appium_port ⇒ Object
Appium’s server port
295 296 297 |
# File 'lib/appium_lib/driver.rb', line 295 def appium_port @appium_port end |
#appium_server_status ⇒ Object (readonly)
Appium’s server version
302 303 304 |
# File 'lib/appium_lib/driver.rb', line 302 def appium_server_status @appium_server_status end |
#appium_wait_interval ⇒ Integer (readonly)
Return a time wait timeout Wait interval time for ::Appium::Common.wait or ::Appium::Common.wait_true. Provide Appium::Drive like { appium_lib: { wait_interval: 20 } }
322 323 324 |
# File 'lib/appium_lib/driver.rb', line 322 def appium_wait_interval @appium_wait_interval end |
#appium_wait_timeout ⇒ Integer (readonly)
Return a time wait timeout Wait time for ::Appium::Common.wait or ::Appium::Common.wait_true. Provide Appium::Drive like { appium_lib: { wait_timeout: 20 } }
317 318 319 |
# File 'lib/appium_lib/driver.rb', line 317 def appium_wait_timeout @appium_wait_timeout end |
#automation_name ⇒ Object (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.
300 301 302 |
# File 'lib/appium_lib/driver.rb', line 300 def automation_name @automation_name end |
#caps ⇒ Object
Selenium webdriver capabilities
278 279 280 |
# File 'lib/appium_lib/driver.rb', line 278 def caps @caps end |
#custom_url ⇒ Object
Custom URL for the selenium server
280 281 282 |
# File 'lib/appium_lib/driver.rb', line 280 def custom_url @custom_url end |
#default_wait ⇒ Integer (readonly)
Default wait time for elements to appear Returns the default client side wait. This value is independent of what the server is using
287 288 289 |
# File 'lib/appium_lib/driver.rb', line 287 def default_wait @default_wait end |
#driver ⇒ Driver (readonly)
Returns the driver
309 310 311 |
# File 'lib/appium_lib/driver.rb', line 309 def driver @driver end |
#export_session ⇒ Object
Export session id to textfile in /tmp for 3rd party tools
282 283 284 |
# File 'lib/appium_lib/driver.rb', line 282 def export_session @export_session end |
#global_webdriver_http_sleep ⇒ Object
The amount to sleep in seconds before every webdriver http call.
276 277 278 |
# File 'lib/appium_lib/driver.rb', line 276 def global_webdriver_http_sleep @global_webdriver_http_sleep end |
#http_client ⇒ Selenium::WebDriver::Remote::Http::Default (readonly)
Return http client called in start_driver()
312 313 314 |
# File 'lib/appium_lib/driver.rb', line 312 def http_client @http_client end |
#listener ⇒ Object
instance of AbstractEventListener for logging support
306 307 308 |
# File 'lib/appium_lib/driver.rb', line 306 def listener @listener end |
#sauce_access_key ⇒ Object
Access Key for use on Sauce Labs. Set ‘false` to disable Sauce, even when SAUCE_ACCESS_KEY is in ENV.
291 292 293 |
# File 'lib/appium_lib/driver.rb', line 291 def sauce_access_key @sauce_access_key end |
#sauce_endpoint ⇒ Object
Override the Sauce Appium endpoint to allow e.g. TestObject tests
293 294 295 |
# File 'lib/appium_lib/driver.rb', line 293 def sauce_endpoint @sauce_endpoint end |
#sauce_username ⇒ Object
Username for use on Sauce Labs. Set ‘false` to disable Sauce, even when SAUCE_USERNAME is in ENV.
289 290 291 |
# File 'lib/appium_lib/driver.rb', line 289 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.
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 |
# File 'lib/appium_lib/driver.rb', line 528 def self.absolute_app_path(opts) raise '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] raise '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 =~ /^http/ # public URL for Sauce if app_path =~ /^(\/|[a-zA-Z]:)/ # absolute file path app_path = File. app_path unless File.exist? app_path raise "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 =~ /[\/\\]/ # relative path that must be expanded. # absolute_app_path is called from load_settings # and the txt file path is the base of the app path in that case. app_path = File. app_path raise "App doesn't exist #{app_path}" unless File.exist? app_path app_path end |
Instance Method Details
#appium_client_version ⇒ Hash
Returns the client’s version info
“‘ruby
"version" => "9.1.1"
“‘
516 517 518 |
# File 'lib/appium_lib/driver.rb', line 516 def appium_client_version { version: ::Appium::VERSION } end |
#appium_server_version ⇒ Hash
Returns the server’s version info
“‘ruby {
"build" => {
"version" => "0.18.1",
"revision" => "d242ebcfd92046a974347ccc3a28f0e898595198"
}
} “‘
499 500 501 502 503 504 505 |
# File 'lib/appium_lib/driver.rb', line 499 def appium_server_version driver.remote_status rescue Selenium::WebDriver::Error::WebDriverError => ex raise unless ex..include?('content-type=""') # server (TestObject for instance) does not respond to status call {} end |
#automation_name_is_xcuitest? ⇒ Boolean
Return true if automationName is ‘XCUITest’
471 472 473 |
# File 'lib/appium_lib/driver.rb', line 471 def automation_name_is_xcuitest? !@automation_name.nil? && 'xcuitest'.casecmp(@automation_name).zero? end |
#check_server_version_xcuitest ⇒ Boolean
Return true if the target Appium server is over REQUIRED_VERSION_XCUITEST. If the Appium server is under REQUIRED_VERSION_XCUITEST, then error is raised.
478 479 480 481 482 483 484 485 |
# File 'lib/appium_lib/driver.rb', line 478 def check_server_version_xcuitest if automation_name_is_xcuitest? && !@appium_server_status.empty? && (@appium_server_status['build']['version'] < REQUIRED_VERSION_XCUITEST) raise Appium::Error::NotSupportedAppiumServer, "XCUITest requires Appium version >= #{REQUIRED_VERSION_XCUITEST}" end true end |
#device_is_android? ⇒ Boolean
465 466 467 |
# File 'lib/appium_lib/driver.rb', line 465 def device_is_android? @appium_device == :android end |
#driver_attributes ⇒ Object
Returns a hash of the driver attributes
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
# File 'lib/appium_lib/driver.rb', line 440 def driver_attributes attributes = { caps: @caps, automation_name: @automation_name, custom_url: @custom_url, export_session: @export_session, default_wait: @default_wait, sauce_username: @sauce_username, sauce_access_key: @sauce_access_key, sauce_endpoint: @sauce_endpoint, port: @appium_port, device: @appium_device, debug: @appium_debug, listener: @listener, wait_timeout: @appium_wait_timeout, wait_interval: @appium_wait_interval } # 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
590 591 592 593 594 595 |
# File 'lib/appium_lib/driver.rb', line 590 def driver_quit # rescue NoSuchDriverError or nil driver @driver.quit rescue nil end |
#execute_script(script, *args) ⇒ Object
The same as @driver.execute_script
693 694 695 |
# File 'lib/appium_lib/driver.rb', line 693 def execute_script(script, *args) @driver.execute_script script, *args end |
#exists(pre_check = 0, post_check = @default_wait) { ... } ⇒ Boolean
Returns existence of element.
Example:
exists { button(‘sign in’) } ? puts(‘true’) : puts(‘false’)
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 |
# File 'lib/appium_lib/driver.rb', line 669 def exists(pre_check = 0, post_check = @default_wait) # 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 yield # 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_element
“‘ “`
If you call ‘Appium.promote_appium_methods`, you can call `find_element` directly.
730 731 732 |
# File 'lib/appium_lib/driver.rb', line 730 def find_element(*args) @driver.find_element(*args) end |
#find_elements(*args) ⇒ Array<Element>
Calls @driver.find_elements_with_appium
“‘ “`
If you call ‘Appium.promote_appium_methods`, you can call `find_elements` directly.
“‘ “`
If you call ‘Appium.promote_appium_methods`, you can call `find_elements` directly.
715 716 717 |
# File 'lib/appium_lib/driver.rb', line 715 def find_elements(*args) @driver.find_elements(*args) end |
#no_wait ⇒ Object
Set implicit wait to zero.
638 639 640 |
# File 'lib/appium_lib/driver.rb', line 638 def no_wait @driver.manage.timeouts.implicit_wait = 0 end |
#restart ⇒ Driver
Restarts the driver
572 573 574 575 |
# File 'lib/appium_lib/driver.rb', line 572 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’
583 584 585 586 |
# File 'lib/appium_lib/driver.rb', line 583 def screenshot(png_save_path) @driver.save_screenshot png_save_path nil end |
#server_url ⇒ String
Get the server url
561 562 563 564 565 566 567 568 |
# File 'lib/appium_lib/driver.rb', line 561 def server_url return @custom_url if @custom_url if !@sauce_username.nil? && !@sauce_access_key.nil? "https://#{@sauce_username}:#{@sauce_access_key}@#{@sauce_endpoint}" 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
743 744 745 746 747 748 |
# File 'lib/appium_lib/driver.rb', line 743 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. Default to @default_wait.
“‘ruby set_wait 2 set_wait # @default_wait
“‘
652 653 654 655 |
# File 'lib/appium_lib/driver.rb', line 652 def set_wait(timeout = nil) timeout = @default_wait if timeout.nil? @driver.manage.timeouts.implicit_wait = timeout end |
#start_driver ⇒ Selenium::WebDriver
Creates a new global driver and quits the old one if it exists.
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 |
# File 'lib/appium_lib/driver.rb', line 600 def start_driver # open_timeout and read_timeout are explicit wait. @http_client ||= Selenium::WebDriver::Remote::Http::Default.new(open_timeout: 999_999, read_timeout: 999_999) begin driver_quit @driver = Selenium::WebDriver.for(:remote, http_client: @http_client, desired_capabilities: @caps, url: server_url, listener: @listener) # 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 on #{server_url}?" end @appium_server_status = appium_server_version check_server_version_xcuitest set_automation_name_if_nil @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.
753 754 755 756 |
# File 'lib/appium_lib/driver.rb', line 753 def x driver_quit exit # exit pry end |