Skip to content

Commit 29c528e

Browse files
authored
arrange consts names and add some options for app management (#59)
* arrange consts names * add options for manage apps * fix rubocop * update documentation * add only for android * arrange a bit
1 parent da7fd55 commit 29c528e

File tree

9 files changed

+182
-47
lines changed

9 files changed

+182
-47
lines changed

lib/appium_lib_core.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
require_relative 'appium_lib_core/device/touch_actions'
1010
require_relative 'appium_lib_core/device/multi_touch'
1111
require_relative 'appium_lib_core/device/screen_record'
12-
require_relative 'appium_lib_core/device/app_management'
12+
require_relative 'appium_lib_core/device/app_state'
1313

1414
require_relative 'appium_lib_core/android'
1515
require_relative 'appium_lib_core/android_uiautomator2'

lib/appium_lib_core/common/command.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ module Commands
4040
app_installed?: [:post, 'session/:session_id/appium/device/app_installed'.freeze],
4141
activate_app: [:post, 'session/:session_id/appium/device/activate_app'.freeze],
4242
terminate_app: [:post, 'session/:session_id/appium/device/terminate_app'.freeze],
43-
app_state: [:get, 'session/:session_id/appium/device/app_state'.freeze],
43+
app_state: [:post, 'session/:session_id/appium/device/app_state'.freeze],
4444
background_app: [:post, 'session/:session_id/appium/app/background'.freeze],
4545
hide_keyboard: [:post, 'session/:session_id/appium/device/hide_keyboard'.freeze],
4646
press_keycode: [:post, 'session/:session_id/appium/device/press_keycode'.freeze],

lib/appium_lib_core/common/device.rb

+73-22
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ module Device
4949

5050
# @!method is_keyboard_shown
5151
# Get whether keyboard is displayed or not.
52-
# @return [Bool] Return true if keyboard is shown. Return false if keyboard is hidden.
52+
# @return [Boolean] Return true if keyboard is shown. Return false if keyboard is hidden.
5353
#
5454
# @example
5555
# @driver.is_keyboard_shown # false
@@ -142,25 +142,37 @@ module Device
142142
## With arguments
143143
####
144144

145-
# @!method install_app(path)
145+
# @!method install_app(path, replace: nil, timeout: nil, allow_test_packages: nil, use_sdcard: nil, grant_permissions: nil)
146146
# Install the given app onto the device
147147
#
148+
# @param [String] path The absolute local path or remote http URL to an .ipa or .apk file, or a .zip containing one of these.
149+
# @param [Boolean] replace: Only for Android. Whether to reinstall/upgrade the package if it is already present on the device under test. `true` by default
150+
# @param [Integer] timeout: Only for Android. How much time to wait for the installation to complete. 60000ms by default.
151+
# @param [Boolean] allow_test_packages: Only for Android. Whether to allow installation of packages marked as test in the manifest. `false` by default
152+
# @param [Boolean] use_sdcard: Only for Android. Whether to use the SD card to install the app. `false` by default
153+
# @param [Boolean] grant_permissions: Only for Android. whether to automatically grant application permissions on Android 6+ after the installation completes. `false` by default
154+
#
148155
# @example
149156
#
150157
# @driver.install_app("/path/to/test.apk")
158+
# @driver.install_app("/path/to/test.apk", replace: true, timeout: 20000, allow_test_packages: true, use_sdcard: false, grant_permissions: false)
151159
#
152160

153-
# @!method remove_app(app_id)
154-
# Install the given app onto the device
161+
# @!method remove_app(app_id, keep_data: nil, timeout: nil)
162+
#
163+
# @param [Strong] app_id BundleId for iOS or package name for Android
164+
# @param [Boolean] keep_data: Only for Android. Whether to keep application data and caches after it is uninstalled. `false` by default
165+
# @param [Integer] timeout: Only for Android. How much time to wait for the uninstall to complete. 20000ms by default.
155166
#
156167
# @example
157168
#
158169
# @driver.remove_app("io.appium.bundle")
170+
# @driver.remove_app("io.appium.bundle", keep_data: false, timeout, 10000)
159171
#
160172

161173
# @!method app_installed?(app_id)
162174
# Check whether the specified app is installed on the device
163-
# @return [bool]
175+
# @return [Boolean]
164176
#
165177
# @example
166178
#
@@ -169,11 +181,15 @@ module Device
169181

170182
# @!method terminate_app(app_id)
171183
# Terminate the specified app.
172-
# @return [bool]
184+
#
185+
# @param [Strong] app_id BundleId for iOS or package name for Android
186+
# @param [Integer] timeout: Only for Android. How much time to wait for the application termination to complete. 500ms by default.
187+
# @return [Boolean]
173188
#
174189
# @example
175190
#
176191
# @driver.terminate_app("io.appium.bundle") # true
192+
# @driver.terminate_app("io.appium.bundle", timeout: 500)
177193
#
178194

179195
# @!method activate_app(app_id)
@@ -187,11 +203,11 @@ module Device
187203

188204
# Get the status of an existing application on the device.
189205
# State:
190-
# AppManagement::APP_STATE_NOT_INSTALLED : The current application state cannot be determined/is unknown
191-
# AppManagement::APP_STATE_NOT_RUNNING : The application is not running
192-
# AppManagement::APP_STATE_RUNNING_IN_BACKGROUND_SUSPEND : The application is running in the background and is suspended
193-
# AppManagement::APP_STATE_RUNNING_IN_BACKGROUND : The application is running in the background and is not suspended
194-
# AppManagement::APP_STATE_RUNNING_IN_FOREGROUND : The application is running in the foreground
206+
# AppState::NOT_INSTALLED : The current application state cannot be determined/is unknown
207+
# AppState::NOT_RUNNING : The application is not running
208+
# AppState::RUNNING_IN_BACKGROUND_SUSPENDED : The application is running in the background and is suspended
209+
# AppState::RUNNING_IN_BACKGROUND : The application is running in the background and is not suspended
210+
# AppState::RUNNING_IN_FOREGROUND : The application is running in the foreground
195211
#
196212
# For more details: https://developer.apple.com/documentation/xctest/xcuiapplicationstate
197213
#
@@ -630,17 +646,45 @@ def create_bridge_command(method)
630646
end
631647
end
632648

649+
# rubocop:disable Metrics/ParameterLists,Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity
633650
def add_app_management
634651
add_endpoint_method(:install_app) do
635-
def install_app(path)
636-
execute :install_app, {}, appPath: path
652+
def install_app(path,
653+
replace: nil,
654+
timeout: nil,
655+
allow_test_packages: nil,
656+
use_sdcard: nil,
657+
grant_permissions: nil)
658+
args = { appPath: path }
659+
660+
args[:options] = {} unless options?(replace, timeout, allow_test_packages, use_sdcard, grant_permissions)
661+
662+
args[:options][:replace] = replace unless replace.nil?
663+
args[:options][:timeout] = timeout unless timeout.nil?
664+
args[:options][:allowTestPackages] = allow_test_packages unless allow_test_packages.nil?
665+
args[:options][:useSdcard] = use_sdcard unless use_sdcard.nil?
666+
args[:options][:grantPermissions] = grant_permissions unless grant_permissions.nil?
667+
668+
execute :install_app, {}, args
669+
end
670+
671+
private
672+
673+
def options?(replace, timeout, allow_test_packages, use_sdcard, grant_permissions)
674+
replace.nil? || timeout.nil? || allow_test_packages.nil? || use_sdcard.nil? || grant_permissions.nil?
637675
end
638676
end
639677

640678
add_endpoint_method(:remove_app) do
641-
def remove_app(id)
679+
def remove_app(id, keep_data: nil, timeout: nil)
642680
# required: [['appId'], ['bundleId']]
643-
execute :remove_app, {}, appId: id
681+
args = { appId: id }
682+
683+
args[:options] = {} unless keep_data.nil? || timeout.nil?
684+
args[:options][:keepData] = keep_data unless keep_data.nil?
685+
args[:options][:timeout] = timeout unless timeout.nil?
686+
687+
execute :remove_app, {}, args
644688
end
645689
end
646690

@@ -659,9 +703,15 @@ def activate_app(app_id)
659703
end
660704

661705
add_endpoint_method(:terminate_app) do
662-
def terminate_app(app_id)
706+
def terminate_app(app_id, timeout: nil)
663707
# required: [['appId'], ['bundleId']]
664-
execute :terminate_app, {}, appId: app_id
708+
#
709+
args = { appId: app_id }
710+
711+
args[:options] = {} unless timeout.nil?
712+
args[:options][:timeout] = timeout unless timeout.nil?
713+
714+
execute :terminate_app, {}, args
665715
end
666716
end
667717

@@ -672,22 +722,23 @@ def app_state(app_id)
672722

673723
case response
674724
when 0
675-
Appium::Core::Device::AppManagement::APP_STATE_NOT_INSTALLED
725+
Appium::Core::Device::AppState::NOT_INSTALLED
676726
when 1
677-
Appium::Core::Device::AppManagement::APP_STATE_NOT_RUNNING
727+
Appium::Core::Device::AppState::NOT_RUNNING
678728
when 2
679-
Appium::Core::Device::AppManagement::APP_STATE_RUNNING_IN_BACKGROUND_SUSPEND
729+
Appium::Core::Device::AppState::RUNNING_IN_BACKGROUND_SUSPENDED
680730
when 3
681-
Appium::Core::Device::AppManagement::APP_STATE_RUNNING_IN_BACKGROUND
731+
Appium::Core::Device::AppState::RUNNING_IN_BACKGROUND
682732
when 4
683-
Appium::Core::Device::AppManagement::APP_STATE_RUNNING_IN_FOREGROUND
733+
Appium::Core::Device::AppState::RUNNING_IN_FOREGROUND
684734
else
685735
Appium::Logger.debug("Unexpected status in app_state: #{response}")
686736
response
687737
end
688738
end
689739
end
690740
end
741+
# rubocop:enable Metrics/ParameterLists,Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity
691742

692743
def add_keyevent
693744
# Only for Selendroid

lib/appium_lib_core/device/app_management.rb

-13
This file was deleted.
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Appium
2+
module Core
3+
module Device
4+
class AppState
5+
NOT_INSTALLED = 0
6+
NOT_RUNNING = 1
7+
RUNNING_IN_BACKGROUND_SUSPENDED = 2
8+
RUNNING_IN_BACKGROUND = 3
9+
RUNNING_IN_FOREGROUND = 4
10+
end
11+
end
12+
end
13+
end

test/functional/android/android/device_test.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,15 @@ def test_re_install
110110

111111
def test_app_management
112112
assert @@driver.app_state('io.appium.android.apis') ==
113-
Appium::Core::Device::AppManagement::APP_STATE_RUNNING_IN_FOREGROUND
113+
Appium::Core::Device::AppState::RUNNING_IN_FOREGROUND
114114

115115
assert @@driver.terminate_app('io.appium.android.apis')
116116
assert @@driver.app_state('io.appium.android.apis') ==
117-
Appium::Core::Device::AppManagement::APP_STATE_NOT_RUNNING
117+
Appium::Core::Device::AppState::NOT_RUNNING
118118

119119
assert @@driver.activate_app('io.appium.android.apis').nil?
120120
assert @@driver.app_state('io.appium.android.apis') ==
121-
Appium::Core::Device::AppManagement::APP_STATE_RUNNING_IN_FOREGROUND
121+
Appium::Core::Device::AppState::RUNNING_IN_FOREGROUND
122122
end
123123

124124
def test_start_activity

test/functional/ios/ios/device_test.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,15 @@ def test_re_install
109109

110110
def test_app_management
111111
assert @@driver.app_state('com.example.apple-samplecode.UICatalog') ==
112-
Appium::Core::Device::AppManagement::APP_STATE_RUNNING_IN_FOREGROUND
112+
Appium::Core::Device::AppState::RUNNING_IN_FOREGROUND
113113

114114
assert @@driver.terminate_app('com.example.apple-samplecode.UICatalog')
115115
assert @@driver.app_state('com.example.apple-samplecode.UICatalog') ==
116-
Appium::Core::Device::AppManagement::APP_STATE_NOT_RUNNING
116+
Appium::Core::Device::AppState::NOT_RUNNING
117117

118118
assert @@driver.activate_app('com.example.apple-samplecode.UICatalog') == {}
119119
assert @@driver.app_state('com.example.apple-samplecode.UICatalog') ==
120-
Appium::Core::Device::AppManagement::APP_STATE_RUNNING_IN_FOREGROUND
120+
Appium::Core::Device::AppState::RUNNING_IN_FOREGROUND
121121
end
122122

123123
def test_push_pull

test/unit/android/device_test.rb

+44-2
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,28 @@ def test_install_app
280280
assert_requested(:post, "#{SESSION}/appium/device/install_app", times: 1)
281281
end
282282

283+
def test_install_app_with_params
284+
stub_request(:post, "#{SESSION}/appium/device/install_app")
285+
.with(body: { appPath: 'app_path',
286+
options: {
287+
replace: true,
288+
timeout: 20_000,
289+
allowTestPackages: true,
290+
useSdcard: false,
291+
grantPermissions: false
292+
} }.to_json)
293+
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
294+
295+
@driver.install_app 'app_path',
296+
replace: true,
297+
timeout: 20_000,
298+
allow_test_packages: true,
299+
use_sdcard: false,
300+
grant_permissions: false
301+
302+
assert_requested(:post, "#{SESSION}/appium/device/install_app", times: 1)
303+
end
304+
283305
def test_remove_app
284306
stub_request(:post, "#{SESSION}/appium/device/remove_app")
285307
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
@@ -289,6 +311,16 @@ def test_remove_app
289311
assert_requested(:post, "#{SESSION}/appium/device/remove_app", times: 1)
290312
end
291313

314+
def test_remove_app_with_param
315+
stub_request(:post, "#{SESSION}/appium/device/remove_app")
316+
.with(body: { appId: 'com.app.id', options: { keepData: false, timeout: 20_000 } }.to_json)
317+
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
318+
319+
@driver.remove_app 'com.app.id', keep_data: false, timeout: 20_000
320+
321+
assert_requested(:post, "#{SESSION}/appium/device/remove_app", times: 1)
322+
end
323+
292324
def test_terminate_app
293325
stub_request(:post, "#{SESSION}/appium/device/terminate_app")
294326
.to_return(headers: HEADER, status: 200, body: { value: true }.to_json)
@@ -298,6 +330,16 @@ def test_terminate_app
298330
assert_requested(:post, "#{SESSION}/appium/device/terminate_app", times: 1)
299331
end
300332

333+
def test_terminate_app_with_param
334+
stub_request(:post, "#{SESSION}/appium/device/terminate_app")
335+
.with(body: { appId: 'com.app.id', options: { timeout: 20_000 } }.to_json)
336+
.to_return(headers: HEADER, status: 200, body: { value: true }.to_json)
337+
338+
@driver.terminate_app 'com.app.id', timeout: 20_000
339+
340+
assert_requested(:post, "#{SESSION}/appium/device/terminate_app", times: 1)
341+
end
342+
301343
def test_activate_app
302344
stub_request(:post, "#{SESSION}/appium/device/activate_app")
303345
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
@@ -308,12 +350,12 @@ def test_activate_app
308350
end
309351

310352
def test_app_state
311-
stub_request(:get, "#{SESSION}/appium/device/app_state")
353+
stub_request(:post, "#{SESSION}/appium/device/app_state")
312354
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
313355

314356
@driver.app_state 'com.app.id'
315357

316-
assert_requested(:get, "#{SESSION}/appium/device/app_state", times: 1)
358+
assert_requested(:post, "#{SESSION}/appium/device/app_state", times: 1)
317359
end
318360

319361
def test_app_installed?

0 commit comments

Comments
 (0)