lib/appium_lib/driver.rb in appium_lib-15.0.0 vs lib/appium_lib/driver.rb in appium_lib-15.2.0
- old
+ new
@@ -154,11 +154,11 @@
# Capybara can't put `global_driver` as the 2nd argument.
global_driver = opts.delete :global_driver if global_driver.nil?
$driver&.driver_quit if global_driver
- raise 'opts must be a hash' unless opts.is_a? Hash
+ raise ArgumentError, 'opts must be a hash' unless opts.is_a? Hash
@core = ::Appium::Core.for(opts)
extend ::Appium::Core::Device
opts = Appium.symbolize_keys opts
@@ -327,11 +327,11 @@
#
# element = find_element(:id, "some id")
# action.click(element).perform # The `click` is a part of `PointerActions`
#
def action
- @driver.action
+ @driver&.action
end
# Returns the server's version info
#
# @example
@@ -382,16 +382,16 @@
#
# if app isn't set then an error is raised.
#
# @return [String] APP_PATH as an absolute path
def self.absolute_app_path(opts)
- raise 'opts must be a hash' unless opts.is_a? Hash
+ raise ArgumentError, 'opts must be a hash' unless opts.is_a? Hash
# FIXME: 'caps' and 'app' will be correct
caps = opts[:caps] || opts['caps'] || {}
app_path = caps[:app] || caps['app']
- raise 'absolute_app_path invoked and app is not set!' if app_path.nil? || app_path.empty?
+ raise ArgumentError, 'absolute_app_path invoked and app is not set!' if app_path.nil? || app_path.empty?
# Sauce storage API. http://saucelabs.com/docs/rest#storage
return app_path if app_path.start_with? 'sauce-storage:'
return app_path if app_path =~ URI::DEFAULT_PARSER.make_regexp # public URL for Sauce
::Appium::Logger.warn('[Deprecation] Converting the path to absolute path will be removed. ' \
@@ -429,11 +429,11 @@
# screenshot '/tmp/hi.png'
#
# @param png_save_path [String] the full path to save the png
# @return [File]
def screenshot(png_save_path)
- @driver.save_screenshot png_save_path
+ @driver&.save_screenshot png_save_path
end
# Takes a png screenshot of particular element's area
#
# @example
@@ -443,11 +443,11 @@
#
# @param [String] element Element take a screenshot
# @param [String] png_save_path the full path to save the png
# @return [File]
def element_screenshot(element, png_save_path)
- @driver.take_element_screenshot element, png_save_path
+ @driver&.take_element_screenshot element, png_save_path
nil
end
# Quits the driver
# @return [void]
@@ -467,10 +467,13 @@
# size = @driver.window_size
# size.width #=> Integer
# size.height #=> Integer
#
def window_size
+ # maybe exception is expected as no driver created
+ raise NoDriverInstanceError if @driver.nil?
+
@driver.window_size
end
# Get the device window's rect.
# @return [Selenium::WebDriver::Rectangle]
@@ -482,10 +485,12 @@
# size.height #=> Integer
# size.x #=> Integer
# size.y #=> Integer
#
def window_rect
+ raise NoDriverInstanceError if @driver.nil?
+
@driver.window_rect
end
# Creates a new global driver and quits the old one if it exists.
# You can customise http_client as the following
@@ -553,11 +558,11 @@
{}
end
# Set implicit wait to zero.
def no_wait
- @driver.manage.timeouts.implicit_wait = 0
+ @driver&.manage&.timeouts&.implicit_wait = 0
end
# Set implicit wait. Default to @default_wait.
#
# @example
@@ -568,11 +573,11 @@
#
# @param timeout [Integer] the timeout in seconds
# @return [void]
def set_wait(timeout = nil)
timeout = @default_wait if timeout.nil?
- @driver.manage.timeouts.implicit_wait = timeout
+ @driver&.manage&.timeouts&.implicit_wait = timeout
end
# Returns existence of element.
#
# Example:
@@ -587,39 +592,43 @@
# @return [Boolean]
def exists(pre_check = 0, post_check = @default_wait)
# do not uset set_wait here.
# it will cause problems with other methods reading the default_wait of 0
# which then gets converted to a 1 second wait.
- @driver.manage.timeouts.implicit_wait = pre_check
+ @driver&.manage&.timeouts&.implicit_wait = pre_check
# the element exists unless an error is raised.
- exists = true
+ exists = true
begin
yield # search for element
rescue StandardError
exists = false # error means it's not there
end
# restore wait
- @driver.manage.timeouts.implicit_wait = post_check if post_check != pre_check
+ @driver&.manage&.timeouts&.implicit_wait = post_check if post_check != pre_check
exists
end
# The same as @driver.execute_script
# @param [String] script The script to execute
# @param [*args] args The args to pass to the script
# @return [Object]
def execute_script(script, *args)
+ raise NoDriverInstanceError if @driver.nil?
+
@driver.execute_script script, *args
end
###
# Wrap calling selenium webdrier APIs via ruby_core
###
# Get the window handles of open browser windows
def execute_async_script(script, *args)
+ raise NoDriverInstanceError if @driver.nil?
+
@driver.execute_async_script script, *args
end
# Run a set of script against the current session, allowing execution of many commands in one Appium request.
# Supports {https://webdriver.io/docs/api.html WebdriverIO} API so far.
@@ -648,45 +657,63 @@
# r #=> An instance of Appium::Core::Base::Device::ExecuteDriver::Result
# r.result #=> The `result` key part as the result of the script
# r.logs #=> The `logs` key part as `{'log' => [], 'warn' => [], 'error' => []}`
#
def execute_driver(script: '', type: 'webdriverio', timeout_ms: nil)
+ raise NoDriverInstanceError if @driver.nil?
+
@driver.execute_driver(script: script, type: type, timeout_ms: timeout_ms)
end
def window_handles
+ raise NoDriverInstanceError if @driver.nil?
+
@driver.window_handles
end
# Get the current window handle
def window_handle
+ raise NoDriverInstanceError if @driver.nil?
+
@driver.window_handle
end
def navigate
+ raise NoDriverInstanceError if @driver.nil?
+
@driver.navigate
end
def manage
+ raise NoDriverInstanceError if @driver.nil?
+
@driver.manage
end
def get(url)
+ raise NoDriverInstanceError if @driver.nil?
+
@driver.get(url)
end
def current_url
+ raise NoDriverInstanceError if @driver.nil?
+
@driver.current_url
end
def title
+ raise NoDriverInstanceError if @driver.nil?
+
@driver.title
end
# @return [TargetLocator]
# @see TargetLocator
def switch_to
+ raise NoDriverInstanceError if @driver.nil?
+
@driver.switch_to
end
###
# End core
###
@@ -710,10 +737,12 @@
# If you call `Appium.promote_appium_methods`, you can call `find_elements` directly.
#
# @param [*args] args The args to use
# @return [Array<Element>] Array is empty when no elements are found.
def find_elements(*args)
+ raise NoDriverInstanceError if @driver.nil?
+
@driver.find_elements(*args)
end
# Calls @driver.find_element
#
@@ -726,10 +755,12 @@
# If you call `Appium.promote_appium_methods`, you can call `find_element` directly.
#
# @param [*args] args The args to use
# @return [Element]
def find_element(*args)
+ raise NoDriverInstanceError if @driver.nil?
+
@driver.find_element(*args)
end
# Return ImageElement if current view has a partial image
#
@@ -741,10 +772,12 @@
# @example
#
# @driver.find_element_by_image './test/functional/data/test_element_image.png'
#
def find_element_by_image(png_img_path)
+ raise NoDriverInstanceError if @driver.nil?
+
@driver.find_element_by_image(png_img_path)
end
# Return ImageElement if current view has partial images
#
@@ -756,10 +789,12 @@
# @example
#
# @driver.find_elements_by_image ['./test/functional/data/test_element_image.png']
#
def find_elements_by_image(png_img_paths)
+ raise NoDriverInstanceError if @driver.nil?
+
@driver.find_elements_by_image(png_img_paths)
end
# Calls @driver.set_location
#
@@ -769,10 +804,12 @@
# @option opts [Float] :latitude the latitude in degrees (required)
# @option opts [Float] :longitude the longitude in degees (required)
# @option opts [Float] :altitude the altitude, defaulting to 75
# @return [Selenium::WebDriver::Location] the location constructed by the selenium webdriver
def set_location(opts = {})
+ raise NoDriverInstanceError if @driver.nil?
+
latitude = opts.fetch(:latitude)
longitude = opts.fetch(:longitude)
altitude = opts.fetch(:altitude, 75)
@driver.set_location(latitude, longitude, altitude)
end
@@ -792,14 +829,17 @@
# log_event = { vendor: 'appium', event: 'anotherEvent' }
# log_events #=> {...., 'appium:funEvent' => [1572957315, 1572960305],
# # 'appium:anotherEvent' => 1572959315}
#
def log_event(vendor:, event:)
+ raise NoDriverInstanceError if @driver.nil?
+
@driver.logs.event vendor: vendor, event: event
end
def log_event=(log_event)
+ raise if @driver.nil?
unless log_event.is_a?(Hash)
raise ::Appium::Core::Error::ArgumentError('log_event should be Hash like { vendor: "appium", event: "funEvent"}')
end
@driver.logs.event vendor: log_event[:vendor], event: log_event[:event]
@@ -815,9 +855,11 @@
#
# log_events #=> {}
# log_events #=> {'commands' => [{'cmd' => 123455, ....}], 'startTime' => 1572954894127, }
#
def log_events(type = nil)
+ raise NoDriverInstanceError if @driver.nil?
+
@driver.logs.events(type)
end
# Quit the driver and Pry.
# quit and exit are reserved by Pry.