lib/appium_lib/driver.rb in appium_lib-0.4.2 vs lib/appium_lib/driver.rb in appium_lib-0.5.0
- old
+ new
@@ -2,20 +2,68 @@
=begin
Based on simple_test.rb
https://github.com/appium/appium/blob/82995f47408530c80c3376f4e07a1f649d96ba22/sample-code/examples/ruby/simple_test.rb
https://github.com/appium/appium/blob/c58eeb66f2d6fa3b9a89d188a2e657cca7cb300f/LICENSE
=end
+
+# Load appium.txt (toml format) into system ENV
+# the basedir of this file + appium.txt is what's used
+# @param opts [Hash] file: '/path/to/appium.txt', verbose: true
+# @return [nil]
+def load_appium_txt opts
+ raise 'opts must be a hash' unless opts.kind_of? Hash
+ opts.each_pair { |k,v| opts[k.to_s.downcase.strip.intern] = v }
+ opts = {} if opts.nil?
+ file = opts.fetch :file, nil
+ raise 'Must pass file' unless file
+ verbose = opts.fetch :verbose, false
+ # Check for env vars in .txt
+ toml = File.expand_path File.join File.dirname(file), 'appium.txt'
+ puts "appium.txt path: #{toml}" if verbose
+ # @private
+ def update data, *args
+ args.each do |name|
+ var = data[name]
+ ENV[name] = var if var
+ end
+ end
+
+ toml_exists = File.exists? toml
+ puts "Exists? #{toml_exists}" if verbose
+
+ if toml_exists
+ require 'toml'
+ require 'ap'
+ puts "Loading #{toml}" if verbose
+
+ # bash requires A="OK"
+ # toml requires A = "OK"
+ #
+ # A="OK" => A = "OK"
+ data = File.read(toml).gsub /([^\s])\=(")/, "\\1 = \\2"
+ data = TOML::Parser.new(data).parsed
+ ap data unless data.empty?
+
+ update data, 'APP_PATH', 'APP_APK', 'APP_PACKAGE',
+ 'APP_ACTIVITY', 'APP_WAIT_ACTIVITY',
+ 'SELENDROID'
+
+ # Ensure app path is absolute
+ ENV['APP_PATH'] = File.expand_path ENV['APP_PATH'] if ENV['APP_PATH']
+ end
+ nil
+end
+
module Appium
add_to_path __FILE__
require 'selenium-webdriver'
# common
require 'common/helper'
require 'common/patch'
require 'common/version'
- require 'common/dynamic'
require 'common/element/button'
require 'common/element/text'
require 'common/element/window'
# ios
@@ -24,22 +72,23 @@
require 'ios/element/alert'
require 'ios/element/generic'
require 'ios/element/textfield'
# android
+ require 'android/dynamic'
require 'android/helper'
require 'android/patch'
require 'android/element/alert'
require 'android/element/generic'
require 'android/element/textfield'
class Driver
@@loaded = false
- attr_reader :app_path, :app_name, :app_package, :app_activity,
- :app_wait_activity, :sauce_username, :sauce_access_key,
- :port, :os, :debug
+ attr_reader :default_wait, :app_path, :app_name, :selendroid,
+ :app_package, :app_activity, :app_wait_activity,
+ :sauce_username, :sauce_access_key, :port, :os, :debug
# Creates a new driver.
#
# ```ruby
# # Options include:
# :app_path, :app_name, :app_package, :app_activity,
@@ -61,21 +110,21 @@
# }
#
# Appium::Driver.new(apk).start_driver
# ```
#
- # @param options [Object] A hash containing various options.
+ # @param opts [Object] A hash containing various options.
# @return [Driver]
- def initialize options={}
+ def initialize opts={}
# quit last driver
$driver.driver_quit if $driver
-
- opts = {}
+ opts = {} if opts.nil?
# convert to downcased symbols
- options.each_pair { |k,v| opts[k.to_s.downcase.strip.intern] = v }
+ opts.each_pair { |k,v| opts[k.to_s.downcase.strip.intern] = v }
- opts = {} if opts.nil?
+ @default_wait = opts.fetch :wait, 30
+
# Path to the .apk, .app or .app.zip.
# The path can be local or remote for Sauce.
@app_path = opts.fetch :app_path, ENV['APP_PATH']
raise 'APP_PATH must be set.' if @app_path.nil?
@@ -120,13 +169,15 @@
# apply os specific patches
patch_webdriver_element
# enable debug patch
- @debug = opts.fetch :debug, defined?(Pry)
-
+ # !!'constant' == true
+ @debug = opts.fetch :debug, !!defined?(Pry)
+ puts "Debug is: #{@debug}"
if @debug
+ ap opts unless opts.empty?
puts "OS is: #{@os}"
patch_webdriver_bridge
end
# Save global reference to last created Appium driver for top level methods.
@@ -196,19 +247,19 @@
end
# Converts environment variable APP_PATH to an absolute path.
# @return [String] APP_PATH as an absolute path
def absolute_app_path
- raise 'APP_PATH environment variable not set!' if @app_path.nil? || @app_path.empty?
- return @app_path if @app_path.match(/^http/) # public URL for Sauce
- if @app_path.match(/^\//) # absolute file path
- raise "App doesn't exist. #{@app_path}" unless File.exist? @app_path
- return @app_path
- end
- file = File.join(File.dirname(__FILE__), @app_path)
- raise "App doesn't exist #{file}" unless File.exist? file
- file
+ raise 'APP_PATH environment variable not set!' if @app_path.nil? || @app_path.empty?
+ return @app_path if @app_path.match(/^http/) # public URL for Sauce
+ if @app_path.match(/^\//) # absolute file path
+ raise "App doesn't exist. #{@app_path}" unless File.exist? @app_path
+ return @app_path
+ end
+ file = File.join(File.dirname(__FILE__), @app_path)
+ raise "App doesn't exist #{file}" unless File.exist? file
+ file
end
# Get the server url for sauce or local based on env vars.
# @return [String] the server url
def server_url
@@ -232,10 +283,12 @@
@driver
end
# Takes a png screenshot and saves to the target path.
#
+ # Example: screenshot '/tmp/hi.png'
+ #
# @param png_save_path [String] the full path to save the png
# @return [void]
def screenshot png_save_path
@driver.save_screenshot png_save_path
end
@@ -247,18 +300,15 @@
begin; @driver.quit unless @driver.nil?; rescue; end
end
# Creates a new global driver and quits the old one if it exists.
#
- # @param wait [Integer] seconds to wait before timing out a command. defaults to 30 seconds
# @return [Selenium::WebDriver] the new global driver
- def start_driver wait=30
+ def start_driver
@client = @client || Selenium::WebDriver::Remote::Http::Default.new
@client.timeout = 999999
- @default_wait = wait
-
begin
@driver = Selenium::WebDriver.for :remote, http_client: @client, desired_capabilities: capabilities, url: server_url
# Load touch methods. Required for Selendroid.
@driver.extend Selenium::WebDriver::DriverExtensions::HasTouchScreen
rescue Errno::ECONNREFUSED
@@ -299,16 +349,17 @@
#
# Example:
#
# exists { button('sign in') } ? puts('true') : puts('false')
#
+ # @param pre_check [Integer] the amount in seconds to set the
+ # wait to before checking existance
+ # @param post_check [Integer] the amount in seconds to set the
+ # wait to after checking existance
# @param search_block [Block] the block to call
# @return [Boolean]
- def exists &search_block
- pre_check = 0
- post_check = @default_wait
-
+ def exists pre_check=0, post_check=@default_wait, &search_block
set_wait pre_check # set wait to zero
# the element exists unless an error is raised.
exists = true
@@ -317,10 +368,10 @@
rescue
exists = false # error means it's not there
end
# restore wait
- set_wait post_check
+ set_wait post_check if post_check != pre_check
exists
end
# The same as @driver.execute_script
\ No newline at end of file