lib/appium_lib/driver.rb in appium_lib-0.5.16 vs lib/appium_lib/driver.rb in appium_lib-0.6.0

- old
+ new

@@ -50,14 +50,19 @@ data = TOML::Parser.new(data).parsed ap data unless data.empty? if verbose update data, 'APP_PATH', 'APP_APK', 'APP_PACKAGE', 'APP_ACTIVITY', 'APP_WAIT_ACTIVITY', - 'SELENDROID' + 'DEVICE' # Ensure app path is absolute - ENV['APP_PATH'] = File.expand_path ENV['APP_PATH'] if ENV['APP_PATH'] + ENV['APP_PATH'] = File.expand_path ENV['APP_PATH'] if ENV['APP_PATH'] && + !ENV['APP_PATH'].empty? + + if ! %w(ios android selendroid).include? ENV['DEVICE'] + raise 'DEVICE must be ios, android, or selendroid' + end end # return list of require files as an array # nil if require doesn't exist if data && data['require'] @@ -72,10 +77,18 @@ end r.compact # remove nils end end +# Fix uninitialized constant Minitest (NameError) +module Minitest + # Fix superclass mismatch for class Spec + class Runnable; end + class Test < Runnable; end + class Spec < Test; end +end + module Appium add_to_path __FILE__ require 'selenium-webdriver' @@ -103,14 +116,15 @@ require 'android/element/textfield' class Driver @@loaded = false - attr_reader :default_wait, :app_path, :app_name, :selendroid, + attr_reader :default_wait, :app_path, :app_name, :device, :app_package, :app_activity, :app_wait_activity, - :sauce_username, :sauce_access_key, :port, :os, :debug + :sauce_username, :sauce_access_key, :port, :debug # Creates a new driver. + # :device is :android, :ios, or :selendroid # # ```ruby # # Options include: # :app_path, :app_name, :app_package, :app_activity, # :app_wait_activity, :sauce_username, :sauce_access_key, @@ -118,15 +132,16 @@ # # require 'rubygems' # require 'appium_lib' # # # Start iOS driver - # app = { app_path: '/path/to/MyiOS.app'} + # app = { device: :ios, app_path: '/path/to/MyiOS.app'} # Appium::Driver.new(app).start_driver # # # Start Android driver - # apk = { app_path: '/path/to/the.apk', + # apk = { device: :android + # app_path: '/path/to/the.apk', # app_package: 'com.example.pkg', # app_activity: 'act.Start', # app_wait_activity: 'act.Start' # } # @@ -150,14 +165,10 @@ raise 'APP_PATH must be set.' if @app_path.nil? # The name to use for the test run on Sauce. @app_name = opts.fetch :app_name, ENV['APP_NAME'] - # If Android, this will toggle selendroid as a device - @selendroid = opts.fetch :selendroid, ENV['SELENDROID'] - @selendroid = 'selendroid' if @selendroid - # Android app package @app_package = opts.fetch :app_package, ENV['APP_PACKAGE'] # Android app starting activity. @app_activity = opts.fetch :app_activity, ENV['APP_ACTIVITY'] @@ -171,16 +182,17 @@ # Sauce Key @sauce_access_key = opts.fetch :sauce_access_key, ENV['SAUCE_ACCESS_KEY'] @port = opts.fetch :port, ENV['PORT'] || 4723 - @os = :ios - @os = :android if @app_path.match /\.apk/i + # :ios, :android, :selendroid + @device = opts.fetch :device, ENV['DEVICE'] || :ios + @device = @device.intern # device must be a symbol # load common methods extend Appium::Common - if @os == :android + if @device == :android raise 'APP_ACTIVITY must be set.' if @app_activity.nil? # load Android specific methods extend Appium::Android else @@ -195,11 +207,11 @@ # !!'constant' == true @debug = opts.fetch :debug, !!defined?(Pry) puts "Debug is: #{@debug}" if @debug ap opts unless opts.empty? - puts "OS is: #{@os}" + puts "Device is: #{@device}" patch_webdriver_bridge end # Save global reference to last created Appium driver for top level methods. $driver = self @@ -208,18 +220,22 @@ # Subsequent drivers do not trigger promotion. unless @@loaded @@loaded = true # Promote Appium driver methods to Object instance methods. $driver.public_methods(false).each do | m | - Object.class_eval do + # not MiniTest::Spec + ::Minitest::Spec.class_eval do define_method m do | *args, &block | begin # puts "[Object.class_eval] Calling super for '#{m}'" # prefer existing method. # super will invoke method missing on driver super(*args, &block) - rescue NoMethodError + # minitest also defines a name method, + # so rescue argument error + # and call the name method on $driver + rescue NoMethodError, ArgumentError # puts "[Object.class_eval] '#{m}' not on super" $driver.send m, *args, &block if $driver.respond_to?(m) end end end @@ -262,13 +278,12 @@ def android_capabilities { browserName: 'Android', platform: 'LINUX', version: '4.2', - device: @selendroid || 'Android', + device: @device == :android ? 'Android' : 'selendroid', name: @app_name || 'Ruby Console Android Appium', - app: absolute_app_path, :'app-package' => @app_package, :'app-activity' => @app_activity, :'app-wait-activity' => @app_wait_activity || @app_activity } end @@ -279,18 +294,19 @@ { browserName: 'iOS 6.0', platform: 'Mac 10.8', version: '6.0', device: 'iPhone Simulator', - name: @app_name || 'Ruby Console iOS Appium', - app: absolute_app_path + name: @app_name || 'Ruby Console iOS Appium' } end # @private def capabilities - @os == :ios ? ios_capabilities : android_capabilities + caps = @device == :ios ? ios_capabilities : android_capabilities + caps[:app] = absolute_app_path unless @app_path.nil? || @app_path.empty? + caps end # Converts environment variable APP_PATH to an absolute path. # @return [String] APP_PATH as an absolute path def absolute_app_path @@ -363,10 +379,10 @@ end # Set timeout to a large number so that Appium doesn't quit # when no commands are entered after 60 seconds. # broken on selendroid: https://github.com/appium/appium/issues/513 - mobile :setCommandTimeout, timeout: 9999 unless @selendroid + mobile :setCommandTimeout, timeout: 9999 unless @device == :selendroid # Set implicit wait by default unless we're using Pry. @driver.manage.timeouts.implicit_wait = @default_wait unless defined? Pry @driver