Class: Lightpanda::Capybara::Node
- Inherits:
-
Capybara::Driver::Node
- Object
- Capybara::Driver::Node
- Lightpanda::Capybara::Node
- Defined in:
- lib/lightpanda/capybara/node.rb
Instance Attribute Summary collapse
-
#selector_info ⇒ Object
readonly
Returns the value of attribute selector_info.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](name) ⇒ Object
- #checked? ⇒ Boolean
- #click(_keys = [], **_options) ⇒ Object
- #disabled? ⇒ Boolean
- #double_click(_keys = [], **_options) ⇒ Object
- #find_css(selector) ⇒ Object
- #find_xpath(selector) ⇒ Object
- #hover ⇒ Object
-
#initialize(driver, native, _index) ⇒ Node
constructor
A new instance of Node.
- #multiple? ⇒ Boolean
- #path ⇒ Object
- #readonly? ⇒ Boolean
- #right_click(_keys = [], **_options) ⇒ Object
- #select_option ⇒ Object
- #selected? ⇒ Boolean
- #send_keys(*args) ⇒ Object
- #set(value, **_options) ⇒ Object
- #style(styles) ⇒ Object
- #tag_name ⇒ Object
- #text ⇒ Object
- #unselect_option ⇒ Object
- #value ⇒ Object
- #visible? ⇒ Boolean
- #visible_text ⇒ Object
Constructor Details
#initialize(driver, native, _index) ⇒ Node
Returns a new instance of Node.
8 9 10 11 12 |
# File 'lib/lightpanda/capybara/node.rb', line 8 def initialize(driver, native, _index) super(driver, native) @selector_info = native # { selector: "h1", index: 0 } end |
Instance Attribute Details
#selector_info ⇒ Object (readonly)
Returns the value of attribute selector_info.
6 7 8 |
# File 'lib/lightpanda/capybara/node.rb', line 6 def selector_info @selector_info end |
Instance Method Details
#==(other) ⇒ Object
208 209 210 |
# File 'lib/lightpanda/capybara/node.rb', line 208 def ==(other) other.is_a?(self.class) && selector_info == other.selector_info end |
#[](name) ⇒ Object
22 23 24 |
# File 'lib/lightpanda/capybara/node.rb', line 22 def [](name) evaluate_on("this.getAttribute(#{name.to_s.inspect})") end |
#checked? ⇒ Boolean
144 145 146 |
# File 'lib/lightpanda/capybara/node.rb', line 144 def checked? evaluate_on("this.checked") end |
#click(_keys = [], **_options) ⇒ Object
43 44 45 |
# File 'lib/lightpanda/capybara/node.rb', line 43 def click(_keys = [], **) evaluate_on("this.click()") end |
#disabled? ⇒ Boolean
152 153 154 |
# File 'lib/lightpanda/capybara/node.rb', line 152 def disabled? evaluate_on("this.disabled") end |
#double_click(_keys = [], **_options) ⇒ Object
53 54 55 56 57 |
# File 'lib/lightpanda/capybara/node.rb', line 53 def double_click(_keys = [], **) evaluate_on(" this.dispatchEvent(new MouseEvent('dblclick', {bubbles: true, cancelable: true}))\n JS\nend\n") |
#find_css(selector) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/lightpanda/capybara/node.rb', line 196 def find_css(selector) count = evaluate_on("this.querySelectorAll(#{selector.inspect}).length") return [] if count.nil? || count.zero? (0...count).map do |idx| child_selector = "#{element_selector} #{selector}" Node.new(driver, { selector: child_selector, index: idx }, idx) end end |
#find_xpath(selector) ⇒ Object
192 193 194 |
# File 'lib/lightpanda/capybara/node.rb', line 192 def find_xpath(selector) driver.find_xpath(selector) end |
#hover ⇒ Object
59 60 61 62 63 |
# File 'lib/lightpanda/capybara/node.rb', line 59 def hover evaluate_on(" this.dispatchEvent(new MouseEvent('mouseover', {bubbles: true, cancelable: true}))\n JS\nend\n") |
#multiple? ⇒ Boolean
160 161 162 |
# File 'lib/lightpanda/capybara/node.rb', line 160 def multiple? evaluate_on("this.multiple") end |
#path ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/lightpanda/capybara/node.rb', line 164 def path evaluate_on(" (function(el) {\n if (!el) return '';\n var path = [];\n\n while (el && el.nodeType === Node.ELEMENT_NODE) {\n var selector = el.nodeName.toLowerCase();\n if (el.id) {\n selector += '#' + el.id;\n path.unshift(selector);\n break;\n } else {\n var sibling = el;\n var nth = 1;\n while (sibling = sibling.previousElementSibling) {\n if (sibling.nodeName.toLowerCase() === selector) nth++;\n }\n if (nth > 1) selector += ':nth-of-type(' + nth + ')';\n }\n path.unshift(selector);\n el = el.parentNode;\n }\n return path.join(' > ');\n })(this)\n JS\nend\n") |
#readonly? ⇒ Boolean
156 157 158 |
# File 'lib/lightpanda/capybara/node.rb', line 156 def readonly? evaluate_on("this.readOnly") end |
#right_click(_keys = [], **_options) ⇒ Object
47 48 49 50 51 |
# File 'lib/lightpanda/capybara/node.rb', line 47 def right_click(_keys = [], **) evaluate_on(" this.dispatchEvent(new MouseEvent('contextmenu', {bubbles: true, cancelable: true}))\n JS\nend\n") |
#select_option ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/lightpanda/capybara/node.rb', line 87 def select_option evaluate_on(" this.selected = true;\n var event = new Event('change', {bubbles: true});\n this.parentElement.dispatchEvent(event);\n JS\nend\n") |
#selected? ⇒ Boolean
148 149 150 |
# File 'lib/lightpanda/capybara/node.rb', line 148 def selected? evaluate_on("this.selected") end |
#send_keys(*args) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/lightpanda/capybara/node.rb', line 109 def send_keys(*args) args.each do |key| next unless key.is_a?(String) evaluate_on(" this.focus();\n this.value += \#{key.inspect};\n this.dispatchEvent(new Event('input', {bubbles: true}));\n JS\n end\nend\n") |
#set(value, **_options) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/lightpanda/capybara/node.rb', line 65 def set(value, **) if tag_name == "input" type = self["type"] case type when "checkbox", "radio" if value evaluate_on("this.checked = true; this.dispatchEvent(new Event('change', {bubbles: true}))") else evaluate_on("this.checked = false; this.dispatchEvent(new Event('change', {bubbles: true}))") end when "file" raise NotImplementedError, "File inputs need special handling via CDP. File uploads not yet supported" else set_text_value(value) end elsif tag_name == "textarea" set_text_value(value) elsif self["contenteditable"] evaluate_on("this.innerHTML = #{value.to_s.inspect}") end end |
#style(styles) ⇒ Object
37 38 39 40 41 |
# File 'lib/lightpanda/capybara/node.rb', line 37 def style(styles) styles.each_with_object({}) do |style, result| result[style] = evaluate_on("window.getComputedStyle(this).#{style}") end end |
#tag_name ⇒ Object
121 122 123 |
# File 'lib/lightpanda/capybara/node.rb', line 121 def tag_name evaluate_on("this.tagName.toLowerCase()") end |
#text ⇒ Object
14 15 16 |
# File 'lib/lightpanda/capybara/node.rb', line 14 def text evaluate_on("this.textContent") end |
#unselect_option ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/lightpanda/capybara/node.rb', line 95 def unselect_option select = find_xpath("./ancestor::select")[0] if select && !select["multiple"] raise ::::UnselectNotAllowed, "Cannot unselect option from single select" end evaluate_on(" this.selected = false;\n var event = new Event('change', {bubbles: true});\n this.parentElement.dispatchEvent(event);\n JS\nend\n") |
#value ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/lightpanda/capybara/node.rb', line 26 def value evaluate_on(" (function(el) {\n if (el.tagName === 'SELECT' && el.multiple) {\n return Array.from(el.selectedOptions).map(o => o.value);\n }\n return el.value;\n })(this)\n JS\nend\n") |
#visible? ⇒ Boolean
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/lightpanda/capybara/node.rb', line 125 def visible? selector = @selector_info[:selector] index = @selector_info[:index] js = " (function() {\n var el = document.querySelectorAll(\#{selector.inspect})[\#{index}];\n if (!el) return false;\n var style = window.getComputedStyle(el);\n var isVisible = style.display !== 'none' &&\n style.visibility !== 'hidden' &&\n el.offsetParent !== null;\n return isVisible;\n })()\n JS\n\n driver.browser.evaluate(js)\nend\n" |
#visible_text ⇒ Object
18 19 20 |
# File 'lib/lightpanda/capybara/node.rb', line 18 def visible_text evaluate_on("this.innerText") end |