Skip to content

Commit 1f93d58

Browse files
authored
Add some emulator commands (#60)
* add android emulator commands * define consts * add commands in changelog * add comments and test for device
1 parent 341b936 commit 1f93d58

File tree

10 files changed

+325
-8
lines changed

10 files changed

+325
-8
lines changed

.rubocop.yml

+2
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ Lint/NestedMethodDefinition:
2626
Enabled: false
2727
Naming/AccessorMethodName:
2828
Enabled: false
29+
Style/SymbolArray:
30+
Enabled: false

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ All notable changes to this project will be documented in this file.
33

44
## [Unreleased]
55
### Enhancements
6+
- Add Android emulator commands
7+
- `send_sms`, `gsm_call`, `gsm_signal`, `gsm_voice`, `set_network_speed`, `set_power_capacity`, `set_power_ac`
68

79
### Bug fixes
810

lib/appium_lib_core/android/device.rb

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require_relative 'device/emulator'
2+
13
module Appium
24
module Android
35
module Device
@@ -175,6 +177,7 @@ def get_performance_data(package_name:, data_type:, data_read_timeout: 1000)
175177
end
176178

177179
add_screen_recording
180+
Emulator.emulator_commands
178181
end
179182

180183
private
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
module Appium
2+
module Android
3+
module Device
4+
module Emulator
5+
GSM_CALL_ACTIONS = [:call, :accept, :cancel, :hold].freeze
6+
7+
GSM_VOICE_STATES = [:on, :off, :denied, :searching, :roaming, :home, :unregistered].freeze
8+
9+
GSM_SIGNALS = { none_or_unknown: 0, poor: 1, moderate: 2, good: 3, great: 4 }.freeze
10+
11+
# :gsm // GSM/CSD (up: 14.4, down: 14.4).
12+
# :scsd // HSCSD (up: 14.4, down: 57.6).
13+
# :gprs // GPRS (up: 28.8, down: 57.6).
14+
# :edge // EDGE/EGPRS (up: 473.6, down: 473.6).
15+
# :umts // UMTS/3G (up: 384.0, down: 384.0).
16+
# :hsdpa // HSDPA (up: 5760.0, down: 13,980.0).
17+
# :lte // LTE (up: 58,000, down: 173,000).
18+
# :evdo // EVDO (up: 75,000, down: 280,000).
19+
# :full // No limit, the default (up: 0.0, down: 0.0).
20+
NET_SPEED = [:gsm, :scsd, :gprs, :edge, :umts, :hsdpa, :lte, :evdo, :full].freeze
21+
22+
POWER_AC_STATE = [:on, :off].freeze
23+
24+
# @!method send_sms(phone_number:, message:)
25+
# Emulate send SMS event on the connected emulator.
26+
#
27+
# @param [String] phone_number: The phone number of message sender
28+
# @param [String] message: The message to send
29+
#
30+
# @example
31+
#
32+
# @driver.send_sms phone_number: '00000000000', message: 'test message'
33+
#
34+
35+
# @!method gsm_call(phone_number:, action:)
36+
# Emulate GSM call event on the connected emulator.
37+
#
38+
# @param [String] phone_number: The phone number of message sender
39+
# @param [Hash] action: One of available GSM call actions. Available action is GSM_CALL_ACTION.
40+
#
41+
# @example
42+
#
43+
# @driver.gsm_call phone_number: '00000000000', action: :call
44+
#
45+
46+
# @!method gsm_signal(signal_strength)
47+
# Emulate GSM signal strength change event on the connected emulator.
48+
#
49+
# @param [Hash] signal_strength One of available GSM signal strength. Available action is GSM_SIGNAL.
50+
#
51+
# @example
52+
#
53+
# @driver.gsm_signal :good
54+
#
55+
56+
# @!method gsm_voice(state)
57+
# Emulate GSM voice event on the connected emulator.
58+
#
59+
# @param [Hash] state One of available GSM voice state. Available action is GSM_VOICE_STATE.
60+
#
61+
# @example
62+
#
63+
# @driver.gsm_voice :on
64+
#
65+
66+
# @!method set_network_speed(netspeed)
67+
# Emulate network speed change event on the connected emulator.
68+
#
69+
# @param [Hash] netspeed One of available Network Speed values. Available action is NET_SPEED.
70+
#
71+
# @example
72+
#
73+
# @driver.set_network_speed :gsm
74+
#
75+
76+
# @!method set_power_capacity(percent)
77+
# Emulate power capacity change on the connected emulator.
78+
#
79+
# @param [Integer] percent Percentage value in range [0, 100].
80+
#
81+
# @example
82+
#
83+
# @driver.set_power_capacity 10
84+
#
85+
86+
# @!method set_power_ac(state)
87+
# Emulate power state change on the connected emulator.
88+
#
89+
# @param [Hash] state One of available power AC state. Available action is POWER_AC_STATE.
90+
#
91+
# @example
92+
#
93+
# @driver.set_power_ac :on
94+
#
95+
def self.emulator_commands
96+
Appium::Core::Device.add_endpoint_method(:send_sms) do
97+
def send_sms(phone_number:, message:)
98+
execute(:send_sms, {}, { phoneNumber: phone_number, message: message })
99+
end
100+
end
101+
102+
Appium::Core::Device.add_endpoint_method(:gsm_call) do
103+
def gsm_call(phone_number:, action:)
104+
unless GSM_CALL_ACTIONS.member? action.to_sym
105+
raise "action: should be member of #{GSM_CALL_ACTIONS}. Not #{action}."
106+
end
107+
108+
execute(:gsm_call, {}, { phoneNumber: phone_number, action: action })
109+
end
110+
end
111+
112+
Appium::Core::Device.add_endpoint_method(:gsm_signal) do
113+
def gsm_signal(signal_strength)
114+
raise "#{signal_strength} should be member of #{GSM_SIGNALS} " if GSM_SIGNALS[signal_strength.to_sym].nil?
115+
116+
execute(:gsm_signal, {}, { signalStrengh: GSM_SIGNALS[signal_strength] })
117+
end
118+
end
119+
120+
Appium::Core::Device.add_endpoint_method(:gsm_voice) do
121+
def gsm_voice(state)
122+
unless GSM_VOICE_STATES.member? state.to_sym
123+
raise "The state should be member of #{GSM_VOICE_STATES}. Not #{state}."
124+
end
125+
126+
execute(:gsm_voice, {}, { state: state })
127+
end
128+
end
129+
130+
Appium::Core::Device.add_endpoint_method(:set_network_speed) do
131+
def set_network_speed(netspeed)
132+
unless NET_SPEED.member? netspeed.to_sym
133+
raise "The netspeed should be member of #{NET_SPEED}. Not #{netspeed}."
134+
end
135+
136+
execute(:set_network_speed, {}, { netspeed: netspeed })
137+
end
138+
end
139+
140+
Appium::Core::Device.add_endpoint_method(:set_power_capacity) do
141+
def set_power_capacity(percent)
142+
unless (0..100).member? percent
143+
raise "The percent should be between 0 and 100. Not #{percent}."
144+
end
145+
146+
execute(:set_power_capacity, {}, { percent: percent })
147+
end
148+
end
149+
150+
Appium::Core::Device.add_endpoint_method(:set_power_ac) do
151+
def set_power_ac(state)
152+
unless POWER_AC_STATE.member? state.to_sym
153+
raise "The state should be member of #{POWER_AC_STATE}. Not #{state}."
154+
end
155+
156+
execute(:set_power_ac, {}, { state: state })
157+
end
158+
end
159+
end # def self.emulator_commands
160+
end # module Emulator
161+
end # module Device
162+
end # module Android
163+
end # module Appium

lib/appium_lib_core/common/command.rb

+10-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,16 @@ module Commands
6464
start_activity: [:post, 'session/:session_id/appium/device/start_activity'.freeze],
6565
end_coverage: [:post, 'session/:session_id/appium/app/end_test_coverage'.freeze],
6666
set_network_connection: [:post, 'session/:session_id/network_connection'.freeze], # defined also in OSS
67-
get_performance_data: [:post, 'session/:session_id/appium/getPerformanceData'.freeze]
67+
get_performance_data: [:post, 'session/:session_id/appium/getPerformanceData'.freeze],
68+
69+
# only emulator
70+
send_sms: [:post, 'session/:session_id/appium/device/send_sms'.freeze],
71+
gsm_call: [:post, 'session/:session_id/appium/device/gsm_call'.freeze],
72+
gsm_signal: [:post, 'session/:session_id/appium/device/gsm_signal'.freeze],
73+
gsm_voice: [:post, 'session/:session_id/appium/device/gsm_voice'.freeze],
74+
set_network_speed: [:post, 'session/:session_id/appium/device/network_speed'.freeze],
75+
set_power_capacity: [:post, 'session/:session_id/appium/device/power_capacity'.freeze],
76+
set_power_ac: [:post, 'session/:session_id/appium/device/power_ac'.freeze]
6877
}.freeze
6978

