Skip to content

Commit 47f53f9

Browse files
Device is now required
The appium_lib gem is now able to launch apps on device without having an apk. The new device parameter eliminates the need to guess which device to use based on file extension. Fixed exception related to minitest spec support. Fixed issue where empty app path expanded to a directory.
1 parent 62d5f2a commit 47f53f9

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

lib/appium_lib/android/helper.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ def run node
361361
# Returns a string containing interesting elements.
362362
# @return [String]
363363
def get_inspect
364-
@selendroid ? get_selendroid_inspect : get_android_inspect
364+
@device == :selendroid ? get_selendroid_inspect : get_android_inspect
365365
end
366366

367367
# Intended for use with console.

lib/appium_lib/driver.rb

+28-20
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,15 @@ def update data, *args
5252

5353
update data, 'APP_PATH', 'APP_APK', 'APP_PACKAGE',
5454
'APP_ACTIVITY', 'APP_WAIT_ACTIVITY',
55-
'SELENDROID'
55+
'DEVICE'
5656

5757
# Ensure app path is absolute
58-
ENV['APP_PATH'] = File.expand_path ENV['APP_PATH'] if ENV['APP_PATH']
58+
ENV['APP_PATH'] = File.expand_path ENV['APP_PATH'] if ENV['APP_PATH'] &&
59+
!ENV['APP_PATH'].empty?
60+
61+
if ! %w(ios android selendroid).include? ENV['DEVICE']
62+
raise 'DEVICE must be ios, android, or selendroid'
63+
end
5964
end
6065

6166
# return list of require files as an array
@@ -74,6 +79,10 @@ def update data, *args
7479
end
7580
end
7681

82+
module MiniTest
83+
class Spec; end
84+
end
85+
7786
module Appium
7887
add_to_path __FILE__
7988

@@ -105,10 +114,11 @@ module Appium
105114
class Driver
106115
@@loaded = false
107116

108-
attr_reader :default_wait, :app_path, :app_name, :selendroid,
117+
attr_reader :default_wait, :app_path, :app_name,
109118
:app_package, :app_activity, :app_wait_activity,
110119
:sauce_username, :sauce_access_key, :port, :os, :debug
111120
# Creates a new driver.
121+
# :device is :android, :ios, or :selendroid
112122
#
113123
# ```ruby
114124
# # Options include:
@@ -120,11 +130,12 @@ class Driver
120130
# require 'appium_lib'
121131
#
122132
# # Start iOS driver
123-
# app = { app_path: '/path/to/MyiOS.app'}
133+
# app = { device: :ios, app_path: '/path/to/MyiOS.app'}
124134
# Appium::Driver.new(app).start_driver
125135
#
126136
# # Start Android driver
127-
# apk = { app_path: '/path/to/the.apk',
137+
# apk = { device: :android
138+
# app_path: '/path/to/the.apk',
128139
# app_package: 'com.example.pkg',
129140
# app_activity: 'act.Start',
130141
# app_wait_activity: 'act.Start'
@@ -152,10 +163,6 @@ def initialize opts={}
152163
# The name to use for the test run on Sauce.
153164
@app_name = opts.fetch :app_name, ENV['APP_NAME']
154165

155-
# If Android, this will toggle selendroid as a device
156-
@selendroid = opts.fetch :selendroid, ENV['SELENDROID']
157-
@selendroid = 'selendroid' if @selendroid
158-
159166
# Android app package
160167
@app_package = opts.fetch :app_package, ENV['APP_PACKAGE']
161168

@@ -173,12 +180,13 @@ def initialize opts={}
173180

174181
@port = opts.fetch :port, ENV['PORT'] || 4723
175182

176-
@os = :ios
177-
@os = :android if @app_path.match /\.apk/i
183+
# :ios, :android, :selendroid
184+
@device = opts.fetch :device, ENV['DEVICE'] || :ios
185+
@device = @device.intern # device must be a symbol
178186

179187
# load common methods
180188
extend Appium::Common
181-
if @os == :android
189+
if @device == :android
182190
raise 'APP_ACTIVITY must be set.' if @app_activity.nil?
183191

184192
# load Android specific methods
@@ -197,7 +205,7 @@ def initialize opts={}
197205
puts "Debug is: #{@debug}"
198206
if @debug
199207
ap opts unless opts.empty?
200-
puts "OS is: #{@os}"
208+
puts "Device is: #{@device}"
201209
patch_webdriver_bridge
202210
end
203211

@@ -210,7 +218,7 @@ def initialize opts={}
210218
@@loaded = true
211219
# Promote Appium driver methods to Object instance methods.
212220
$driver.public_methods(false).each do | m |
213-
MiniTest::Spec.class_eval do
221+
::MiniTest::Spec.class_eval do
214222
define_method m do | *args, &block |
215223
begin
216224
# puts "[Object.class_eval] Calling super for '#{m}'"
@@ -264,9 +272,8 @@ def android_capabilities
264272
browserName: 'Android',
265273
platform: 'LINUX',
266274
version: '4.2',
267-
device: @selendroid || 'Android',
275+
device: @device == :android ? 'Android' : 'selendroid',
268276
name: @app_name || 'Ruby Console Android Appium',
269-
app: absolute_app_path,
270277
:'app-package' => @app_package,
271278
:'app-activity' => @app_activity,
272279
:'app-wait-activity' => @app_wait_activity || @app_activity
@@ -281,14 +288,15 @@ def ios_capabilities
281288
platform: 'Mac 10.8',
282289
version: '6.0',
283290
device: 'iPhone Simulator',
284-
name: @app_name || 'Ruby Console iOS Appium',
285-
app: absolute_app_path
291+
name: @app_name || 'Ruby Console iOS Appium'
286292
}
287293
end
288294

289295
# @private
290296
def capabilities
291-
@os == :ios ? ios_capabilities : android_capabilities
297+
caps = @device == :ios ? ios_capabilities : android_capabilities
298+
caps[:app] = absolute_app_path unless @app_path.nil? || @app_path.empty?
299+
caps
292300
end
293301

294302
# Converts environment variable APP_PATH to an absolute path.
@@ -365,7 +373,7 @@ def start_driver
365373
# Set timeout to a large number so that Appium doesn't quit
366374
# when no commands are entered after 60 seconds.
367375
# broken on selendroid: https://github.com/appium/appium/issues/513
368-
mobile :setCommandTimeout, timeout: 9999 unless @selendroid
376+
mobile :setCommandTimeout, timeout: 9999 unless @device == :selendroid
369377

370378
# Set implicit wait by default unless we're using Pry.
371379
@driver.manage.timeouts.implicit_wait = @default_wait unless defined? Pry

0 commit comments

Comments
 (0)