Class: Puppeteer::ElementHandle

Inherits:
Object
  • Object
show all
Defined in:
lib/isomorfeus/puppeteer/element_handle.rb

Instance Method Summary collapse

Instance Method Details

#==(o) ⇒ Object



2
3
4
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 2

def ==(o)
  evaluate('(eh, oh) => { return (eh == oh); }', o) rescue false
end

#attribute(a) ⇒ Object



10
11
12
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 10

def attribute(a)
  evaluate("(eh) => { return eh.getAttribute('#{a.to_s}'); }")
end

#checkObject



18
19
20
21
22
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 18

def check
  return false if disabled? || !visible?
  evaluate("(eh) => { eh.click(); eh.setAttribute('checked', ''); eh.checked = true; }")
  true
end

#checked?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 14

def checked?
  evaluate("(eh) => { return eh.checked; }")
end

#computed_styleObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 30

def computed_style
  evaluate <<~JAVASCRIPT
  (eh) => {
    let cs = window.getComputedStyle(eh);
    let ro = {};
    let pr;
    for (let i = 0; i<cs.length; i++) {
      pr = cs[i];
      ro[pr] = cs.getPropertyValue(pr);
    }
    return ro
  }
  JAVASCRIPT
end

#disabled?Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 45

def disabled?
  d = attribute('disabled')
  return true if d.is_a?(String)
  o = evaluate('(eh) => { if (eh.closest("[disabled]")!=null) { return true; }; return false; }')
  return true if o
  return false if d.nil?
  return d
end

#double_click(button: 'left', delay: nil, offset: nil) ⇒ Object



54
55
56
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 54

def double_click(button: 'left', delay: nil, offset: nil)
  click(button: button, click_count: 2, delay: delay, offset: offset)
end

#eval(j, *args) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 74

def eval(j, *args)
  js = <<~JAVASCRIPT
  function(eh) {
    let args = [];
    for (let i=1; i<arguments.length; i++) {
      args[i-1] = arguments[i];
    }
    let _f = function() {
      return #{j.strip}
    }
    return _f.apply(eh, args);
  }
  JAVASCRIPT
  r = evaluate_handle(js, *args)
  return r if r.is_a?(Puppeteer::ElementHandle)
  r&.json_value
end

#exec(j, *args) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 58

def exec(j, *args)
  js = <<~JAVASCRIPT
  function(eh) {
    let args = [];
    for (let i=1; i<arguments.length; i++) {
      args[i-1] = arguments[i];
    }
    let _f = function() {
      #{j.strip}
    }
    _f.apply(eh, args);
  }
  JAVASCRIPT
  evaluate(js, *args)
end

#find_el(selector) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 97

def find_el(selector)
  el = query_selector(selector) rescue nil
  el = Sx1(selector) rescue nil if el.nil?
  el = Sx1("//*[normalize-space(text())=\"#{selector}\"]") rescue nil if el.nil?
  el = Sx1("//*[@name=\"#{selector}\"]") rescue nil if el.nil?
  el = Sx1("//*[@title=\"#{selector}\"]") rescue nil if el.nil?
  el = Sx1("//*[@value=\"#{selector}\"]") rescue nil if el.nil?
  el
end

#has_field?(s) ⇒ Boolean

rspec matcher methods

Returns:

  • (Boolean)


175
176
177
178
179
180
181
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 175

def has_field?(s)
  eh = query_selector(s)
  return false unless eh.is_a?(Puppeteer::ElementHandle)
  tu = eh.tag_name.upcase
  return true if tu == 'INPUT' || tu == 'TEXTAREA'
  false
end

#has_no_field?(s) ⇒ Boolean

Returns:

  • (Boolean)


183
184
185
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 183

def has_no_field?(s)
  !has_field?(s)
end

#has_no_selector?(s, count: nil) ⇒ Boolean

Returns:

  • (Boolean)


197
198
199
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 197

def has_no_selector?(s, count: nil)
  !has_selector?(s, count: count)
end

#has_no_text?(t, count: nil, between: nil, max: nil, min: nil) ⇒ Boolean

Returns:

  • (Boolean)


226
227
228
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 226

def has_no_text?(t, count: nil, between: nil, max: nil, min: nil)
  !has_text?(t, count: count, between: between, max: max, min: min)
end

#has_no_xpath?(x, count: nil) ⇒ Boolean

Returns:

  • (Boolean)


236
237
238
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 236

def has_no_xpath?(x, count: nil)
  !has_xpath?(x, count: count)
end

#has_selected?(v) ⇒ Boolean

Returns:

  • (Boolean)


187
188
189
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 187

def has_selected?(v)
  selected_options.include?(v)
end

#has_selector?(s, count: nil) ⇒ Boolean

