lib/element.rb in gridium-0.2.4 vs lib/element.rb in gridium-0.2.5

- old
+ new

@@ -3,10 +3,11 @@ require 'spec_data' class Element attr_reader :name, :by, :locator + def initialize(name, by, locator) @name = name @by = by @locator = locator @element_screenshot = nil #used to store the path of element screenshots for comparison @@ -14,10 +15,13 @@ # wrapped driver @driver = Driver.driver # selenium web element @element = nil + + #how long to wait between clearing an input and sending keys to it + @text_padding_time = 0.1 end def to_s "'#{@name}' (By:#{@by} => '#{@locator}')" end @@ -98,12 +102,17 @@ element.enabled? end def clear element.clear + sleep @text_padding_time end + def value + element.attribute "value" + end + def click Log.debug("Clicking on #{self}") if element.enabled? ElementExtensions.highlight(self) if Gridium.config.highlight_verifications $verification_passes += 1 @@ -111,21 +120,48 @@ else Log.error('Cannot click on element. Element is not present.') end end + # + # add to what's already in the text field + # for cases when you don't want to stomp what's already in the text field + # + + def append_keys(*args) + ElementExtensions.highlight(self) if Gridium.config.highlight_verifications + $verification_passes += 1 + unless element.enabled? + raise "Browser Error: tried to enter #{args} but the input is disabled" + end + element.send_keys(*args) + sleep @text_padding_time + # when it's possible to validate for more than non-empty outcomes, do that here + end + + # + # overwrite to what's already in the text field + # and validate afterward + # + def send_keys(*args) - Log.debug("Typing: #{args} into element: (#{self}).") - if element.enabled? - ElementExtensions.highlight(self) if Gridium.config.highlight_verifications - $verification_passes += 1 - element.send_keys(*args) + ElementExtensions.highlight(self) if Gridium.config.highlight_verifications + $verification_passes += 1 + unless element.enabled? + raise "Browser Error: tried to enter #{args} but the input is disabled" + end + if only_symbols? *args + append_keys *args else - Log.error('Cannot type into element. Element is not present.') + _stomp_input_text *args + field_empty_afterward? end end + alias_method :text=, :send_keys + + def location element.location end def hover_over @@ -200,20 +236,10 @@ def text #this is used for text based elements element.text end - def text=(text) - element.clear - element.send_keys(text) - end - - def value - #this is used for inputs and forms - element.attribute("value") - end - # # Search for an element within this element # # @param [Symbol] by (:css or :xpath) # @param [String] locator @@ -316,6 +342,64 @@ @element.disabled? rescue StandardError Log.warn("Stale element detected.... #{self.to_s}") return true end + + # + # helper to clear input and put new text in + # + + def _stomp_input_text(*args) + Log.debug("Clearing \"#{value}\" from element: (#{self})") + element.clear + sleep @text_padding_time + Log.debug("Typing: #{args} into element: (#{self}).") + element.send_keys(*args) + sleep @text_padding_time + end + + # + # raise error if the field is empty after we sent it values + # TODO: verify if text correct afterward, but need to be able to handle cases + # of symbols like :space and :enter correctly + # + + def field_empty_afterward?(*args) + check_again = (not args.empty? and no_symbols? *args) + field_is_empty_but_should_not_be = (check_again and field_empty?) + if field_is_empty_but_should_not_be + raise "Browser Error: tried to input #{args} but found an empty string afterward: #{actual_text}" + end + end + + def field_empty? + value.empty? + end + + # + # helper to check if *args to send_keys has any symbols + # if so, don't bother trying to validate the text afterward + # + + def no_symbols?(*args) + symbols = args.select { |_| _.is_a? Symbol } + if symbols.length > 0 + return false + end + true + end + + # + # helper to check if *args to send_keys has only symbols + # if so, don't bother clearing the field first + # + + def only_symbols?(*args) + symbols = args.select { |_| _.is_a? Symbol } + if symbols.length == args.length + return true + end + false + end + end