Skip to content

Commit 8f6bca9

Browse files
authored
feat: raise its own defined errors (#564)
* feat: raise its own defined errors * set java 17 for espresso * set JAVA_HOME * set jvmTarget * modify * move the timing to refer to javahome * cleanup
1 parent 8cc7388 commit 8f6bca9

File tree

13 files changed

+60
-26
lines changed

13 files changed

+60
-26
lines changed

.github/workflows/functional-test.yml

+5
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ jobs:
192192
steps:
193193
- uses: actions/checkout@v3
194194

195+
- uses: actions/setup-java@v3
196+
with:
197+
distribution: 'temurin'
198+
java-version: '17'
199+
195200
- name: Install Node.js
196201
uses: actions/setup-node@v3
197202
with:

lib/appium_lib_core/android/device.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -417,15 +417,15 @@ def toggle_location_services
417417
def start_activity(opts)
418418
::Appium::Logger.warn "[DEPRECATION] Please use 'mobile: startActivity' extension instead"
419419

420-
raise 'opts must be a hash' unless opts.is_a? Hash
420+
raise ::Appium::Core::Error::ArgumentError, 'opts must be a hash' unless opts.is_a? Hash
421421

422422
option = {}
423423

424424
app_package = opts[:app_package]
425-
raise 'app_package is required' unless app_package
425+
raise ::Appium::Core::Error::ArgumentError, 'app_package is required' unless app_package
426426

427427
app_activity = opts[:app_activity]
428-
raise 'app_activity is required' unless app_activity
428+
raise ::Appium::Core::Error::ArgumentError, 'app_activity is required' unless app_activity
429429

430430
option[:appPackage] = app_package
431431
option[:appActivity] = app_activity

lib/appium_lib_core/android/device/clipboard.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ def self.add_methods
2323
::Appium::Core::Device.add_endpoint_method(:get_clipboard) do
2424
def get_clipboard(content_type: :plaintext)
2525
unless ::Appium::Core::Base::Device::Clipboard::CONTENT_TYPE.member?(content_type)
26-
raise "content_type should be #{::Appium::Core::Base::Device::Clipboard::CONTENT_TYPE}"
26+
raise ::Appium::Core::Error::ArgumentError,
27+
"content_type should be #{::Appium::Core::Base::Device::Clipboard::CONTENT_TYPE}"
2728
end
2829

2930
params = { contentType: content_type }
@@ -36,7 +37,8 @@ def get_clipboard(content_type: :plaintext)
3637
::Appium::Core::Device.add_endpoint_method(:set_clipboard) do
3738
def set_clipboard(content:, content_type: :plaintext, label: nil)
3839
unless ::Appium::Core::Base::Device::Clipboard::CONTENT_TYPE.member?(content_type)
39-
raise "content_type should be #{::Appium::Core::Base::Device::Clipboard::CONTENT_TYPE}"
40+
raise ::Appium::Core::Error::ArgumentError,
41+
"content_type should be #{::Appium::Core::Base::Device::Clipboard::CONTENT_TYPE}"
4042
end
4143

4244
params = {

lib/appium_lib_core/android/device/emulator.rb

+11-5
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def send_sms(phone_number:, message:)
122122
::Appium::Core::Device.add_endpoint_method(:gsm_call) do
123123
def gsm_call(phone_number:, action:)
124124
unless GSM_CALL_ACTIONS.member? action.to_sym
125-
raise "action: should be member of #{GSM_CALL_ACTIONS}. Not #{action}."
125+
raise ::Appium::Core::Error::ArgumentError, "action: should be member of #{GSM_CALL_ACTIONS}. Not #{action}."
126126
end
127127

128128
execute(:gsm_call, {}, { phoneNumber: phone_number, action: action })
@@ -131,7 +131,10 @@ def gsm_call(phone_number:, action:)
131131

132132
::Appium::Core::Device.add_endpoint_method(:gsm_signal) do
133133
def gsm_signal(signal_strength)
134-
raise "#{signal_strength} should be member of #{GSM_SIGNALS.keys} " if GSM_SIGNALS[signal_strength.to_sym].nil?
134+
if GSM_SIGNALS[signal_strength.to_sym].nil?
135+
raise ::Appium::Core::Error::ArgumentError,
136+
"#{signal_strength} should be member of #{GSM_SIGNALS.keys} "
137+
end
135138

136139
execute(:gsm_signal, {}, { signalStrength: GSM_SIGNALS[signal_strength],
137140
signalStrengh: GSM_SIGNALS[signal_strength] })
@@ -141,7 +144,7 @@ def gsm_signal(signal_strength)
141144
::Appium::Core::Device.add_endpoint_method(:gsm_voice) do
142145
def gsm_voice(state)
143146
unless GSM_VOICE_STATES.member? state.to_sym
144-
raise "The state should be member of #{GSM_VOICE_STATES}. Not #{state}."
147+
raise ::Appium::Core::Error::ArgumentError, "The state should be member of #{GSM_VOICE_STATES}. Not #{state}."
145148
end
146149

147150
execute(:gsm_voice, {}, { state: state })
@@ -150,7 +153,10 @@ def gsm_voice(state)
150153

151154
::Appium::Core::Device.add_endpoint_method(:set_network_speed) do
152155
def set_network_speed(netspeed)
153-
raise "The netspeed should be member of #{NET_SPEED}. Not #{netspeed}." unless NET_SPEED.member? netspeed.to_sym
156+
unless NET_SPEED.member? netspeed.to_sym
157+
raise ::Appium::Core::Error::ArgumentError,
158+
"The netspeed should be member of #{NET_SPEED}. Not #{netspeed}."
159+
end
154160

155161
execute(:set_network_speed, {}, { netspeed: netspeed })
156162
end
@@ -169,7 +175,7 @@ def set_power_capacity(percent)
169175
::Appium::Core::Device.add_endpoint_method(:set_power_ac) do
170176
def set_power_ac(state)
171177
unless POWER_AC_STATE.member? state.to_sym
172-
raise "The state should be member of #{POWER_AC_STATE}. Not #{state}."
178+
raise ::Appium::Core::Error::ArgumentError, "The state should be member of #{POWER_AC_STATE}. Not #{state}."
173179
end
174180

175181
execute(:set_power_ac, {}, { state: state })

lib/appium_lib_core/android/device/screen.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ def start_recording_screen(remote_path: nil, user: nil, pass: nil, method: 'PUT'
4141
option[:bitRate] = bit_rate unless bit_rate.nil?
4242

4343
unless bug_report.nil?
44-
raise 'bug_report should be true or false' unless [true, false].member?(bug_report)
44+
unless [true, false].member?(bug_report)
45+
raise ::Appium::Core::Error::ArgumentError, 'bug_report should be true or false'
46+
end
4547

4648
option[:bugReport] = bug_report
4749
end

lib/appium_lib_core/common/base/capabilities.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def convert_key(key)
3434
# here do not convert to camel case
3535
key.to_s
3636
else
37-
raise TypeError, "expected String or Symbol, got #{key.inspect}:#{key.class}"
37+
raise ::Appium::Core::Error::ArgumentError, "expected String or Symbol, got #{key.inspect}:#{key.class}"
3838
end
3939
end
4040
end

lib/appium_lib_core/common/base/screenshot.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def screenshot_as(format)
4848
when :png
4949
bridge.screenshot.unpack('m')[0]
5050
else
51-
raise Core::Error::UnsupportedOperationError, "unsupported format: #{format.inspect}"
51+
raise ::Appium::Core::Error::UnsupportedOperationError, "unsupported format: #{format.inspect}"
5252
end
5353
end
5454

@@ -88,7 +88,7 @@ def element_screenshot_as(element, format)
8888
when :png
8989
bridge.element_screenshot(element.id).unpack('m')[0]
9090
else
91-
raise Core::Error::UnsupportedOperationError, "unsupported format: #{format.inspect}"
91+
raise ::Appium::Core::Error::UnsupportedOperationError, "unsupported format: #{format.inspect}"
9292
end
9393
end
9494

lib/appium_lib_core/common/device/image_comparison.rb

+15-6
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,17 @@ def match_images_features(first_image:,
6565
good_matches_factor: nil,
6666
visualize: false)
6767
unless MATCH_FEATURES[:detector_name].member?(detector_name.to_s)
68-
raise "detector_name should be #{MATCH_FEATURES[:detector_name]}"
68+
raise ::Appium::Core::Error::ArgumentError, "detector_name should be #{MATCH_FEATURES[:detector_name]}"
6969
end
7070

7171
unless MATCH_FEATURES[:match_func].member?(match_func.to_s)
72-
raise "match_func should be #{MATCH_FEATURES[:match_func]}"
72+
raise ::Appium::Core::Error::ArgumentError, "match_func should be #{MATCH_FEATURES[:match_func]}"
7373
end
7474

75-
raise "visualize should be #{MATCH_FEATURES[:visualize]}" unless MATCH_FEATURES[:visualize].member?(visualize)
75+
unless MATCH_FEATURES[:visualize].member?(visualize)
76+
raise ::Appium::Core::Error::ArgumentError,
77+
"visualize should be #{MATCH_FEATURES[:visualize]}"
78+
end
7679

7780
options = {}
7881
options[:detectorName] = detector_name.to_s.upcase
@@ -109,7 +112,10 @@ def match_images_features(first_image:,
109112
#
110113
def find_image_occurrence(full_image:, partial_image:, visualize: false, threshold: nil,
111114
multiple: nil, match_neighbour_threshold: nil)
112-
raise "visualize should be #{MATCH_TEMPLATE[:visualize]}" unless MATCH_TEMPLATE[:visualize].member?(visualize)
115+
unless MATCH_TEMPLATE[:visualize].member?(visualize)
116+
raise ::Appium::Core::Error::ArgumentError,
117+
"visualize should be #{MATCH_TEMPLATE[:visualize]}"
118+
end
113119

114120
options = {}
115121
options[:visualize] = visualize
@@ -136,7 +142,10 @@ def find_image_occurrence(full_image:, partial_image:, visualize: false, thresho
136142
# File.write 'images_similarity_visual.png', Base64.decode64(visual['visualization']) # if the image is PNG
137143
#
138144
def get_images_similarity(first_image:, second_image:, visualize: false)
139-
raise "visualize should be #{GET_SIMILARITY[:visualize]}" unless GET_SIMILARITY[:visualize].member?(visualize)
145+
unless GET_SIMILARITY[:visualize].member?(visualize)
146+
raise ::Appium::Core::Error::ArgumentError,
147+
"visualize should be #{GET_SIMILARITY[:visualize]}"
148+
end
140149

141150
options = {}
142151
options[:visualize] = visualize
@@ -158,7 +167,7 @@ def get_images_similarity(first_image:, second_image:, visualize: false)
158167
# See the documentation on +appium-support+ module for more details.
159168
#
160169
def compare_images(mode: :matchFeatures, first_image:, second_image:, options: nil)
161-
raise "content_type should be #{MODE}" unless MODE.member?(mode)
170+
raise ::Appium::Core::Error::ArgumentError, "content_type should be #{MODE}" unless MODE.member?(mode)
162171

163172
params = {}
164173
params[:mode] = mode

lib/appium_lib_core/common/device/screen_record.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ def initialize(remote_path: nil, user: nil, pass: nil, method: 'PUT',
3030
@upload_option = if remote_path.nil?
3131
{}
3232
else
33-
raise 'method should be POST or PUT' unless METHOD.member?(method.to_s.upcase)
33+
unless METHOD.member?(method.to_s.upcase)
34+
raise ::Appium::Core::Error::ArgumentError,
35+
'method should be POST or PUT'
36+
end
3437

3538
option = {}
3639
option[:remotePath] = remote_path
@@ -45,7 +48,10 @@ def initialize(remote_path: nil, user: nil, pass: nil, method: 'PUT',
4548

4649
return if force_restart.nil?
4750

48-
raise 'force_restart should be true or false' unless [true, false].member?(force_restart)
51+
unless [true, false].member?(force_restart)
52+
raise ::Appium::Core::Error::ArgumentError,
53+
'force_restart should be true or false'
54+
end
4955

5056
@upload_option[:forceRestart] = force_restart
5157
end

lib/appium_lib_core/common/error.rb

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class UnsupportedOperationError < CoreError; end
2727
class ServerError < CoreError; end
2828

2929
# ruby_lib_core library specific errors
30+
class SessionNotCreatedError < CoreError; end
3031
class ArgumentError < CoreError; end
3132
end
3233
end

lib/appium_lib_core/driver.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,8 @@ def attach_to(session_id, url: nil, automation_name: nil, platform_name: nil,
475475
automation_name: automation_name,
476476
platform_name: platform_name)
477477
rescue Errno::ECONNREFUSED
478-
raise "ERROR: Unable to connect to Appium. Is the server running on #{@custom_url}?"
478+
raise ::Appium::Core::Error::SessionNotCreatedError,
479+
"ERROR: Unable to connect to Appium. Is the server running on #{@custom_url}?"
479480
end
480481

481482
@driver

lib/appium_lib_core/element.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def screenshot_as(format)
117117
when :png
118118
bridge.element_screenshot(@id).unpack('m')[0]
119119
else
120-
raise Core::Error::UnsupportedOperationError, "unsupported format: #{format.inspect}"
120+
raise ::Appium::Core::Error::UnsupportedOperationError, "unsupported format: #{format.inspect}"
121121
end
122122
end
123123

lib/appium_lib_core/ios/device/clipboard.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ def self.add_methods
2323
::Appium::Core::Device.add_endpoint_method(:get_clipboard) do
2424
def get_clipboard(content_type: :plaintext)
2525
unless ::Appium::Core::Base::Device::Clipboard::CONTENT_TYPE.member?(content_type)
26-
raise "content_type should be #{::Appium::Core::Base::Device::Clipboard::CONTENT_TYPE}"
26+
raise ::Appium::Core::Error::ArgumentError,
27+
"content_type should be #{::Appium::Core::Base::Device::Clipboard::CONTENT_TYPE}"
2728
end
2829

2930
params = { contentType: content_type }
@@ -36,7 +37,8 @@ def get_clipboard(content_type: :plaintext)
3637
::Appium::Core::Device.add_endpoint_method(:set_clipboard) do
3738
def set_clipboard(content:, content_type: :plaintext)
3839
unless ::Appium::Core::Base::Device::Clipboard::CONTENT_TYPE.member?(content_type)
39-
raise "content_type should be #{::Appium::Core::Base::Device::Clipboard::CONTENT_TYPE}"
40+
raise ::Appium::Core::Error::ArgumentError,
41+
"content_type should be #{::Appium::Core::Base::Device::Clipboard::CONTENT_TYPE}"
4042
end
4143

4244
params = {

0 commit comments

Comments
 (0)