Returns:

  • (Boolean)


191
192
193
194
195
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 191

def has_selector?(s, count: nil)
  return query_selector_all(s).size == count.to_i unless count.nil?
  return true if query_selector(s).is_a?(Puppeteer::ElementHandle)
  false
end

#has_style?(sh) ⇒ Boolean

Returns:

  • (Boolean)


201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 201

def has_style?(sh)
  s = computed_style
  m = true
  sh.each do |k,v|
    if v.is_a?(Regexp)
      m = false unless s[k.to_s].match?(v)
    else
      m = false unless s[k.to_s] == v
    end
  end
  m
end

#has_text?(t, count: nil, between: nil, max: nil, min: nil) ⇒ Boolean

Returns:

  • (Boolean)


214
215
216
217
218
219
220
221
222
223
224
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 214

def has_text?(t, count: nil, between: nil, max: nil, min: nil)
  c = inner_text
  n = nil
  n = c.scan(t).size if count || between || max || min
  return n == count.to_i if count
  return n <= max.to_i if max
  return n >= min.to_i if min
  return between.include?(n) if between
  return c.match?(t) if t.is_a?(Regexp)
  c.include?(t.to_s)
end

#has_xpath?(x, count: nil) ⇒ Boolean

Returns:

  • (Boolean)


230
231
232
233
234
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 230

def has_xpath?(x, count: nil)
  e = self.Sx(x)
  return e.any? if count.nil?
  return e.size == count
end

#idObject



107
108
109
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 107

def id
  evaluate('(eh) => { return eh.id; }')
end

#inner_htmlObject



111
112
113
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 111

def inner_html
  evaluate('(eh) => { return eh.innerHTML; }')
end

#inner_textObject Also known as: text, text_content



115
116
117
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 115

def inner_text
  evaluate('(eh) => { return eh.innerText; }')&.strip&.tr("\u00a0", ' ')
end

#outer_htmlObject



121
122
123
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 121

def outer_html
  evaluate('(eh) => { return eh.outerHTML; }')
end

#prop(p) ⇒ Object



125
126
127
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 125

def prop(p)
  self[p.to_s].json_value
end

#right_click(click_count: 1, delay: nil, offset: nil) ⇒ Object



129
130
131
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 129

def right_click(click_count: 1, delay: nil, offset: nil)
  click(button: 'right', click_count: click_count, delay: delay, offset: offset)
end

#save_screenshot(f, options = {}) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 133

def save_screenshot(f, options = {})
  raise "Invalid filename '#{f}'!" if f.include?('..')
  raise "Invalid Isomorfeus::Puppetmaster.download_path!" unless Isomorfeus::Puppetmaster.download_path.is_a?(String)
  options[:path] = File.join(Isomorfeus::Puppetmaster.download_path, f)
  options[:type] = 'png' unless options.key?(:type)
  screenshot(**options)
end

#selected?Boolean

Returns:

  • (Boolean)


141
142
143
144
145
146
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 141

def selected?
  c = attribute('selected')
  return true if c.is_a?(String)
  return false if c.nil?
  return c
end

#selected_optionsObject



148
149
150
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 148

def selected_options
  evaluate('(eh) => { let a=[]; let o = eh.selectedOptions; for (let i=0; i<o.length; i++) { a.push(o[i].value) }; return a; }')
end

#set_inner_html(c) ⇒ Object



152
153
154
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 152

def set_inner_html(c)
  evaluate('(eh, c) => { if (eh.readOnly) {throw "Element is read only!" }; return eh.innerHTML = c; }', c)
end

#set_value(v) ⇒ Object Also known as: value=



156
157
158
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 156

def set_value(v)
  evaluate('(eh, v) => { if (eh.readOnly) {throw "Element is read only!" }; return eh.value = v; }', v)
end

#Sx1(x) ⇒ Object Also known as: find_xpath



6
7
8
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 6

def Sx1(x)
  self.Sx(x).first
end

#tag_nameObject



161
162
163
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 161

def tag_name
  evaluate('(eh) => { return eh.tagName; }').downcase
end

#uncheckObject



24
25
26
27
28
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 24

def uncheck
  return false if disabled? || !visible?
  evaluate("(eh) => { eh.click(); eh.removeAttribute('checked'); eh.checked = false; }")
  true
end

#valueObject



165
166
167
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 165

def value
  evaluate('(eh) => { return eh.value; }')
end

#visible?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/isomorfeus/puppeteer/element_handle.rb', line 169

def visible?
  evaluate('(eh) => { return !!(( eh.offsetWidth || eh.offsetHeight || eh.getClientRects().length) && (window.getComputedStyle(eh).visibility !== "hidden" && window.getComputedStyle(eh).display !== "none" )); }')
end