Skip to content

Commit

Permalink
Improve override background (#188)
Browse files Browse the repository at this point in the history
* improve included, extended methods

* update changelog

* add tests for espresso

* add tests for appium automation name for android and ios
  • Loading branch information
KazuCocoa authored Feb 1, 2019
1 parent 1e588c8 commit c9fff7b
Show file tree
Hide file tree
Showing 16 changed files with 123 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ before_install:
script:
- bundle exec rake rubocop
- bundle exec parallel_test test/unit/ -n 4
- AUTOMATION_NAME_DROID=espresso bundle exec parallel_test test/unit/android -n 4
- AUTOMATION_NAME_DROID=appium AUTOMATION_NAME_IOS=appium bundle exec parallel_test test/unit -n 4

notifications:
email:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Read `release_notes.md` for commit level details.
### Enhancements

### Bug fixes
- Fix potential override of `AppManagement#background_app`

### Deprecations

Expand Down
9 changes: 8 additions & 1 deletion lib/appium_lib_core/android/device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ def start_activity(opts)
end
end

# Android, Override
# Android, Override included method in bridge
::Appium::Core::Device.add_endpoint_method(:hide_keyboard) do
def hide_keyboard(close_key = nil, strategy = nil)
option = {}
Expand All @@ -338,6 +338,13 @@ def hide_keyboard(close_key = nil, strategy = nil)
end
end

# Android, Override included method in bridge
::Appium::Core::Device.add_endpoint_method(:background_app) do
def background_app(duration = 0)
execute :background_app, {}, seconds: duration
end
end

# TODO: TEST ME
::Appium::Core::Device.add_endpoint_method(:end_coverage) do
def end_coverage(path, intent)
Expand Down
5 changes: 3 additions & 2 deletions lib/appium_lib_core/common/device/app_management.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ def app_strings(language = nil)
execute :app_strings, {}, opts
end

def background_app(duration = 0)
execute :background_app, {}, seconds: duration
def background_app(duration = 0) # rubocop:disable Lint/UnusedMethodArgument
# Should override in each driver
raise NotImplementedError
end

def install_app(path,
Expand Down
1 change: 1 addition & 0 deletions lib/appium_lib_core/ios.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
require_relative 'ios/device'

require_relative 'ios/uiautomation/patch'
require_relative 'ios/uiautomation/device'
require_relative 'ios/uiautomation/bridge'
1 change: 1 addition & 0 deletions lib/appium_lib_core/ios/uiautomation/bridge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def self.for(target)
target.extend Appium::Core::Ios::Device

Core::Ios::Uiautomation.patch_webdriver_element
Core::Ios::Uiautomation::Device.add_methods
end
end
end
Expand Down
30 changes: 30 additions & 0 deletions lib/appium_lib_core/ios/uiautomation/device.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Appium
module Core
module Ios
module Uiautomation
module Device
def self.add_methods
# UiAutomation, Override included method in bridge
::Appium::Core::Device.add_endpoint_method(:hide_keyboard) do
def hide_keyboard(close_key = nil, strategy = nil)
option = {}

option[:key] = close_key || 'Done' # default to Done key.
option[:strategy] = strategy || :pressKey # default to pressKey

execute :hide_keyboard, {}, option
end
end

# UiAutomation, Override included method in bridge
::Appium::Core::Device.add_endpoint_method(:background_app) do
def background_app(duration = 0)
execute :background_app, {}, seconds: duration
end
end
end
end # module Device
end # module Uiautomation
end # module Ios
end # module Core
end # module Appium
4 changes: 2 additions & 2 deletions lib/appium_lib_core/ios/xcuitest/device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ module Device

class << self
def extended(_mod)
# Override
# Xcuitest, Override included method in bridge
::Appium::Core::Device.add_endpoint_method(:hide_keyboard) do
def hide_keyboard(close_key = nil, strategy = nil)
option = {}
Expand All @@ -162,7 +162,7 @@ def hide_keyboard(close_key = nil, strategy = nil)
end
end

# Override
# Xcuitest, Override included method in bridge
::Appium::Core::Device.add_endpoint_method(:background_app) do
def background_app(duration = 0)
# https://github.com/appium/ruby_lib/issues/500, https://github.com/appium/appium/issues/7741
Expand Down
12 changes: 6 additions & 6 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def ios
{
caps: { # :desiredCapabilities is also available
platformName: :ios,
automationName: 'XCUITest',
automationName: ENV['AUTOMATION_NAME_IOS'] || 'XCUITest',
app: 'test/functional/app/UICatalog.app.zip',
platformVersion: '11.4',
deviceName: device_name,
Expand Down Expand Up @@ -101,7 +101,7 @@ def android(activity_name = nil)
{
desired_capabilities: { # :caps is also available
platformName: :android,
automationName: ENV['AUTOMATION_NAME'] || 'uiautomator2',
automationName: ENV['AUTOMATION_NAME_DROID'] || 'uiautomator2',
app: 'test/functional/app/api.apk.zip',
udid: get_udid_name,
deviceName: 'Android Emulator',
Expand Down Expand Up @@ -137,7 +137,7 @@ def android_web
caps: {
browserName: :chrome,
platformName: :android,
automationName: ENV['AUTOMATION_NAME'] || 'uiautomator2',
automationName: ENV['AUTOMATION_NAME_DROID'] || 'uiautomator2',
chromeOptions: { androidPackage: 'com.android.chrome', args: ['--disable-popup-blocking'] },
# refer: https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/web/chromedriver.md
# An emulator 8.1 has Chrome/61.0.3163.98
Expand Down Expand Up @@ -212,7 +212,7 @@ def android_mock_create_session
warnings: {},
desired: {
platformName: 'Android',
automationName: 'uiautomator2',
automationName: ENV['AUTOMATION_NAME_DROID'] || 'uiautomator2',
platformVersion: '7.1.1',
deviceName: 'Android Emulator',
app: '/test/apps/ApiDemos-debug.apk',
Expand All @@ -221,7 +221,7 @@ def android_mock_create_session
resetKeyboard: true
},
platformName: 'Android',
automationName: 'uiautomator2',
automationName: ENV['AUTOMATION_NAME_DROID'] || 'uiautomator2',
platformVersion: '7.1.1',
deviceName: 'emulator-5554',
app: '/test/apps/ApiDemos-debug.apk',
Expand Down Expand Up @@ -260,7 +260,7 @@ def android_mock_create_session_w3c
sessionId: '1234567890',
capabilities: {
platformName: :android,
automationName: 'uiautomator2',
automationName: ENV['AUTOMATION_NAME_DROID'] || 'uiautomator2',
app: 'test/functional/app/api.apk.zip',
platformVersion: '7.1.1',
deviceName: 'Android Emulator',
Expand Down
1 change: 1 addition & 0 deletions test/unit/android/device/mjsonwp/app_management_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def test_app_strings

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

@driver.background_app 0
Expand Down
2 changes: 2 additions & 0 deletions test/unit/android/device/mjsonwp/commands_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,8 @@ def test_toggle_location_services
end

def test_get_battery_info
skip('Only uiautomator2 has this method') unless @core.automation_name == :uiautomator2

stub_request(:post, "#{SESSION}/execute")
.with(body: { script: 'mobile: batteryInfo', args: [{}] }.to_json)
.to_return(headers: HEADER, status: 200, body: { value: { state: 2, level: 1.0 } }.to_json)
Expand Down
1 change: 1 addition & 0 deletions test/unit/android/device/w3c/app_management_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def test_app_strings

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

@driver.background_app 0
Expand Down
2 changes: 2 additions & 0 deletions test/unit/android/device/w3c/commands_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,8 @@ def test_toggle_location_services
end

def test_get_battery_info
skip('Only uiautomator2 has this method') unless @core.automation_name == :uiautomator2

stub_request(:post, "#{SESSION}/execute/sync")
.with(body: { script: 'mobile: batteryInfo', args: [{}] }.to_json)
.to_return(headers: HEADER, status: 200, body: { value: { state: 5, level: 0.5 } }.to_json)
Expand Down
2 changes: 1 addition & 1 deletion test/unit/common/websocket_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class AppiumLibCoreTest
class WebSocketTest < Minitest::Test
def test_connect_websocket
ws = ::Appium::Core::WebSocket.new(url: 'ws://localhost:9292')
assert_equal nil, ws.client
assert_nil ws.client
end
end
end
36 changes: 36 additions & 0 deletions test/unit/ios/device/mjsonwp/commands_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def test_toggle_touch_id_enrollment
# Screen recording

def test_start_recording_screen
skip 'Only XCUITest supports' unless @core.automation_name == :xcuitest

stub_request(:post, "#{SESSION}/appium/start_recording_screen")
.with(body: { options: { videoType: 'mjpeg', timeLimit: '180', videoQuality: 'medium' } }.to_json)
.to_return(headers: HEADER, status: 200, body: { value: ['a'] }.to_json)
Expand All @@ -45,6 +47,8 @@ def test_start_recording_screen
end

def test_start_recording_screen_custom
skip 'Only XCUITest supports' unless @core.automation_name == :xcuitest

stub_request(:post, "#{SESSION}/appium/start_recording_screen")
.with(body: { options: {
videoType: 'libx264', timeLimit: '60', videoQuality: 'medium', videoScale: '320:240'
Expand All @@ -57,6 +61,8 @@ def test_start_recording_screen_custom
end

def test_start_recording_screen_custom_force
skip 'Only XCUITest supports' unless @core.automation_name == :xcuitest

stub_request(:post, "#{SESSION}/appium/start_recording_screen")
.with(body:
{ options: { forceRestart: true, videoType: 'libx264', timeLimit: '60', videoQuality: 'medium' } }.to_json)
Expand All @@ -68,6 +74,8 @@ def test_start_recording_screen_custom_force
end

def test_stop_recording_screen_default
skip 'Only XCUITest supports' unless @core.automation_name == :xcuitest

stub_request(:post, "#{SESSION}/appium/stop_recording_screen")
.with(body: {}.to_json)
.to_return(headers: HEADER, status: 200, body: { value: ['a'] }.to_json)
Expand All @@ -89,6 +97,8 @@ def test_stop_recording_screen_custom
end

def test_get_battery_info
skip 'Only XCUITest supports' unless @core.automation_name == :xcuitest

stub_request(:post, "#{SESSION}/execute")
.with(body: { script: 'mobile: batteryInfo', args: [{}] }.to_json)
.to_return(headers: HEADER, status: 200, body: { value: { state: 3, level: 1.0 } }.to_json)
Expand All @@ -99,6 +109,32 @@ def test_get_battery_info
assert_equal :full, info[:state]
assert_equal 1.0, info[:level]
end

def test_method_missing
stub_request(:get, "#{SESSION}/element/id/attribute/name")
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)

e = ::Selenium::WebDriver::Element.new(@driver.send(:bridge), 'id')
e.name

assert_requested(:get, "#{SESSION}/element/id/attribute/name", times: 1)
end

def test_background_app
if @core.automation_name == :xcuitest
stub_request(:post, "#{SESSION}/appium/app/background")
.with(body: { seconds: { timeout: 0 } }.to_json)
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
else
stub_request(:post, "#{SESSION}/appium/app/background")
.with(body: { seconds: 0 }.to_json)
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
end

@driver.background_app 0

assert_requested(:post, "#{SESSION}/appium/app/background", times: 1)
end
end # class CommandsTest
end # module MJSONWP
end # module Device
Expand Down
26 changes: 26 additions & 0 deletions test/unit/ios/device/w3c/commands_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def test_toggle_touch_id_enrollment
end

def test_start_recording_screen
skip 'Only XCUITest supports' unless @core.automation_name == :xcuitest

stub_request(:post, "#{SESSION}/appium/start_recording_screen")
.with(body: { options: { videoType: 'mjpeg', timeLimit: '180', videoQuality: 'medium' } }.to_json)
.to_return(headers: HEADER, status: 200, body: { value: ['a'] }.to_json)
Expand All @@ -43,6 +45,8 @@ def test_start_recording_screen
end

def test_start_recording_screen_custom
skip 'Only XCUITest supports' unless @core.automation_name == :xcuitest

stub_request(:post, "#{SESSION}/appium/start_recording_screen")
.with(body: { options: {
videoType: 'libx264', timeLimit: '60', videoQuality: 'medium', videoFps: '50', videoScale: '320:240'
Expand All @@ -55,6 +59,8 @@ def test_start_recording_screen_custom
end

def test_stop_recording_screen_default
skip 'Only XCUITest supports' unless @core.automation_name == :xcuitest

stub_request(:post, "#{SESSION}/appium/stop_recording_screen")
.with(body: {}.to_json)
.to_return(headers: HEADER, status: 200, body: { value: ['a'] }.to_json)
Expand All @@ -65,6 +71,8 @@ def test_stop_recording_screen_default
end

def test_stop_recording_screen_custom
skip 'Only XCUITest supports' unless @core.automation_name == :xcuitest

stub_request(:post, "#{SESSION}/appium/stop_recording_screen")
.with(body: { options:
{ remotePath: 'https://example.com', user: 'user name', pass: 'pass', method: 'PUT' } }.to_json)
Expand All @@ -76,6 +84,8 @@ def test_stop_recording_screen_custom
end

def test_get_battery_info
skip 'Only XCUITest supports' unless @core.automation_name == :xcuitest

stub_request(:post, "#{SESSION}/execute/sync")
.with(body: { script: 'mobile: batteryInfo', args: [{}] }.to_json)
.to_return(headers: HEADER, status: 200, body: { value: { state: 1, level: 0.5 } }.to_json)
Expand All @@ -96,6 +106,22 @@ def test_method_missing

assert_requested(:get, "#{SESSION}/element/id/attribute/name", times: 1)
end

def test_background_app
if @core.automation_name == :xcuitest
stub_request(:post, "#{SESSION}/appium/app/background")
.with(body: { seconds: { timeout: 0 } }.to_json)
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
else
stub_request(:post, "#{SESSION}/appium/app/background")
.with(body: { seconds: 0 }.to_json)
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
end

@driver.background_app 0

assert_requested(:post, "#{SESSION}/appium/app/background", times: 1)
end
end # class CommandsTest
end # module W3C
end # module Device
Expand Down

0 comments on commit c9fff7b

Please sign in to comment.