Module: Watir::JSExecution

Included in:
Element
Defined in:
lib/watir/js_execution.rb

Instance Method Summary collapse

Instance Method Details

#execute_script(script, *args) ⇒ Object

Delegates script execution to Browser or IFrame.


6
7
8
# File 'lib/watir/js_execution.rb', line 6

def execute_script(script, *args)
  @query_scope.execute_script(script, *args)
end

#fire_event(event_name) ⇒ Object

Simulates JavaScript events on element. Note that you may omit “on” from event name.

Examples:

browser.button(name: "new_user_button").fire_event :click
browser.button(name: "new_user_button").fire_event "mousemove"
browser.button(name: "new_user_button").fire_event "onmouseover"

Parameters:


22
23
24
25
26
# File 'lib/watir/js_execution.rb', line 22

def fire_event(event_name)
  event_name = event_name.to_s.sub(/^on/, '').downcase

  element_call { execute_js :fireEvent, @element, event_name }
end

#flash(preset = :default, color: 'red', flashes: 10, delay: 0.1) ⇒ Watir::Element

Flashes (change background color to a new color and back a few times) element.

Examples:

browser.li(id: 'non_link_1').flash
browser.li(id: 'non_link_1').flash(color: "green", flashes: 3, delay: 0.05)
browser.li(id: 'non_link_1').flash(color: "yellow")
browser.li(id: 'non_link_1').flash(color: ["yellow", "green"])
browser.li(id: 'non_link_1').flash(flashes: 4)
browser.li(id: 'non_link_1').flash(delay: 0.1)
browser.li(id: 'non_link_1').flash(:fast)
browser.li(id: 'non_link_1').flash(:slow)
browser.li(id: 'non_link_1').flash(:rainbow)

Parameters:

  • preset (Symbol) (defaults to: :default)

    :fast, :slow, :long or :rainbow for pre-set values

  • color (String/Array) (defaults to: 'red')

    what 'color' or [colors] to flash with

  • flashes (Integer) (defaults to: 10)

    number of times element should be flashed

  • delay (Integer, Float) (defaults to: 0.1)

    how long to wait between flashes

Returns:


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/watir/js_execution.rb', line 50

def flash(preset = :default, color: 'red', flashes: 10, delay: 0.1)
  presets = {
    fast: {delay: 0.04},
    slow: {delay: 0.2},
    long: {flashes: 5, delay: 0.5},
    rainbow: {flashes: 5, color: %w[red orange yellow green blue indigo violet]}
  }
  return flash(**presets[preset]) unless presets[preset].nil?

  background_color = original_color = style('background-color')
  background_color = 'white' if background_color.empty?
  colors = Array(color).push(background_color)

  (colors * flashes).each do |next_color|
    element_call { execute_js(:backgroundColor, @element, next_color) }
    sleep(delay)
  end

  element_call { execute_js(:backgroundColor, @element, original_color) }

  self
end

#focusObject

Focuses element. Note that Firefox queues focus events until the window actually has focus.


80
81
82
# File 'lib/watir/js_execution.rb', line 80

def focus
  element_call { execute_js(:focus, @element) }
end

#inner_htmlString

Returns inner HTML code of element.

Examples:

browser.div(id: 'shown').inner_html
#=> "<div id=\"hidden\" style=\"display: none;\">Not shown</div><div>Not hidden</div>"

Returns:

  • (String)

94
95
96
# File 'lib/watir/js_execution.rb', line 94

def inner_html
  element_call { execute_js(:getInnerHtml, @element) }.strip
end

#inner_textString

Returns inner Text code of element.

Examples:

browser.div(id: 'shown').inner_text
#=> "Not hidden"

Returns:

  • (String)

108
109
110
# File 'lib/watir/js_execution.rb', line 108

def inner_text
  element_call { execute_js(:getInnerText, @element) }.strip
end

#outer_htmlString Also known as: html

Returns outer (inner + element itself) HTML code of element.

Examples:

browser.div(id: 'shown').outer_html
#=> "<div id=\"shown\"><div id=\"hidden\" style=\"display: none;\">Not shown</div><div>Not hidden</div></div>"

Returns:

  • (String)

122
123
124
# File 'lib/watir/js_execution.rb', line 122

def outer_html
  element_call { execute_js(:getOuterHtml, @element) }.strip
end

#select_text(str) ⇒ Object

Selects text on page (as if dragging clicked mouse across provided text).

Examples:

browser.li(id: 'non_link_1').select_text('Non-link')

148
149
150
# File 'lib/watir/js_execution.rb', line 148

def select_text(str)
  element_call { execute_js :selectText, @element, str }
end

#selected_textObject

Selects text on page (as if dragging clicked mouse across provided text).

Examples:

browser.li(id: 'non_link_1').selected_text

159
160
161
# File 'lib/watir/js_execution.rb', line 159

def selected_text
  element_call { execute_js :selectedText }
end

#text_contentString

Returns text content of element.

Examples:

browser.div(id: 'shown').text_content
#=> "Not shownNot hidden"

Returns:

  • (String)

137
138
139
# File 'lib/watir/js_execution.rb', line 137

def text_content
  element_call { execute_js(:getTextContent, @element) }.strip
end