Skip to content

Commit

Permalink
update Exceptions and Watir namespacing
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Sep 17, 2018
1 parent 86cf366 commit 83e7f63
Show file tree
Hide file tree
Showing 22 changed files with 70 additions and 60 deletions.
4 changes: 3 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ Metrics/MethodLength:

# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 116
Max: 105
Exclude:
- 'lib/watir/locators/element/locator.rb'
- 'lib/watir/locators/element/selector_builder/xpath.rb'
- 'lib/watir/browser.rb'
- 'lib/watir/window.rb'
- 'lib/watir/elements/element.rb'
- 'lib/watir/elements/select.rb'
- 'lib/watir/generator/base/spec_extractor.rb'

Metrics/PerceivedComplexity:
Max: 9
Expand Down
2 changes: 1 addition & 1 deletion lib/watir.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def element_class_for(tag_name)
#

def self.logger
@logger ||= Watir::Logger.new
@logger ||= Logger.new
end
end
require 'watir/locators'
Expand Down
7 changes: 4 additions & 3 deletions lib/watir/alert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Watir
class Alert
include EventuallyPresent
include Waitable
include Exception

def initialize(browser)
@browser = browser
Expand Down Expand Up @@ -79,7 +80,7 @@ def set(value)
def exists?
assert_exists
true
rescue Exception::UnknownObjectException
rescue UnknownObjectException
false
end
alias present? exists?
Expand All @@ -97,7 +98,7 @@ def selector_string
def assert_exists
@alert = @browser.driver.switch_to.alert
rescue Selenium::WebDriver::Error::NoSuchAlertError
raise Exception::UnknownObjectException, 'unable to locate alert'
raise UnknownObjectException, 'unable to locate alert'
end

def wait_for_exists
Expand All @@ -112,7 +113,7 @@ def wait_for_exists
message << 'consider using Alert#exists? instead of rescuing UnknownObjectException'
Watir.logger.warn message, ids: [:wait_for_alert]
end
raise Exception::UnknownObjectException, 'unable to locate alert'
raise UnknownObjectException, 'unable to locate alert'
end
end
end # Alert
Expand Down
11 changes: 6 additions & 5 deletions lib/watir/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Browser
include HasWindow
include Waitable
include Navigation
include Exception

attr_writer :default_context, :original_window, :locator_namespace
attr_reader :driver, :after_hooks
Expand Down Expand Up @@ -40,7 +41,7 @@ def start(url, browser = :chrome, *args)
def initialize(browser = :chrome, *args)
case browser
when ::Symbol, String
selenium_args = Watir::Capabilities.new(browser, *args).to_args
selenium_args = Capabilities.new(browser, *args).to_args
@driver = Selenium::WebDriver.for(*selenium_args)
when Selenium::WebDriver::Driver
@driver = browser
Expand Down Expand Up @@ -212,7 +213,7 @@ def status
#

def execute_script(script, *args)
args.map! { |e| e.is_a?(Watir::Element) ? e.wd : e }
args.map! { |e| e.is_a?(Element) ? e.wd : e }

wrap_elements_in(self, @driver.execute_script(script, *args))
end
Expand Down Expand Up @@ -262,11 +263,11 @@ def assert_exists
locate
return if window.present?

raise Exception::NoMatchingWindowFoundException, 'browser window was closed'
raise NoMatchingWindowFoundException, 'browser window was closed'
end

def locate
raise Exception::Error, 'browser was closed' if @closed
raise Error, 'browser was closed' if @closed

ensure_context
end
Expand Down Expand Up @@ -298,7 +299,7 @@ def browser
#

def locator_namespace
@locator_namespace ||= Watir::Locators
@locator_namespace ||= Locators
end

#
Expand Down
2 changes: 1 addition & 1 deletion lib/watir/element_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def to_a
selector = @selector.merge(element: e)
selector[:index] = idx
element = element_class.new(@query_scope, selector)
if [Watir::HTMLElement, Watir::Input].include? element.class
if [HTMLElement, Input].include? element.class
tag_name = @selector[:tag_name] || element.tag_name.to_sym
hash[tag_name] ||= 0
hash[tag_name] += 1
Expand Down
4 changes: 2 additions & 2 deletions lib/watir/elements/button.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Watir
#

class Button < HTMLElement
inherit_attributes_from Watir::Input
inherit_attributes_from Input

VALID_TYPES = %w[button reset submit image].freeze

Expand All @@ -28,7 +28,7 @@ def text
when 'button'
super
else
raise Exception::Error, "unknown tag name for button: #{tn}"
raise Error, "unknown tag name for button: #{tn}"
end
end
end # Button
Expand Down
16 changes: 8 additions & 8 deletions lib/watir/elements/element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ def browser
#

