Skip to content

Commit 6b5d969

Browse files
authored
feat: raise its own defined errors (#1035)
* feat: raise an error when no driver instances were created in some cases * raise Appium::ArgumentError error * use ArgumentError
1 parent 4a74c60 commit 6b5d969

File tree

10 files changed

+99
-35
lines changed

10 files changed

+99
-35
lines changed

lib/appium_lib/android/common/helper.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def ele_index(class_name, index)
150150
index = results.length
151151
index -= 1 if index >= 0
152152
else
153-
raise 'Index must be >= 1' unless index >= 1
153+
raise ArgumentError, 'Index must be >= 1' unless index >= 1
154154

155155
index -= 1 if index >= 1
156156
end

lib/appium_lib/android/element/button.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def button(value)
2929
# Android needs to combine button and image button to match iOS.
3030
if value.is_a? Numeric
3131
index = value
32-
raise "#{index} is not a valid index. Must be >= 1" if index <= 0
32+
raise ArgumentError, "#{index} is not a valid index. Must be >= 1" if index <= 0
3333

3434
# 1 indexed
3535
return find_element :uiautomator, _button_visible_selectors(index: index)

lib/appium_lib/android/espresso/element/button.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def button(value)
2525
# Android needs to combine button and image button to match iOS.
2626
if value.is_a? Numeric
2727
index = value
28-
raise "#{index} is not a valid index. Must be >= 1" if index <= 0
28+
raise ArgumentError, "#{index} is not a valid index. Must be >= 1" if index <= 0
2929

3030
# zero index
3131
_button_visible_selectors_xpath(index: index - 1)

lib/appium_lib/android/uiautomator2/element/button.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def button(value)
2525
# Android needs to combine button and image button to match iOS.
2626
if value.is_a? Numeric
2727
index = value
28-
raise "#{index} is not a valid index. Must be >= 1" if index <= 0
28+
raise ArgumentError, "#{index} is not a valid index. Must be >= 1" if index <= 0
2929

3030
result = find_elements :uiautomator, _button_visible_selectors(index: index)
3131
raise _no_such_element if result.empty?

lib/appium_lib/appium.rb

+7-7
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ class << self
5656
# @param opts [Hash] file: '/path/to/appium.txt', verbose: true
5757
# @return [hash] the symbolized hash with updated :app and :require keys
5858
def load_settings(opts = {})
59-
raise 'opts must be a hash' unless opts.is_a? Hash
60-
raise 'opts must not be empty' if opts.empty?
59+
raise ArgumentError, 'opts must be a hash' unless opts.is_a? Hash
60+
raise ArgumentError, 'opts must not be empty' if opts.empty?
6161

6262
toml = opts[:file]
63-
raise 'Must pass a capability file which has [caps] and [appium_lib]' unless toml
63+
raise ArgumentError, 'Must pass a capability file which has [caps] and [appium_lib]' unless toml
6464

6565
verbose = opts.fetch :verbose, false
6666

@@ -69,7 +69,7 @@ def load_settings(opts = {})
6969
toml_exists = File.exist? toml
7070
Appium::Logger.info "Exists? #{toml_exists}" if verbose
7171

72-
raise "toml doesn't exist #{toml}" unless toml_exists
72+
raise ArgumentError, "toml doesn't exist #{toml}" unless toml_exists
7373

7474
require 'tomlrb'
7575
Appium::Logger.info "Loading #{toml}" if verbose
@@ -151,7 +151,7 @@ def expand_required_files(base_dir, file_paths)
151151
# @param [Array<Module>] modules An array of modules
152152
# @param [Driver] driver A driver to extend for
153153
def promote_singleton_appium_methods(modules, driver = $driver)
154-
raise 'Global $driver is nil' if driver.nil?
154+
raise ArgumentError, 'Global $driver is nil' if driver.nil?
155155

156156
target_modules = []
157157

@@ -160,7 +160,7 @@ def promote_singleton_appium_methods(modules, driver = $driver)
160160
target_modules << modules.const_get(sub_module)
161161
end
162162
else
163-
raise 'modules must be a module or an array' unless modules.is_a? Array
163+
raise ArgumentError, 'modules must be a module or an array' unless modules.is_a? Array
164164

165165
target_modules = modules
166166
end
@@ -205,7 +205,7 @@ def promote_singleton_appium_methods(modules, driver = $driver)
205205
# Appium.promote_appium_methods Minitest::Spec
206206
#
207207
def promote_appium_methods(class_array, driver = $driver)
208-
raise 'Driver is nil' if driver.nil?
208+
raise ArgumentError, 'Driver is nil' if driver.nil?
209209

210210
# Wrap single class into an array
211211
class_array = [class_array] unless class_array.instance_of? Array

lib/appium_lib/driver.rb

+53-11
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def initialize(opts = {}, global_driver = false)
156156

157157
$driver&.driver_quit if global_driver
158158

159-
raise 'opts must be a hash' unless opts.is_a? Hash
159+
raise ArgumentError, 'opts must be a hash' unless opts.is_a? Hash
160160

161161
@core = ::Appium::Core.for(opts)
162162
extend ::Appium::Core::Device
@@ -329,7 +329,7 @@ def automation_name_is_xcuitest?
329329
# action.click(element).perform # The `click` is a part of `PointerActions`
330330
#
331331
def action
332-
@driver.action
332+
@driver&.action
333333
end
334334

335335
# Returns the server's version info
@@ -384,12 +384,12 @@ def appium_client_version
384384
#
385385
# @return [String] APP_PATH as an absolute path
386386
def self.absolute_app_path(opts)
387-
raise 'opts must be a hash' unless opts.is_a? Hash
387+
raise ArgumentError, 'opts must be a hash' unless opts.is_a? Hash
388388

389389
# FIXME: 'caps' and 'app' will be correct
390390
caps = opts[:caps] || opts['caps'] || {}
391391
app_path = caps[:app] || caps['app']
392-
raise 'absolute_app_path invoked and app is not set!' if app_path.nil? || app_path.empty?
392+
raise ArgumentError, 'absolute_app_path invoked and app is not set!' if app_path.nil? || app_path.empty?
393393
# Sauce storage API. http://saucelabs.com/docs/rest#storage
394394
return app_path if app_path.start_with? 'sauce-storage:'
395395
return app_path if app_path =~ URI::DEFAULT_PARSER.make_regexp # public URL for Sauce
@@ -431,7 +431,7 @@ def restart
431431
# @param png_save_path [String] the full path to save the png
432432
# @return [File]
433433
def screenshot(png_save_path)
434-
@driver.save_screenshot png_save_path
434+
@driver&.save_screenshot png_save_path
435435
end
436436

437437
# Takes a png screenshot of particular element's area
@@ -445,7 +445,7 @@ def screenshot(png_save_path)
445445
# @param [String] png_save_path the full path to save the png
446446
# @return [File]
447447
def element_screenshot(element, png_save_path)
448-
@driver.take_element_screenshot element, png_save_path
448+
@driver&.take_element_screenshot element, png_save_path
449449
nil
450450
end
451451

@@ -469,6 +469,9 @@ def driver_quit
469469
# size.height #=> Integer
470470
#
471471
def window_size
472+
# maybe exception is expected as no driver created
473+
raise NoDriverInstanceError if @driver.nil?
474+
472475
@driver.window_size
473476
end
474477

@@ -484,6 +487,8 @@ def window_size
484487
# size.y #=> Integer
485488
#
486489
def window_rect
490+
raise NoDriverInstanceError if @driver.nil?
491+
487492
@driver.window_rect
488493
end
489494

@@ -555,7 +560,7 @@ def set_implicit_wait(wait)
555560

556561
# Set implicit wait to zero.
557562
def no_wait
558-
@driver.manage.timeouts.implicit_wait = 0
563+
@driver&.manage&.timeouts&.implicit_wait = 0
559564
end
560565

561566
# Set implicit wait. Default to @default_wait.
@@ -570,7 +575,7 @@ def no_wait
570575
# @return [void]
571576
def set_wait(timeout = nil)
572577
timeout = @default_wait if timeout.nil?
573-
@driver.manage.timeouts.implicit_wait = timeout
578+
@driver&.manage&.timeouts&.implicit_wait = timeout
574579
end
575580

576581
# Returns existence of element.
@@ -589,9 +594,9 @@ def exists(pre_check = 0, post_check = @default_wait)
589594
# do not uset set_wait here.
590595
# it will cause problems with other methods reading the default_wait of 0
591596
# which then gets converted to a 1 second wait.
592-
@driver.manage.timeouts.implicit_wait = pre_check
597+
@driver&.manage&.timeouts&.implicit_wait = pre_check
593598
# the element exists unless an error is raised.
594-
exists = true
599+
exists = true
595600

596601
begin
597602
yield # search for element
@@ -600,7 +605,7 @@ def exists(pre_check = 0, post_check = @default_wait)
600605
end
601606

602607
# restore wait
603-
@driver.manage.timeouts.implicit_wait = post_check if post_check != pre_check
608+
@driver&.manage&.timeouts&.implicit_wait = post_check if post_check != pre_check
604609

605610
exists
606611
end
@@ -610,6 +615,8 @@ def exists(pre_check = 0, post_check = @default_wait)
610615
# @param [*args] args The args to pass to the script
611616
# @return [Object]
612617
def execute_script(script, *args)
618+
raise NoDriverInstanceError if @driver.nil?
619+
613620
@driver.execute_script script, *args
614621
end
615622

@@ -618,6 +625,8 @@ def execute_script(script, *args)
618625
###
619626
# Get the window handles of open browser windows
620627
def execute_async_script(script, *args)
628+
raise NoDriverInstanceError if @driver.nil?
629+
621630
@driver.execute_async_script script, *args
622631
end
623632

@@ -650,41 +659,59 @@ def execute_async_script(script, *args)
650659
# r.logs #=> The `logs` key part as `{'log' => [], 'warn' => [], 'error' => []}`
651660
#
652661
def execute_driver(script: '', type: 'webdriverio', timeout_ms: nil)
662+
raise NoDriverInstanceError if @driver.nil?
663+
653664
@driver.execute_driver(script: script, type: type, timeout_ms: timeout_ms)
654665
end
655666

656667
def window_handles
668+
raise NoDriverInstanceError if @driver.nil?
669+
657670
@driver.window_handles
658671
end
659672

660673
# Get the current window handle
661674
def window_handle
675+
raise NoDriverInstanceError if @driver.nil?
676+
662677
@driver.window_handle
663678
end
664679

665680
def navigate
681+
raise NoDriverInstanceError if @driver.nil?
682+
666683
@driver.navigate
667684
end
668685

669686
def manage
687+
raise NoDriverInstanceError if @driver.nil?
688+
670689
@driver.manage
671690
end
672691

673692
def get(url)
693+
raise NoDriverInstanceError if @driver.nil?
694+
674695
@driver.get(url)
675696
end
676697

677698
def current_url
699+
raise NoDriverInstanceError if @driver.nil?
700+
678701
@driver.current_url
679702
end
680703

681704
def title
705+
raise NoDriverInstanceError if @driver.nil?
706+
682707
@driver.title
683708
end
684709

685710
# @return [TargetLocator]
686711
# @see TargetLocator
687712
def switch_to
713+
raise NoDriverInstanceError if @driver.nil?
714+
688715
@driver.switch_to
689716
end
690717
###
@@ -712,6 +739,8 @@ def switch_to
712739
# @param [*args] args The args to use
713740
# @return [Array<Element>] Array is empty when no elements are found.
714741
def find_elements(*args)
742+
raise NoDriverInstanceError if @driver.nil?
743+
715744
@driver.find_elements(*args)
716745
end
717746

@@ -728,6 +757,8 @@ def find_elements(*args)
728757
# @param [*args] args The args to use
729758
# @return [Element]
730759
def find_element(*args)
760+
raise NoDriverInstanceError if @driver.nil?
761+
731762
@driver.find_element(*args)
732763
end
733764

@@ -743,6 +774,8 @@ def find_element(*args)
743774
# @driver.find_element_by_image './test/functional/data/test_element_image.png'
744775
#
745776
def find_element_by_image(png_img_path)
777+
raise NoDriverInstanceError if @driver.nil?
778+
746779
@driver.find_element_by_image(png_img_path)
747780
end
748781

@@ -758,6 +791,8 @@ def find_element_by_image(png_img_path)
758791
# @driver.find_elements_by_image ['./test/functional/data/test_element_image.png']
759792
#
760793
def find_elements_by_image(png_img_paths)
794+
raise NoDriverInstanceError if @driver.nil?
795+
761796
@driver.find_elements_by_image(png_img_paths)
762797
end
763798

@@ -771,6 +806,8 @@ def find_elements_by_image(png_img_paths)
771806
# @option opts [Float] :altitude the altitude, defaulting to 75
772807
# @return [Selenium::WebDriver::Location] the location constructed by the selenium webdriver
773808
def set_location(opts = {})
809+
raise NoDriverInstanceError if @driver.nil?
810+
774811
latitude = opts.fetch(:latitude)
775812
longitude = opts.fetch(:longitude)
776813
altitude = opts.fetch(:altitude, 75)
@@ -794,10 +831,13 @@ def set_location(opts = {})
794831
# # 'appium:anotherEvent' => 1572959315}
795832
#
796833
def log_event(vendor:, event:)
834+
raise NoDriverInstanceError if @driver.nil?
835+
797836
@driver.logs.event vendor: vendor, event: event
798837
end
799838

800839
def log_event=(log_event)
840+
raise if @driver.nil?
801841
unless log_event.is_a?(Hash)
802842
raise ::Appium::Core::Error::ArgumentError('log_event should be Hash like { vendor: "appium", event: "funEvent"}')
803843
end
@@ -817,6 +857,8 @@ def log_event=(log_event)
817857
# log_events #=> {'commands' => [{'cmd' => 123455, ....}], 'startTime' => 1572954894127, }
818858
#
819859
def log_events(type = nil)
860+
raise NoDriverInstanceError if @driver.nil?
861+
820862
@driver.logs.events(type)
821863
end
822864

lib/appium_lib/error.rb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
module Appium
16+
class Error < StandardError; end
17+
18+
# Driver instance hasn't been created yet.
19+
class NoDriverInstanceError < Appium::Error; end
20+
21+
class ArgumentError < ArgumentError; end
22+
end

0 commit comments

Comments
 (0)