Module: Capybarista::Extensions::Element
Instance Method Summary collapse
- #blur ⇒ Object
-
#filtered_attributes(*keys) ⇒ Object
Returns a map containing only the specified attributes.
- #focus ⇒ Object
- #highlight ⇒ Object
- #inner_html ⇒ Object
-
#label ⇒ Object
Attempts to find the label for the current element.
-
#label! ⇒ Object
Attempts to find the label for the current element.
-
#labels ⇒ Object
Returns 0 or more labels for the current element.
- #outer_html ⇒ Object
-
#scoped ⇒ Object
Syntactic sugar.
- #top_left ⇒ Object
- #unique_xpath ⇒ Object
Methods included from Base
Instance Method Details
#blur ⇒ Object
180 181 182 183 |
# File 'lib/capybarista/extensions.rb', line 180 def blur script = %Q{ (function() { var target = #{ J.find_xpath(unique_xpath) }; if(target) { var evt = document.createEvent("MouseEvents"); evt.initMouseEvent("blur", true, true,window, 1, 1, 1, 1, 1, false, false, false, false, 0, target); target.dispatchEvent(evt); } })(); } session.execute_script(script) end |
#filtered_attributes(*keys) ⇒ Object
Returns a map containing only the specified attributes. If the element does not contain an attribute, then its key/value pairing will be omitted.
FIXME: This methods filters attributes that are equal
to the empty string :-(
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/capybarista/extensions.rb', line 84 def filtered_attributes(*keys) retval = {} keys.each do |k| value = self[k] if value and not value.empty? retval[k] = value end end return retval end |
#focus ⇒ Object
186 187 188 189 |
# File 'lib/capybarista/extensions.rb', line 186 def focus script = %Q{ (function() { var target = #{ J.find_xpath(unique_xpath) }; if(target) { var evt = document.createEvent("MouseEvents"); evt.initMouseEvent("focus", true, true,window, 1, 1, 1, 1, 1, false, false, false, false, 0, target); target.dispatchEvent(evt); } })(); } session.execute_script(script) end |
#highlight ⇒ Object
175 176 177 |
# File 'lib/capybarista/extensions.rb', line 175 def highlight session.execute_script %Q{(function(){ var result = #{ J.find_xpath(unique_xpath) }; if(result) { var old_color = result.style.backgroundColor; result.style.backgroundColor = "yellow"; setTimeout(function(){ result.style.backgroundColor = old_color; }, 1000); } }()); } end |
#inner_html ⇒ Object
165 166 167 |
# File 'lib/capybarista/extensions.rb', line 165 def inner_html session.evaluate_script %Q{(function(){ var result = #{ J.find_xpath(unique_xpath) }; if(result) { return result.innerHTML; } }()); } end |
#label ⇒ Object
Attempts to find the label for the current element. If no label exists, then return nil.
126 127 128 129 130 131 132 133 134 |
# File 'lib/capybarista/extensions.rb', line 126 def label ids = filtered_attributes(:id, :name).values if ids.any? query = ::Queries::XPath.labels_for(*ids) return session.first(:xpath, query) else return nil end end |
#label! ⇒ Object
Attempts to find the label for the current element. If no label exists, then raise Capybara::ElementNotFound
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/capybarista/extensions.rb', line 112 def label! ids = filtered_attributes(:id, :name).values if ids.any? query = ::Queries::XPath.labels_for(*ids) return session.find(:xpath, query) else raise ::ElementNotFound, "The element has no labels" end end |
#labels ⇒ Object
Returns 0 or more labels for the current element
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/capybarista/extensions.rb', line 98 def labels ids = filtered_attributes(:id, :name).values if ids.any? query = ::Queries::XPath.labels_for(*ids) return session.all(:xpath, query) else return [] end end |
#outer_html ⇒ Object
170 171 172 |
# File 'lib/capybarista/extensions.rb', line 170 def outer_html session.evaluate_script %Q{(function(){ var result = #{ J.find_xpath(unique_xpath) }; if(result) { return result.outerHTML; } }()); } end |
#scoped ⇒ Object
Syntactic sugar. Yum!
73 74 75 76 |
# File 'lib/capybarista/extensions.rb', line 73 def scoped s = session s.within(self){ yield s } end |
#top_left ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/capybarista/extensions.rb', line 136 def top_left long_function = %Q{ ( function(){ var obj = document.evaluate("#{unique_xpath}", document, null, XPathResult.ANY_TYPE, null ).iterateNext(); if(obj) { var curleft = 0; var curtop = 0; if (obj && obj.offsetParent) { do { curleft += obj.offsetLeft; curtop += obj.offsetTop; } while (obj = obj.offsetParent); } return [curleft,curtop]; }; }() ); } long_string = long_function.delete("\n") session.evaluate_script(long_string) end |
#unique_xpath ⇒ Object
161 162 163 |
# File 'lib/capybarista/extensions.rb', line 161 def unique_xpath ::UniqueXPath.for(self) end |