lib/element.rb in gridium-1.1.34 vs lib/element.rb in gridium-1.1.35

- old
+ new

@@ -3,10 +3,11 @@ require 'spec_data' class Element attr_reader :name, :by, :locator + class Gridium::InvalidTypeError < StandardError; end def initialize(name, by, locator, opts = {}) @name = name @by = by @locator = locator @@ -62,13 +63,13 @@ Log.debug "[GRIDIUM::Element] found #{elements.length} element(s) via #{@by} and #{@locator} and 0 are displayed" end rescue StandardError => error Log.debug("[GRIDIUM::Element] element.displayed_element rescued: #{error}") if found_element - Log.warn("[GRIDIUM::Element] An element was found, but it was not displayed on the page. Gridium.config.visible_elements_only set to: #{Gridium.config.visible_elements_only} Element: #{self.to_s}") + Log.warn("[GRIDIUM::Element] An element was found, but it was not displayed on the page. Gridium.config.visible_elements_only set to: #{Gridium.config.visible_elements_only} Element: #{self}") else - Log.warn("[GRIDIUM::Element] Could not find Element: #{self.to_s}") + Log.warn("[GRIDIUM::Element] Could not find Element: #{self}") end end found_element end @@ -91,10 +92,21 @@ def attribute(name) element.attribute(name) end + # Requires element to have an ID value, to successfully use `document.getElementById` + def set_attribute(name, value) + id = self.attribute('id') + if id.nil? || id.empty? + Log.warn("[GRIDIUM::Element] #{self} does not have an 'id'. Consider adding one.") + else + Log.debug("[GRIDIUM::Element] setting element attribute '#{name}' to '#{value}'") + ElementExtensions.set_attribute(id, name, value) + end + end + def css_value(name) element.css_value(name) end def present? @@ -202,11 +214,11 @@ def location_once_scrolled_into_view element.location_once_scrolled_into_view end def hover_over - Log.debug("[GRIDIUM::Element] Hovering over element (#{self.to_s})...") + Log.debug("[GRIDIUM::Element] Hovering over element (#{self})...") # @driver.mouse.move_to(element) # Note: Doesn't work with Selenium 2.42 bindings for Firefox v31 # @driver.action.move_to(element).perform # @driver.mouse_over(@locator) if element.enabled? $verification_passes += 1 @@ -215,51 +227,61 @@ Log.error('[GRIDIUM::Element] Cannot hover over element. Element is not present.') end end def hover_away - Log.debug("[GRIDIUM::Element] Hovering away from element (#{self.to_s})...") + Log.debug("[GRIDIUM::Element] Hovering away from element (#{self})...") if element.enabled? $verification_passes += 1 ElementExtensions.hover_away(self) # Javascript workaround to above issue else Log.error('[GRIDIUM::Element] Cannot hover away from element. Element is not present.') end end # Raw webdriver mouse over def mouse_over(x: 1, y: 1) - Log.debug("[GRIDIUM::Element] Triggering mouse over for (#{self.to_s})...") + Log.debug("[GRIDIUM::Element] Triggering mouse over for (#{self})...") if element.enabled? $verification_passes += 1 ElementExtensions.mouse_over(self, x: x, y: y) else Log.error('[GRIDIUM::Element] Cannot mouse over. Element is not present.') end end + # HTML5 Drag n drop + # @param [Element] target element to drag to + def drag_to(target) + raise Gridium::InvalidTypeError, "source element selector must be ':css'" unless self.by == :css + raise Gridium::InvalidTypeError, "target element selector must be ':css'" unless target.by == :css + + Log.debug("[GRIDIUM::Element] Dragging (#{self}) to (#{target})") + ElementExtensions.drag_to(self, target) + end + def scroll_into_view if element.enabled? $verification_passes += 1 ElementExtensions.scroll_to(self) else Log.error('[GRIDIUM::Element] Cannot scroll element into view. Element is not present.') end end def trigger_onblur - Log.debug("[GRIDIUM::Element] Triggering onblur for (#{self.to_s})...") + Log.debug("[GRIDIUM::Element] Triggering onblur for (#{self})...") if element.enabled? $verification_passes += 1 ElementExtensions.trigger_onblur(self) else Log.error('[GRIDIUM::Element] Cannot trigger onblur. Element is not present.') end end def jquery_click - Log.debug("[GRIDIUM::Element] JQuery clickin (#{self.to_s})...") + Log.debug("[GRIDIUM::Element] JQuery clickin (#{self})...") if element.enabled? $verification_passes += 1 ElementExtensions.jquery_click(self) else Log.error('[GRIDIUM::Element] Cannot jquery_click. Element is not present.') @@ -326,11 +348,11 @@ location_y = self.location.y element_width = self.size.width element_height = self.size.height # ChunkyPNG commands tap into oily_png (performance-enhanced version of chunky_png) - image = ChunkyPNG::Image.from_file(screenshot_path.to_s) + image = ChunkyPNG::Image.from_file(screenshot_path) image1 = image.crop(location_x, location_y, element_width, element_height) image2 = image1.to_image element_screenshot_path = File.join($current_run_dir, "#{name}__#{timestamp}.png") image2.save(element_screenshot_path) @element_screenshot = element_screenshot_path @@ -370,11 +392,11 @@ return false else return true end rescue StandardError => e - Log.error("There was a problem comparing element images. #{e.to_s}") + Log.error("There was a problem comparing element images. #{e.message}") end end def method_missing(method_sym, *arguments, &block) Log.debug("[GRIDIUM::Element] called #{method_sym} on element #{@locator} by #{@by_type}") @@ -390,10 +412,10 @@ def stale? return true if @element.nil? @element.displayed? rescue StandardError => error Log.debug("[GRIDIUM::Element] element.stale? is true because this error was rescued: #{error}") - Log.warn("[GRIDIUM::Element] Stale element detected.... #{self.to_s}") + Log.warn("[GRIDIUM::Element] Stale element detected.... #{self}") return true end # # helper to clear input and put new text in