Skip to content

Commit

Permalink
Merge pull request #8 from hoangtaiki/check_has_element
Browse files Browse the repository at this point in the history
Support print error when not found element
  • Loading branch information
hoangtaiki authored Aug 14, 2021
2 parents 60a50e6 + 1855730 commit a0ace62
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
11 changes: 0 additions & 11 deletions lib/appom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,6 @@ module Appom
class InvalidElementError < StandardError; end
# A block was passed to the method, which it cannot interpreter.
class UnsupportedBlockError < StandardError; end
# The condition that was being evaluated inside the block did not evaluate
# to true within the time limit.
class TimeoutError < StandardError; end
# An element could not be located on the page using the given search parameters.
class NoSuchElementError < StandardError; end
# An elements is empty using the given search parameters.
class ElementsEmptyError < StandardError; end
# Text from an element not equal expected text
class ElementsTextVerifyError < StandardError; end
# An element is define with no text value
class ElementsDefineNoTextError < StandardError; end

autoload :ElementContainer, 'appom/element_container'
autoload :Page, 'appom/page'
Expand Down
34 changes: 26 additions & 8 deletions lib/appom/element_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def _find(*find_args)
return element
end
end
raise Appom::ElementsEmptyError, "Can not found element with args = #{find_args}"
raise StandardError, "Can not found element with args = #{find_args}"
end
end

Expand Down Expand Up @@ -58,6 +58,7 @@ def _all(*find_args)
# Check page has or has not element with find_args
# If page has element return TRUE else return FALSE
def _check_has_element(*find_args)
args, text, visible = deduce_element_args(find_args)
elements = page.find_elements(*args)

if visible.nil? && text.nil?
Expand Down Expand Up @@ -91,9 +92,9 @@ def wait_until_get_not_empty(*find_args)
wait = Wait.new(timeout: Appom.max_wait_time)
wait.until do
result = page.find_elements(*find_args)
# If reponse is empty we will return false to make it not pass Wait condition
# If response is empty we will return false to make it not pass Wait condition
if result.empty?
raise Appom::ElementsEmptyError, "Can not found any elements with args = #{find_args}"
raise StandardError, "Can not found any elements with args = #{find_args}"
end
# Return result
return result
Expand All @@ -111,13 +112,30 @@ def wait_until(type, *find_args)
_find(*find_args).enabled?
# Function only return true if element disabled or raise an error if time out
when 'element disable'
!_find(*find_args).enabled?
# Function only return true if we can find at leat one element (array is not empty) or raise error
result = _find(*find_args)
if result.enabled?
raise StandardError, "Still found an element enable with args = #{find_args}"
end
return true
# Function only return true if we can find at least one element (array is not empty) or raise error
when 'at least one element exists'
!_all(*find_args).empty?
# Function only return true if we can't find at leat one element (array is empty) or raise error
result = _all(*find_args)
if result.empty?
raise StandardError, "Could not find any elements with args = #{find_args}"
end
return true

# Function only return true if we can't find at least one element (array is empty) or raise error
when 'no element exists'
_all(*find_args).empty?
result = _all(*find_args)
if !result.empty?
if result.size > 1
raise StandardError, "Still found #{result.size} elements with args = #{find_args}"
else
raise StandardError, "Still found #{result.size} element with args = #{find_args}"
end
end
return true
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/appom/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Appom
VERSION = '1.3.2'.freeze
VERSION = '1.4.0'.freeze
end
6 changes: 4 additions & 2 deletions lib/appom/wait.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,20 @@ def initialize(opts = {})
#
def until
end_time = Time.now + @timeout
error_message = ""

until Time.now > end_time
begin
result = yield
return result if result
rescue
rescue => error
error_message = error.message
end

sleep @interval
end

raise Appom::TimeoutError, "Timed out after #{@timeout}s."
raise StandardError, error_message
end
end
end

0 comments on commit a0ace62

Please sign in to comment.