Class: Gridium::ElementExtensions

Inherits:
Object
  • Object
show all
Defined in:
lib/element_extensions.rb

Class Method Summary collapse

Class Method Details

.drag_to(source, target) ⇒ Object

Javascript HTML5 Drag n drop

Parameters:

  • source (Element)

    element (css locator required)

  • target (Element)

    element (css locator required)



54
55
56
57
58
# File 'lib/element_extensions.rb', line 54

def drag_to(source, target)
  dnd_js = File.read(File.join(File.dirname(__FILE__), "./js/dnd.js"))
  Log.debug("[GRIDIUM::ElementExtensions] dragging '#{source}' to '#{target}'")
  Driver.execute_script_driver(dnd_js + "$('#{source.locator}').simulateDragDrop({ dropTarget: '#{target.locator}'});")
end

.highlight(element) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/element_extensions.rb', line 5

def highlight(element)
  Log.debug("[GRIDIUM::ElementExtensions] Highlighting element...")
  original_border = Driver.execute_script("return arguments[0].style.border", element.element)
  original_background = Driver.execute_script("return arguments[0].style.backgroundColor", element.element)
  Driver.execute_script("arguments[0].style.border='3px solid lime'; return;", element.element)
  Driver.execute_script("arguments[0].style.backgroundColor='lime'; return;", element.element)
  sleep (Gridium.config.highlight_duration)
  Driver.execute_script("arguments[0].style.border='" + original_border + "'; return;", element.element)
  Driver.execute_script("arguments[0].style.backgroundColor='" + original_background + "'; return;", element.element)
end

.hover_away(element) ⇒ Object



22
23
24
25
# File 'lib/element_extensions.rb', line 22

def hover_away(element)
  Driver.execute_script("var evObj = document.createEvent('MouseEvents'); evObj.initMouseEvent(\"mouseout\",true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); arguments[0].dispatchEvent(evObj);", element.element)
  sleep 1
end

.jquery_click(element) ⇒ Object

Occasionaly selenium is unable to click on elements in the DOM which have some interesting React goodies around the element.



45
46
47
# File 'lib/element_extensions.rb', line 45

def jquery_click(element)
  Driver.execute_script("arguments[0].click().change();", element.element)
end

.mouse_over(element, x: 0, y: 0) ⇒ Object Also known as: hover_over

Mouse over the requested element at coordinate (Default x:0, y:0)

Parameters:

  • element (Element)
  • x (Integer) (defaults to: 0)
    • element x coordinate

  • y (Integer) (defaults to: 0)
    • element y coordinate



33
34
35
# File 'lib/element_extensions.rb', line 33

def mouse_over(element, x: 0, y: 0)
  Driver.driver.action.move_to(element.element, x, y).perform
end

.scroll_to(element) ⇒ Object



16
17
18
19
20
# File 'lib/element_extensions.rb', line 16

def scroll_to(element)
  Log.debug("[GRIDIUM::ElementExtensions] Scrolling element into view...")
  Driver.execute_script("arguments[0].scrollIntoView(); return;", element.element)
  sleep 1
end

.set_attribute(selector, attr, val) ⇒ Object

Use Javascript to set element attribute value from :id

Parameters:

  • selector (String)
    • css selector for find element by

  • attribute (String)
    • element attribute to set

  • value (String)
    • element value to set



66
67
68
# File 'lib/element_extensions.rb', line 66

def set_attribute(selector, attr, val)
  Driver.execute_script_driver("document.querySelectorAll('#{selector}')[0].setAttribute('#{attr}', '#{val}')")
end

.trigger_onblur(element) ⇒ Object



39
40
41
# File 'lib/element_extensions.rb', line 39

def trigger_onblur(element)
  Driver.execute_script("arguments[0].focus(); arguments[0].blur(); return true", element.element)
end