Skip to content

Commit c9fff7b

Browse files
authored
Improve override background (#188)
* improve included, extended methods * update changelog * add tests for espresso * add tests for appium automation name for android and ios
1 parent 1e588c8 commit c9fff7b

File tree

16 files changed

+123
-12
lines changed

16 files changed

+123
-12
lines changed

.travis.yml

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ before_install:
1414
script:
1515
- bundle exec rake rubocop
1616
- bundle exec parallel_test test/unit/ -n 4
17+
- AUTOMATION_NAME_DROID=espresso bundle exec parallel_test test/unit/android -n 4
18+
- AUTOMATION_NAME_DROID=appium AUTOMATION_NAME_IOS=appium bundle exec parallel_test test/unit -n 4
1719

1820
notifications:
1921
email:

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Read `release_notes.md` for commit level details.
66
### Enhancements
77

88
### Bug fixes
9+
- Fix potential override of `AppManagement#background_app`
910

1011
### Deprecations
1112

lib/appium_lib_core/android/device.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ def start_activity(opts)
326326
end
327327
end
328328

329-
# Android, Override
329+
# Android, Override included method in bridge
330330
::Appium::Core::Device.add_endpoint_method(:hide_keyboard) do
331331
def hide_keyboard(close_key = nil, strategy = nil)
332332
option = {}
@@ -338,6 +338,13 @@ def hide_keyboard(close_key = nil, strategy = nil)
338338
end
339339
end
340340

341+
# Android, Override included method in bridge
342+
::Appium::Core::Device.add_endpoint_method(:background_app) do
343+
def background_app(duration = 0)
344+
execute :background_app, {}, seconds: duration
345+
end
346+
end
347+
341348
# TODO: TEST ME
342349
::Appium::Core::Device.add_endpoint_method(:end_coverage) do
343350
def end_coverage(path, intent)

lib/appium_lib_core/common/device/app_management.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ def app_strings(language = nil)
2020
execute :app_strings, {}, opts
2121
end
2222

23-
def background_app(duration = 0)
24-
execute :background_app, {}, seconds: duration
23+
def background_app(duration = 0) # rubocop:disable Lint/UnusedMethodArgument
24+
# Should override in each driver
25+
raise NotImplementedError
2526
end
2627

2728
def install_app(path,

lib/appium_lib_core/ios.rb

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
require_relative 'ios/device'
33

44
require_relative 'ios/uiautomation/patch'
5+
require_relative 'ios/uiautomation/device'
56
require_relative 'ios/uiautomation/bridge'

lib/appium_lib_core/ios/uiautomation/bridge.rb

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ def self.for(target)
77
target.extend Appium::Core::Ios::Device
88

99
Core::Ios::Uiautomation.patch_webdriver_element
10+
Core::Ios::Uiautomation::Device.add_methods
1011
end
1112
end
1213
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module Appium
2+
module Core
3+
module Ios
4+
module Uiautomation
5+
module Device
6+
def self.add_methods
7+
# UiAutomation, Override included method in bridge
8+
::Appium::Core::Device.add_endpoint_method(:hide_keyboard) do
9+
def hide_keyboard(close_key = nil, strategy = nil)
10+
option = {}
11+
12+
option[:key] = close_key || 'Done' # default to Done key.
13+
option[:strategy] = strategy || :pressKey # default to pressKey
14+
15+
execute :hide_keyboard, {}, option
16+
end
17+
end
18+
19+
# UiAutomation, Override included method in bridge
20+
::Appium::Core::Device.add_endpoint_method(:background_app) do
21+
def background_app(duration = 0)
22+
execute :background_app, {}, seconds: duration
23+
end
24+
end
25+
end
26+
end # module Device
27+
end # module Uiautomation
28+
end # module Ios
29+
end # module Core
30+
end # module Appium

lib/appium_lib_core/ios/xcuitest/device.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ module Device
150150

151151
class << self
152152
def extended(_mod)
153-
# Override
153+
# Xcuitest, Override included method in bridge
154154
::Appium::Core::Device.add_endpoint_method(:hide_keyboard) do
155155
def hide_keyboard(close_key = nil, strategy = nil)
156156
option = {}
@@ -162,7 +162,7 @@ def hide_keyboard(close_key = nil, strategy = nil)
162162
end
163163
end
164164

165-
# Override
165+
# Xcuitest, Override included method in bridge
166166
::Appium::Core::Device.add_endpoint_method(:background_app) do
167167
def background_app(duration = 0)
168168
# https://github.com/appium/ruby_lib/issues/500, https://github.com/appium/appium/issues/7741

test/test_helper.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def ios
7171
{
7272
caps: { # :desiredCapabilities is also available
7373
platformName: :ios,
74-
automationName: 'XCUITest',
74+
automationName: ENV['AUTOMATION_NAME_IOS'] || 'XCUITest',
7575
app: 'test/functional/app/UICatalog.app.zip',
7676
platformVersion: '11.4',
7777
deviceName: device_name,
@@ -101,7 +101,7 @@ def android(activity_name = nil)
101101
{
102102
desired_capabilities: { # :caps is also available
103103
platformName: :android,
104-
automationName: ENV['AUTOMATION_NAME'] || 'uiautomator2',
104+
automationName: ENV['AUTOMATION_NAME_DROID'] || 'uiautomator2',
105105
app: 'test/functional/app/api.apk.zip',
106106
udid: get_udid_name,
107107
deviceName: 'Android Emulator',
@@ -137,7 +137,7 @@ def android_web
137137
caps: {
138138
browserName: :chrome,
139139
platformName: :android,
140-
automationName: ENV['AUTOMATION_NAME'] || 'uiautomator2',
140+
automationName: ENV['AUTOMATION_NAME_DROID'] || 'uiautomator2',
141141
chromeOptions: { androidPackage: 'com.android.chrome', args: ['--disable-popup-blocking'] },
142142
# refer: https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/web/chromedriver.md
143143
# An emulator 8.1 has Chrome/61.0.3163.98
@@ -212,7 +212,7 @@ def android_mock_create_session
212212
warnings: {},
213213
desired: {
214214
platformName: 'Android',
215-
automationName: 'uiautomator2',
215+
automationName: ENV['AUTOMATION_NAME_DROID'] || 'uiautomator2',
216216
platformVersion: '7.1.1',
217217
deviceName: 'Android Emulator',
218218
app: '/test/apps/ApiDemos-debug.apk',
@@ -221,7 +221,7 @@ def android_mock_create_session
221221
resetKeyboard: true
222222
},
223223
platformName: 'Android',
224-
automationName: 'uiautomator2',
224+
automationName: ENV['AUTOMATION_NAME_DROID'] || 'uiautomator2',
225225
platformVersion: '7.1.1',
226226
deviceName: 'emulator-5554',
227227
app: '/test/apps/ApiDemos-debug.apk',
@@ -260,7 +260,7 @@ def android_mock_create_session_w3c
260260
sessionId: '1234567890',
261261
capabilities: {
262262
platformName: :android,
263-
automationName: 'uiautomator2',
263+
automationName: ENV['AUTOMATION_NAME_DROID'] || 'uiautomator2',
264264
app: 'test/functional/app/api.apk.zip',
265265
platformVersion: '7.1.1',
266266
deviceName: 'Android Emulator',

test/unit/android/device/mjsonwp/app_management_test.rb

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def test_app_strings
6262

6363
def test_background_app
6464
stub_request(:post, "#{SESSION}/appium/app/background")
65+
.with(body: { seconds: 0 }.to_json)
6566
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
6667

6768
@driver.background_app 0

test/unit/android/device/mjsonwp/commands_test.rb

+2
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,8 @@ def test_toggle_location_services
419419
end
420420

421421
def test_get_battery_info
422+
skip('Only uiautomator2 has this method') unless @core.automation_name == :uiautomator2
423+
422424
stub_request(:post, "#{SESSION}/execute")
423425
.with(body: { script: 'mobile: batteryInfo', args: [{}] }.to_json)
424426
.to_return(headers: HEADER, status: 200, body: { value: { state: 2, level: 1.0 } }.to_json)

test/unit/android/device/w3c/app_management_test.rb

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def test_app_strings
5252

5353
def test_background_app
5454
stub_request(:post, "#{SESSION}/appium/app/background")
55+
.with(body: { seconds: 0 }.to_json)
5556
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
5657

5758
@driver.background_app 0

test/unit/android/device/w3c/commands_test.rb

+2
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,8 @@ def test_toggle_location_services
420420
end
421421

422422
def test_get_battery_info
423+
skip('Only uiautomator2 has this method') unless @core.automation_name == :uiautomator2
424+
423425
stub_request(:post, "#{SESSION}/execute/sync")
424426
.with(body: { script: 'mobile: batteryInfo', args: [{}] }.to_json)
425427
.to_return(headers: HEADER, status: 200, body: { value: { state: 5, level: 0.5 } }.to_json)

test/unit/common/websocket_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class AppiumLibCoreTest
55
class WebSocketTest < Minitest::Test
66
def test_connect_websocket
77
ws = ::Appium::Core::WebSocket.new(url: 'ws://localhost:9292')
8-
assert_equal nil, ws.client
8+
assert_nil ws.client
99
end
1010
end
1111
end

test/unit/ios/device/mjsonwp/commands_test.rb

+36
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ def test_toggle_touch_id_enrollment
3535
# Screen recording
3636

3737
def test_start_recording_screen
38+
skip 'Only XCUITest supports' unless @core.automation_name == :xcuitest
39+
3840
stub_request(:post, "#{SESSION}/appium/start_recording_screen")
3941
.with(body: { options: { videoType: 'mjpeg', timeLimit: '180', videoQuality: 'medium' } }.to_json)
4042
.to_return(headers: HEADER, status: 200, body: { value: ['a'] }.to_json)
@@ -45,6 +47,8 @@ def test_start_recording_screen
4547
end
4648

4749
def test_start_recording_screen_custom
50+
skip 'Only XCUITest supports' unless @core.automation_name == :xcuitest
51+
4852
stub_request(:post, "#{SESSION}/appium/start_recording_screen")
4953
.with(body: { options: {
5054
videoType: 'libx264', timeLimit: '60', videoQuality: 'medium', videoScale: '320:240'
@@ -57,6 +61,8 @@ def test_start_recording_screen_custom
5761
end
5862

5963
def test_start_recording_screen_custom_force
64+
skip 'Only XCUITest supports' unless @core.automation_name == :xcuitest
65+
6066
stub_request(:post, "#{SESSION}/appium/start_recording_screen")
6167
.with(body:
6268
{ options: { forceRestart: true, videoType: 'libx264', timeLimit: '60', videoQuality: 'medium' } }.to_json)
@@ -68,6 +74,8 @@ def test_start_recording_screen_custom_force
6874
end
6975

7076
def test_stop_recording_screen_default
77+
skip 'Only XCUITest supports' unless @core.automation_name == :xcuitest
78+
7179
stub_request(:post, "#{SESSION}/appium/stop_recording_screen")
7280
.with(body: {}.to_json)
7381
.to_return(headers: HEADER, status: 200, body: { value: ['a'] }.to_json)
@@ -89,6 +97,8 @@ def test_stop_recording_screen_custom
8997
end
9098

9199
def test_get_battery_info
100+
skip 'Only XCUITest supports' unless @core.automation_name == :xcuitest
101+
92102
stub_request(:post, "#{SESSION}/execute")
93103
.with(body: { script: 'mobile: batteryInfo', args: [{}] }.to_json)
94104
.to_return(headers: HEADER, status: 200, body: { value: { state: 3, level: 1.0 } }.to_json)
@@ -99,6 +109,32 @@ def test_get_battery_info
99109
assert_equal :full, info[:state]
100110
assert_equal 1.0, info[:level]
101111
end
112+
113+
def test_method_missing
114+
stub_request(:get, "#{SESSION}/element/id/attribute/name")
115+
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
116+
117+
e = ::Selenium::WebDriver::Element.new(@driver.send(:bridge), 'id')
118+
e.name
119+
120+
assert_requested(:get, "#{SESSION}/element/id/attribute/name", times: 1)
121+
end
122+
123+
def test_background_app
124+
if @core.automation_name == :xcuitest
125+
stub_request(:post, "#{SESSION}/appium/app/background")
126+
.with(body: { seconds: { timeout: 0 } }.to_json)
127+
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
128+
else
129+
stub_request(:post, "#{SESSION}/appium/app/background")
130+
.with(body: { seconds: 0 }.to_json)
131+
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
132+
end
133+
134+
@driver.background_app 0
135+
136+
assert_requested(:post, "#{SESSION}/appium/app/background", times: 1)
137+
end
102138
end # class CommandsTest
103139
end # module MJSONWP
104140
end # module Device

test/unit/ios/device/w3c/commands_test.rb

+26
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ def test_toggle_touch_id_enrollment
3333
end
3434

3535
def test_start_recording_screen
36+
skip 'Only XCUITest supports' unless @core.automation_name == :xcuitest
37+
3638
stub_request(:post, "#{SESSION}/appium/start_recording_screen")
3739
.with(body: { options: { videoType: 'mjpeg', timeLimit: '180', videoQuality: 'medium' } }.to_json)
3840
.to_return(headers: HEADER, status: 200, body: { value: ['a'] }.to_json)
@@ -43,6 +45,8 @@ def test_start_recording_screen
4345
end
4446

4547
def test_start_recording_screen_custom
48+
skip 'Only XCUITest supports' unless @core.automation_name == :xcuitest
49+
4650
stub_request(:post, "#{SESSION}/appium/start_recording_screen")
4751
.with(body: { options: {
4852
videoType: 'libx264', timeLimit: '60', videoQuality: 'medium', videoFps: '50', videoScale: '320:240'
@@ -55,6 +59,8 @@ def test_start_recording_screen_custom
5559
end
5660

5761
def test_stop_recording_screen_default
62+
skip 'Only XCUITest supports' unless @core.automation_name == :xcuitest
63+
5864
stub_request(:post, "#{SESSION}/appium/stop_recording_screen")
5965
.with(body: {}.to_json)
6066
.to_return(headers: HEADER, status: 200, body: { value: ['a'] }.to_json)
@@ -65,6 +71,8 @@ def test_stop_recording_screen_default
6571
end
6672

6773
def test_stop_recording_screen_custom
74+
skip 'Only XCUITest supports' unless @core.automation_name == :xcuitest
75+
6876
stub_request(:post, "#{SESSION}/appium/stop_recording_screen")
6977
.with(body: { options:
7078
{ remotePath: 'https://example.com', user: 'user name', pass: 'pass', method: 'PUT' } }.to_json)
@@ -76,6 +84,8 @@ def test_stop_recording_screen_custom
7684
end
7785

7886
def test_get_battery_info
87+
skip 'Only XCUITest supports' unless @core.automation_name == :xcuitest
88+
7989
stub_request(:post, "#{SESSION}/execute/sync")
8090
.with(body: { script: 'mobile: batteryInfo', args: [{}] }.to_json)
8191
.to_return(headers: HEADER, status: 200, body: { value: { state: 1, level: 0.5 } }.to_json)
@@ -96,6 +106,22 @@ def test_method_missing
96106

97107
assert_requested(:get, "#{SESSION}/element/id/attribute/name", times: 1)
98108
end
109+
110+
def test_background_app
111+
if @core.automation_name == :xcuitest
112+
stub_request(:post, "#{SESSION}/appium/app/background")
113+
.with(body: { seconds: { timeout: 0 } }.to_json)
114+
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
115+
else
116+
stub_request(:post, "#{SESSION}/appium/app/background")
117+
.with(body: { seconds: 0 }.to_json)
118+
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
119+
end
120+
121+
@driver.background_app 0
122+
123+
assert_requested(:post, "#{SESSION}/appium/app/background", times: 1)
124+
end
99125
end # class CommandsTest
100126
end # module W3C
101127
end # module Device

0 commit comments

Comments
 (0)