def stale?
raise Watir::Exception::Error, 'Can not check staleness of unused element' unless @element
raise Error, 'Can not check staleness of unused element' unless @element

ensure_context
stale_in_context?
Expand Down Expand Up @@ -627,7 +627,7 @@ def wait_for_exists
begin
@query_scope.wait_for_exists unless @query_scope.is_a? Browser
wait_until(&:exists?)
rescue Watir::Wait::TimeoutError
rescue Wait::TimeoutError
msg = "timed out after #{Watir.default_timeout} seconds, waiting for #{inspect} to be located"
raise unknown_exception, msg
end
Expand All @@ -640,7 +640,7 @@ def wait_for_present
begin
@query_scope.wait_for_present unless @query_scope.is_a? Browser
wait_until_present
rescue Watir::Wait::TimeoutError
rescue Wait::TimeoutError
msg = "element located, but timed out after #{Watir.default_timeout} seconds, " \
"waiting for #{inspect} to be present"
raise unknown_exception, msg
Expand All @@ -656,7 +656,7 @@ def wait_for_enabled

begin
wait_until(&:enabled?)
rescue Watir::Wait::TimeoutError
rescue Wait::TimeoutError
raise_disabled
end
end
Expand All @@ -671,7 +671,7 @@ def wait_for_writable

begin
wait_until { !respond_to?(:readonly?) || !readonly? }
rescue Watir::Wait::TimeoutError
rescue Wait::TimeoutError
message = "element present and enabled, but timed out after #{Watir.default_timeout} seconds, " \
"waiting for #{inspect} to not be readonly"
raise ObjectReadOnlyException, message
Expand Down Expand Up @@ -699,7 +699,7 @@ def locate_in_context
private

def unknown_exception
Watir::Exception::UnknownObjectException
UnknownObjectException
end

def raise_writable
Expand Down Expand Up @@ -733,7 +733,7 @@ def assert_enabled
end

def assert_is_element(obj)
raise TypeError, "execpted Watir::Element, got #{obj.inspect}:#{obj.class}" unless obj.is_a? Watir::Element
raise TypeError, "execpted Watir::Element, got #{obj.inspect}:#{obj.class}" unless obj.is_a? Element
end

# Removes duplication in #present? & #visible? and makes setting deprecation notice easier
Expand Down Expand Up @@ -780,7 +780,7 @@ def element_call(precondition = nil, &block)
raise_disabled unless %i[wait_for_present wait_for_enabled wait_for_writable].include?(precondition)
retry
rescue Selenium::WebDriver::Error::NoSuchWindowError
raise Exception::NoMatchingWindowFoundException, 'browser window was closed'
raise NoMatchingWindowFoundException, 'browser window was closed'
ensure
Watir.logger.info "<- `Completed #{inspect}##{caller}`"
Wait.timer.reset! unless already_locked
Expand Down
2 changes: 1 addition & 1 deletion lib/watir/elements/hidden.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def visible?
end

def click
raise Watir::Exception::ObjectDisabledException, 'click is not available on the hidden field element'
raise ObjectDisabledException, 'click is not available on the hidden field element'
end
end

Expand Down
6 changes: 3 additions & 3 deletions lib/watir/elements/iframe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def send_keys(*args)
#

def execute_script(script, *args)
args.map! { |e| e.is_a?(Watir::Element) ? e.wd : e }
args.map! { |e| e.is_a?(Element) ? e.wd : e }
returned = driver.execute_script(script, *args)

browser.wrap_elements_in(self, returned)
Expand Down Expand Up @@ -77,7 +77,7 @@ def relocate?
private

def unknown_exception
Watir::Exception::UnknownFrameException
UnknownFrameException
end
end # IFrame

Expand Down Expand Up @@ -125,7 +125,7 @@ def switch!
@driver.switch_to.frame @element
@browser.default_context = false
rescue Selenium::WebDriver::Error::NoSuchFrameError => e
raise Exception::UnknownFrameException, e.message
raise UnknownFrameException, e.message
end

def wd
Expand Down
8 changes: 3 additions & 5 deletions lib/watir/elements/select.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
module Watir
class Select < HTMLElement
include Watir::Exception

#
# Clears all selected options.
#

def clear
raise Error, 'you can only clear multi-selects' unless multiple?
raise Exception::Error, 'you can only clear multi-selects' unless multiple?

selected_options.each(&:click)
end
Expand Down Expand Up @@ -189,7 +187,7 @@ def matching_option?(how, what)
next unless what.is_a?(String) ? value == what : value =~ what
return true if opt.enabled?

