From 8f6bca93da7a0cc8bb474460197aefe4a6cb2a8c Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Sat, 20 Jul 2024 23:57:02 -0700 Subject: [PATCH] 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 --- .github/workflows/functional-test.yml | 5 +++++ lib/appium_lib_core/android/device.rb | 6 +++--- .../android/device/clipboard.rb | 6 ++++-- .../android/device/emulator.rb | 16 +++++++++----- lib/appium_lib_core/android/device/screen.rb | 4 +++- .../common/base/capabilities.rb | 2 +- lib/appium_lib_core/common/base/screenshot.rb | 4 ++-- .../common/device/image_comparison.rb | 21 +++++++++++++------ .../common/device/screen_record.rb | 10 +++++++-- lib/appium_lib_core/common/error.rb | 1 + lib/appium_lib_core/driver.rb | 3 ++- lib/appium_lib_core/element.rb | 2 +- lib/appium_lib_core/ios/device/clipboard.rb | 6 ++++-- 13 files changed, 60 insertions(+), 26 deletions(-) diff --git a/.github/workflows/functional-test.yml b/.github/workflows/functional-test.yml index 7bbb3fb2..dde2856c 100644 --- a/.github/workflows/functional-test.yml +++ b/.github/workflows/functional-test.yml @@ -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: diff --git a/lib/appium_lib_core/android/device.rb b/lib/appium_lib_core/android/device.rb index e0a7db53..2a57dc8b 100644 --- a/lib/appium_lib_core/android/device.rb +++ b/lib/appium_lib_core/android/device.rb @@ -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 diff --git a/lib/appium_lib_core/android/device/clipboard.rb b/lib/appium_lib_core/android/device/clipboard.rb index e50287fd..36f217e5 100644 --- a/lib/appium_lib_core/android/device/clipboard.rb +++ b/lib/appium_lib_core/android/device/clipboard.rb @@ -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 } @@ -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 = { diff --git a/lib/appium_lib_core/android/device/emulator.rb b/lib/appium_lib_core/android/device/emulator.rb index 940d0754..12efeb8d 100644 --- a/lib/appium_lib_core/android/device/emulator.rb +++ b/lib/appium_lib_core/android/device/emulator.rb @@ -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 }) @@ -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] }) @@ -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 }) @@ -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 @@ -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 }) diff --git a/lib/appium_lib_core/android/device/screen.rb b/lib/appium_lib_core/android/device/screen.rb index e7f58f35..6e464f47 100644 --- a/lib/appium_lib_core/android/device/screen.rb +++ b/lib/appium_lib_core/android/device/screen.rb @@ -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 diff --git a/lib/appium_lib_core/common/base/capabilities.rb b/lib/appium_lib_core/common/base/capabilities.rb index 4a6ba521..7683769c 100644 --- a/lib/appium_lib_core/common/base/capabilities.rb +++ b/lib/appium_lib_core/common/base/capabilities.rb @@ -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 diff --git a/lib/appium_lib_core/common/base/screenshot.rb b/lib/appium_lib_core/common/base/screenshot.rb index 41cd6600..370f1135 100644 --- a/lib/appium_lib_core/common/base/screenshot.rb +++ b/lib/appium_lib_core/common/base/screenshot.rb @@ -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 @@ -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 diff --git a/lib/appium_lib_core/common/device/image_comparison.rb b/lib/appium_lib_core/common/device/image_comparison.rb index e4551cdc..8ce341cb 100644 --- a/lib/appium_lib_core/common/device/image_comparison.rb +++ b/lib/appium_lib_core/common/device/image_comparison.rb @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/lib/appium_lib_core/common/device/screen_record.rb b/lib/appium_lib_core/common/device/screen_record.rb index f4da7cc0..74fbf3a0 100644 --- a/lib/appium_lib_core/common/device/screen_record.rb +++ b/lib/appium_lib_core/common/device/screen_record.rb @@ -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 @@ -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 diff --git a/lib/appium_lib_core/common/error.rb b/lib/appium_lib_core/common/error.rb index bcfcd842..3f04bdef 100644 --- a/lib/appium_lib_core/common/error.rb +++ b/lib/appium_lib_core/common/error.rb @@ -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 diff --git a/lib/appium_lib_core/driver.rb b/lib/appium_lib_core/driver.rb index d482f231..fc1f47c4 100644 --- a/lib/appium_lib_core/driver.rb +++ b/lib/appium_lib_core/driver.rb @@ -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 diff --git a/lib/appium_lib_core/element.rb b/lib/appium_lib_core/element.rb index 58eeca8f..23ca2568 100644 --- a/lib/appium_lib_core/element.rb +++ b/lib/appium_lib_core/element.rb @@ -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 diff --git a/lib/appium_lib_core/ios/device/clipboard.rb b/lib/appium_lib_core/ios/device/clipboard.rb index 9b8ea67a..d43a5b5b 100644 --- a/lib/appium_lib_core/ios/device/clipboard.rb +++ b/lib/appium_lib_core/ios/device/clipboard.rb @@ -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 } @@ -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 = {