Skip to content

Commit

Permalink
feat: raise its own defined errors (#564)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
KazuCocoa authored Jul 21, 2024
1 parent 8cc7388 commit 8f6bca9
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/functional-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ jobs:
steps:
- uses: actions/checkout@v3

- uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '17'

- name: Install Node.js
uses: actions/setup-node@v3
with:
Expand Down
6 changes: 3 additions & 3 deletions lib/appium_lib_core/android/device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -417,15 +417,15 @@ def toggle_location_services
def start_activity(opts)
::Appium::Logger.warn "[DEPRECATION] Please use 'mobile: startActivity' extension instead"

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

option = {}

app_package = opts[:app_package]
raise 'app_package is required' unless app_package
raise ::Appium::Core::Error::ArgumentError, 'app_package is required' unless app_package

app_activity = opts[:app_activity]
raise 'app_activity is required' unless app_activity
raise ::Appium::Core::Error::ArgumentError, 'app_activity is required' unless app_activity

option[:appPackage] = app_package
option[:appActivity] = app_activity
Expand Down
6 changes: 4 additions & 2 deletions lib/appium_lib_core/android/device/clipboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def self.add_methods
::Appium::Core::Device.add_endpoint_method(:get_clipboard) do
def get_clipboard(content_type: :plaintext)
unless ::Appium::Core::Base::Device::Clipboard::CONTENT_TYPE.member?(content_type)
raise "content_type should be #{::Appium::Core::Base::Device::Clipboard::CONTENT_TYPE}"
raise ::Appium::Core::Error::ArgumentError,
"content_type should be #{::Appium::Core::Base::Device::Clipboard::CONTENT_TYPE}"
end

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

params = {
Expand Down
16 changes: 11 additions & 5 deletions lib/appium_lib_core/android/device/emulator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def send_sms(phone_number:, message:)
::Appium::Core::Device.add_endpoint_method(:gsm_call) do
def gsm_call(phone_number:, action:)
unless GSM_CALL_ACTIONS.member? action.to_sym
raise "action: should be member of #{GSM_CALL_ACTIONS}. Not #{action}."
raise ::Appium::Core::Error::ArgumentError, "action: should be member of #{GSM_CALL_ACTIONS}. Not #{action}."
end

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

::Appium::Core::Device.add_endpoint_method(:gsm_signal) do
def gsm_signal(signal_strength)
raise "#{signal_strength} should be member of #{GSM_SIGNALS.keys} " if GSM_SIGNALS[signal_strength.to_sym].nil?
if GSM_SIGNALS[signal_strength.to_sym].nil?
raise ::Appium::Core::Error::ArgumentError,
"#{signal_strength} should be member of #{GSM_SIGNALS.keys} "
end

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

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

::Appium::Core::Device.add_endpoint_method(:set_network_speed) do
def set_network_speed(netspeed)
raise "The netspeed should be member of #{NET_SPEED}. Not #{netspeed}." unless NET_SPEED.member? netspeed.to_sym
unless NET_SPEED.member? netspeed.to_sym
raise ::Appium::Core::Error::ArgumentError,
"The netspeed should be member of #{NET_SPEED}. Not #{netspeed}."
end

execute(:set_network_speed, {}, { netspeed: netspeed })
end
Expand All @@ -169,7 +175,7 @@ def set_power_capacity(percent)
::Appium::Core::Device.add_endpoint_method(:set_power_ac) do
def set_power_ac(state)
unless POWER_AC_STATE.member? state.to_sym
raise "The state should be member of #{POWER_AC_STATE}. Not #{state}."
raise ::Appium::Core::Error::ArgumentError, "The state should be member of #{POWER_AC_STATE}. Not #{state}."
end

execute(:set_power_ac, {}, { state: state })
Expand Down
4 changes: 3 additions & 1 deletion lib/appium_lib_core/android/device/screen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ def start_recording_screen(remote_path: nil, user: nil, pass: nil, method: 'PUT'
option[:bitRate] = bit_rate unless bit_rate.nil?

unless bug_report.nil?
raise 'bug_report should be true or false' unless [true, false].member?(bug_report)
unless [true, false].member?(bug_report)
raise ::Appium::Core::Error::ArgumentError, 'bug_report should be true or false'
end

option[:bugReport] = bug_report
end
Expand Down
2 changes: 1 addition & 1 deletion lib/appium_lib_core/common/base/capabilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def convert_key(key)
# here do not convert to camel case
key.to_s
else
raise TypeError, "expected String or Symbol, got #{key.inspect}:#{key.class}"
raise ::Appium::Core::Error::ArgumentError, "expected String or Symbol, got #{key.inspect}:#{key.class}"
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/appium_lib_core/common/base/screenshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def screenshot_as(format)
when :png
bridge.screenshot.unpack('m')[0]
else
raise Core::Error::UnsupportedOperationError, "unsupported format: #{format.inspect}"
raise ::Appium::Core::Error::UnsupportedOperationError, "unsupported format: #{format.inspect}"
end
end

Expand Down Expand Up @@ -88,7 +88,7 @@ def element_screenshot_as(element, format)
when :png
bridge.element_screenshot(element.id).unpack('m')[0]
else
raise Core::Error::UnsupportedOperationError, "unsupported format: #{format.inspect}"
raise ::Appium::Core::Error::UnsupportedOperationError, "unsupported format: #{format.inspect}"
end
end

Expand Down
21 changes: 15 additions & 6 deletions lib/appium_lib_core/common/device/image_comparison.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,17 @@ def match_images_features(first_image:,
good_matches_factor: nil,
visualize: false)
unless MATCH_FEATURES[:detector_name].member?(detector_name.to_s)
raise "detector_name should be #{MATCH_FEATURES[:detector_name]}"
raise ::Appium::Core::Error::ArgumentError, "detector_name should be #{MATCH_FEATURES[:detector_name]}"
end

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

raise "visualize should be #{MATCH_FEATURES[:visualize]}" unless MATCH_FEATURES[:visualize].member?(visualize)
unless MATCH_FEATURES[:visualize].member?(visualize)
raise ::Appium::Core::Error::ArgumentError,
"visualize should be #{MATCH_FEATURES[:visualize]}"
end

options = {}
options[:detectorName] = detector_name.to_s.upcase
Expand Down Expand Up @@ -109,7 +112,10 @@ def match_images_features(first_image:,
#
def find_image_occurrence(full_image:, partial_image:, visualize: false, threshold: nil,
multiple: nil, match_neighbour_threshold: nil)
raise "visualize should be #{MATCH_TEMPLATE[:visualize]}" unless MATCH_TEMPLATE[:visualize].member?(visualize)
unless MATCH_TEMPLATE[:visualize].member?(visualize)
raise ::Appium::Core::Error::ArgumentError,
"visualize should be #{MATCH_TEMPLATE[:visualize]}"
end

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

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

params = {}
params[:mode] = mode
Expand Down
10 changes: 8 additions & 2 deletions lib/appium_lib_core/common/device/screen_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ def initialize(remote_path: nil, user: nil, pass: nil, method: 'PUT',
@upload_option = if remote_path.nil?
{}
else
raise 'method should be POST or PUT' unless METHOD.member?(method.to_s.upcase)
unless METHOD.member?(method.to_s.upcase)
raise ::Appium::Core::Error::ArgumentError,
'method should be POST or PUT'
end

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

return if force_restart.nil?

raise 'force_restart should be true or false' unless [true, false].member?(force_restart)
unless [true, false].member?(force_restart)
raise ::Appium::Core::Error::ArgumentError,
'force_restart should be true or false'
end

@upload_option[:forceRestart] = force_restart
end
Expand Down
1 change: 1 addition & 0 deletions lib/appium_lib_core/common/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class UnsupportedOperationError < CoreError; end
class ServerError < CoreError; end

# ruby_lib_core library specific errors
class SessionNotCreatedError < CoreError; end
class ArgumentError < CoreError; end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/appium_lib_core/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,8 @@ def attach_to(session_id, url: nil, automation_name: nil, platform_name: nil,
automation_name: automation_name,
platform_name: platform_name)
rescue Errno::ECONNREFUSED
raise "ERROR: Unable to connect to Appium. Is the server running on #{@custom_url}?"
raise ::Appium::Core::Error::SessionNotCreatedError,
"ERROR: Unable to connect to Appium. Is the server running on #{@custom_url}?"
end

@driver
Expand Down
2 changes: 1 addition & 1 deletion lib/appium_lib_core/element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def screenshot_as(format)
when :png
bridge.element_screenshot(@id).unpack('m')[0]
else
raise Core::Error::UnsupportedOperationError, "unsupported format: #{format.inspect}"
raise ::Appium::Core::Error::UnsupportedOperationError, "unsupported format: #{format.inspect}"
end
end

Expand Down
6 changes: 4 additions & 2 deletions lib/appium_lib_core/ios/device/clipboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def self.add_methods
::Appium::Core::Device.add_endpoint_method(:get_clipboard) do
def get_clipboard(content_type: :plaintext)
unless ::Appium::Core::Base::Device::Clipboard::CONTENT_TYPE.member?(content_type)
raise "content_type should be #{::Appium::Core::Base::Device::Clipboard::CONTENT_TYPE}"
raise ::Appium::Core::Error::ArgumentError,
"content_type should be #{::Appium::Core::Base::Device::Clipboard::CONTENT_TYPE}"
end

params = { contentType: content_type }
Expand All @@ -36,7 +37,8 @@ def get_clipboard(content_type: :plaintext)
::Appium::Core::Device.add_endpoint_method(:set_clipboard) do
def set_clipboard(content:, content_type: :plaintext)
unless ::Appium::Core::Base::Device::Clipboard::CONTENT_TYPE.member?(content_type)
raise "content_type should be #{::Appium::Core::Base::Device::Clipboard::CONTENT_TYPE}"
raise ::Appium::Core::Error::ArgumentError,
"content_type should be #{::Appium::Core::Base::Device::Clipboard::CONTENT_TYPE}"
end

params = {
Expand Down

0 comments on commit 8f6bca9

Please sign in to comment.