7079
COMMAND_IOS = {

test/unit/android/device_test.rb

+71-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
require 'webmock/minitest'
33

44
# $ rake test:unit TEST=test/unit/android/device_test.rb
5-
# rubocop:disable Style/SymbolArray
65
class AppiumLibCoreTest
76
module Android
87
class DeviceTest < Minitest::Test
@@ -589,6 +588,77 @@ def test_stop_recording_screen_custom
589588

590589
assert_requested(:post, "#{SESSION}/appium/stop_recording_screen", times: 1)
591590
end
591+
592+
# emulator
593+
def test_send_sms
594+
stub_request(:post, "#{SESSION}/appium/device/send_sms")
595+
.with(body: { phoneNumber: '00000000000', message: 'test message' }.to_json)
596+
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
597+
598+
@driver.send_sms phone_number: '00000000000', message: 'test message'
599+
600+
assert_requested(:post, "#{SESSION}/appium/device/send_sms", times: 1)
601+
end
602+
603+
def test_gsm_call
604+
stub_request(:post, "#{SESSION}/appium/device/gsm_call")
605+
.with(body: { phoneNumber: '00000000000', action: 'call' }.to_json)
606+
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
607+
608+
@driver.gsm_call phone_number: '00000000000', action: :call
609+
610+
assert_requested(:post, "#{SESSION}/appium/device/gsm_call", times: 1)
611+
end
612+
613+
def test_gsm_signal
614+
stub_request(:post, "#{SESSION}/appium/device/gsm_signal")
615+
.with(body: { signalStrengh: ::Appium::Android::Device::Emulator::GSM_SIGNALS[:good] }.to_json)
616+
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
617+
618+
@driver.gsm_signal :good
619+
620+
assert_requested(:post, "#{SESSION}/appium/device/gsm_signal", times: 1)
621+
end
622+
623+
def test_gsm_voice
624+
stub_request(:post, "#{SESSION}/appium/device/gsm_voice")
625+
.with(body: { state: 'on' }.to_json)
626+
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
627+
628+
@driver.gsm_voice :on
629+
630+
assert_requested(:post, "#{SESSION}/appium/device/gsm_voice", times: 1)
631+
end
632+
633+
def test_network_speed
634+
stub_request(:post, "#{SESSION}/appium/device/network_speed")
635+
.with(body: { netspeed: 'gsm' }.to_json)
636+
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
637+
638+
@driver.set_network_speed :gsm
639+
640+
assert_requested(:post, "#{SESSION}/appium/device/network_speed", times: 1)
641+
end
642+
643+
def test_set_power_capacity
644+
stub_request(:post, "#{SESSION}/appium/device/power_capacity")
645+
.with(body: { percent: 10 }.to_json)
646+
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
647+
648+
@driver.set_power_capacity 10
649+
650+
assert_requested(:post, "#{SESSION}/appium/device/power_capacity", times: 1)
651+
end
652+
653+
def test_power_ac
654+
stub_request(:post, "#{SESSION}/appium/device/power_ac")
655+
.with(body: { state: 'on' }.to_json)
656+
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
657+
658+
@driver.set_power_ac :on
659+
660+
assert_requested(:post, "#{SESSION}/appium/device/power_ac", times: 1)
661+
end
592662
end
593663
end
594664
end

test/unit/android/device_w3c_test.rb

+71-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
require 'webmock/minitest'
33

44
# $ rake test:unit TEST=test/unit/android/device_test.rb
5-
# rubocop:disable Style/SymbolArray
65
class AppiumLibCoreTest
76
module Android
87
class DeviceW3CTest < Minitest::Test
@@ -579,6 +578,77 @@ def test_stop_recording_screen_custom
579578

580579
assert_requested(:post, "#{SESSION}/appium/stop_recording_screen", times: 1)
581580
end
581+
582+
# emulator
583+
def test_send_sms
584+
stub_request(:post, "#{SESSION}/appium/device/send_sms")
585+
.with(body: { phoneNumber: '00000000000', message: 'test message' }.to_json)
586+
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
587+
588+
@driver.send_sms phone_number: '00000000000', message: 'test message'
589+
590+
assert_requested(:post, "#{SESSION}/appium/device/send_sms", times: 1)
591+
end
592+
593+
def test_gsm_call
594+
stub_request(:post, "#{SESSION}/appium/device/gsm_call")
595+
.with(body: { phoneNumber: '00000000000', action: 'call' }.to_json)
596+
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
597+
598+
@driver.gsm_call phone_number: '00000000000', action: :call
599+
600+
assert_requested(:post, "#{SESSION}/appium/device/gsm_call", times: 1)
601+
end
602+
603+
def test_gsm_signal
604+
stub_request(:post, "#{SESSION}/appium/device/gsm_signal")
605+
.with(body: { signalStrengh: ::Appium::Android::Device::Emulator::GSM_SIGNALS[:good] }.to_json)
606+
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
607+
608+
@driver.gsm_signal :good
609+
610+
assert_requested(:post, "#{SESSION}/appium/device/gsm_signal", times: 1)
611+
end
612+
613+
def test_gsm_voice
614+
stub_request(:post, "#{SESSION}/appium/device/gsm_voice")
615+
.with(body: { state: 'on' }.to_json)
616+
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
617+
618+
@driver.gsm_voice :on
619+
620+
assert_requested(:post, "#{SESSION}/appium/device/gsm_voice", times: 1)
621+
end
622+
623+
def test_network_speed
624+
stub_request(:post, "#{SESSION}/appium/device/network_speed")
625+
.with(body: { netspeed: 'gsm' }.to_json)
626+
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
627+
628+
@driver.set_network_speed :gsm
629+
630+
assert_requested(:post, "#{SESSION}/appium/device/network_speed", times: 1)
631+
end
632+
633+
def test_set_power_capacity
634+
stub_request(:post, "#{SESSION}/appium/device/power_capacity")
635+
.with(body: { percent: 10 }.to_json)
636+
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
637+
638+
@driver.set_power_capacity 10
639+
640+
assert_requested(:post, "#{SESSION}/appium/device/power_capacity", times: 1)
641+
end
642+
643+
def test_power_ac
644+
stub_request(:post, "#{SESSION}/appium/device/power_ac")
645+
.with(body: { state: 'on' }.to_json)
646+
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
647+
648+
@driver.set_power_ac :on
649+
650+
assert_requested(:post, "#{SESSION}/appium/device/power_ac", times: 1)
651+
end
582652
end
583653
end
584654
end

test/unit/ios/device_test.rb

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
require 'webmock/minitest'
33

44
# $ rake test:unit TEST=test/unit/ios/device_test.rb
5-
# rubocop:disable Style/SymbolArray
65
class AppiumLibCoreTest
76
module IOS
87
class DeviceTest < Minitest::Test

test/unit/ios/device_w3c_test.rb

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
require 'webmock/minitest'
33

44
# $ rake test:unit TEST=test/unit/ios/device_test.rb
5-
# rubocop:disable Style/SymbolArray
65
class AppiumLibCoreTest
76
module IOS
87
class DeviceW3CTest < Minitest::Test

0 commit comments

Comments
 (0)