Module: Watir::JSExecution
- Included in:
- Element
- Defined in:
- lib/watir/js_execution.rb
Instance Method Summary collapse
-
#execute_script(script, *args, function_name: nil) ⇒ Object
Delegates script execution to Browser or IFrame.
-
#fire_event(event_name) ⇒ Object
Simulates JavaScript events on element.
-
#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.
-
#focus ⇒ Object
Focuses element.
-
#inner_html ⇒ String
Returns inner HTML code of element.
-
#inner_text ⇒ String
Returns inner Text code of element.
-
#outer_html ⇒ String
(also: #html)
Returns outer (inner + element itself) HTML code of element.
-
#select_text(str) ⇒ Object
Selects text on page (as if dragging clicked mouse across provided text).
-
#selected_text ⇒ Object
Selects text on page (as if dragging clicked mouse across provided text).
-
#text_content ⇒ String
Returns text content of element.
Instance Method Details
#execute_script(script, *args, function_name: nil) ⇒ Object
Delegates script execution to Browser or IFrame.
8 9 10 |
# File 'lib/watir/js_execution.rb', line 8 def execute_script(script, *args, function_name: nil) @query_scope.execute_script(script, *args, function_name: function_name) end |
#fire_event(event_name) ⇒ Object
Simulates JavaScript events on element. Note that you may omit “on” from event name.
24 25 26 27 28 |
# File 'lib/watir/js_execution.rb', line 24 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.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/watir/js_execution.rb', line 52 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 |
#focus ⇒ Object
Focuses element. Note that Firefox queues focus events until the window actually has focus.
82 83 84 |
# File 'lib/watir/js_execution.rb', line 82 def focus element_call { execute_js(:focus, @element) } end |
#inner_html ⇒ String
Returns inner HTML code of element.
96 97 98 |
# File 'lib/watir/js_execution.rb', line 96 def inner_html element_call { execute_js(:getInnerHtml, @element) }.strip end |
#inner_text ⇒ String
Returns inner Text code of element.
110 111 112 |
# File 'lib/watir/js_execution.rb', line 110 def inner_text element_call { execute_js(:getInnerText, @element) }.strip end |
#outer_html ⇒ String Also known as: html
Returns outer (inner + element itself) HTML code of element.
124 125 126 |
# File 'lib/watir/js_execution.rb', line 124 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).
150 151 152 |
# File 'lib/watir/js_execution.rb', line 150 def select_text(str) element_call { execute_js :selectText, @element, str } end |
#selected_text ⇒ Object
Selects text on page (as if dragging clicked mouse across provided text).
161 162 163 |
# File 'lib/watir/js_execution.rb', line 161 def selected_text element_call { execute_js :selectedText } end |
#text_content ⇒ String
Returns text content of element.
139 140 141 |
# File 'lib/watir/js_execution.rb', line 139 def text_content element_call { execute_js(:getTextContent, @element) }.strip end |