raise Watir::Exception::ObjectDisabledException, "option matching #{what} by #{how} on #{inspect} is disabled"
raise ObjectDisabledException, "option matching #{what} by #{how} on #{inspect} is disabled"
end
false
end
Expand Down Expand Up @@ -236,7 +234,7 @@ def matches_regexp?(how, element, exp)
when :value
element.value =~ exp
else
raise Error, "unknown how: #{how.inspect}"
raise Exception::Error, "unknown how: #{how.inspect}"
end
end
end # Select
Expand Down
4 changes: 2 additions & 2 deletions lib/watir/elements/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def each(&block)

def hashes
all_rows = rows.locate
header_row = all_rows.first || raise(Exception::Error, 'no rows in table')
header_row = all_rows.first || raise(Error, 'no rows in table')

all_rows.entries[1..-1].map do |row|
cell_size_check(header_row, row)
Expand Down Expand Up @@ -69,7 +69,7 @@ def cell_size_check(header_row, cell_row)

index = cell_row.selector[:index]
row_id = index ? "row at index #{index - 1}" : 'designated row'
raise Exception::Error, "#{row_id} has #{row_size} cells, while header row has #{header_size}"
raise Error, "#{row_id} has #{row_size} cells, while header row has #{header_size}"
end
end # Table
end # Watir
3 changes: 1 addition & 2 deletions lib/watir/exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ class UnknownObjectException < Error; end
class ObjectDisabledException < Error; end
class ObjectReadOnlyException < Error; end
class NoValueFoundException < Error; end
class UnknownCellException < Error; end
class NoMatchingWindowFoundException < Error; end
class UnknownFrameException < Error; end
class UnknownRowException < Error; end
class LocatorException < Error; end
end # Exception
end # Watir
2 changes: 1 addition & 1 deletion lib/watir/js_snippets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module JSSnippets

def execute_js(function_name, *arguments)
file = File.expand_path("../js_snippets/#{function_name}.js", __FILE__)
raise Watir::Error, "Can not excute script as #{function_name}.js does not exist" unless File.exist?(file)
raise Exception::Error, "Can not excute script as #{function_name}.js does not exist" unless File.exist?(file)

js = File.read(file)
script = "return (#{js}).apply(null, arguments)"
Expand Down
10 changes: 5 additions & 5 deletions lib/watir/legacy_wait.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def respond_to_missing?(*args)
def method_missing(method, *args, &block)
return super unless @element.respond_to?(method)

Watir::Wait.until(@timeout, @message) { wait_until }
Wait.until(@timeout, @message) { wait_until }

@element.__send__(method, *args, &block)
end
Expand All @@ -29,9 +29,9 @@ def method_missing(method, *args, &block)

class WhenPresentDecorator < BaseDecorator
def present?
Watir::Wait.until(@timeout, @message) { wait_until }
Wait.until(@timeout, @message) { wait_until }
true
rescue Watir::Wait::TimeoutError
rescue Wait::TimeoutError
false
end

Expand Down Expand Up @@ -85,7 +85,7 @@ def when_present(timeout = nil)
message = "waiting for #{selector_string} to become present"

if block_given?
Watir::Wait.until(timeout, message) { present? }
Wait.until(timeout, message) { present? }
yield self
else
WhenPresentDecorator.new(self, timeout, message)
Expand All @@ -112,7 +112,7 @@ def when_enabled(timeout = nil)
message = "waiting for #{selector_string} to become enabled"

if block_given?
Watir::Wait.until(timeout, message) { enabled? }
Wait.until(timeout, message) { enabled? }
yield self
else
WhenEnabledDecorator.new(self, timeout, message)
Expand Down
6 changes: 3 additions & 3 deletions lib/watir/locators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@ def locator_class
class_from_string("#{browser.locator_namespace}::#{element_class_name}::Locator") ||
class_from_string("Watir::Locators::#{element_class_name}::Locator") ||
class_from_string("#{browser.locator_namespace}::Element::Locator") ||
Watir::Locators::Element::Locator
Locators::Element::Locator
end

def element_validator_class
class_from_string("#{browser.locator_namespace}::#{element_class_name}::Validator") ||
class_from_string("Watir::Locators::#{element_class_name}::Validator") ||
class_from_string("#{browser.locator_namespace}::Element::Validator") ||
Watir::Locators::Element::Validator
Locators::Element::Validator
end

def selector_builder_class
class_from_string("#{browser.locator_namespace}::#{element_class_name}::SelectorBuilder") ||
class_from_string("Watir::Locators::#{element_class_name}::SelectorBuilder") ||
class_from_string("#{browser.locator_namespace}::Element::SelectorBuilder") ||
Watir::Locators::Element::SelectorBuilder
Locators::Element::SelectorBuilder
end

def class_from_string(string)
Expand Down
Loading

0 comments on commit 83e7f63

Please sign in to comment.