Class: Puppeteer::Page

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#puppetmaster_sessionObject

Returns the value of attribute puppetmaster_session.



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

def puppetmaster_session
  @puppetmaster_session
end

Instance Method Details

#_original_gotoObject



83
# File 'lib/isomorfeus/puppeteer/page.rb', line 83

alias_method :_original_goto, :goto

#await_ruby(ruby_source = '', file = nil, line = nil, &block) ⇒ Object



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

def await_ruby(ruby_source = '', file = nil, line = nil, &block)
  main_frame.await_ruby(ruby_source, file, line, &block)
end

#check_el(selector) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/isomorfeus/puppeteer/page.rb', line 8

def check_el(selector)
  el = query_selector(selector) rescue nil
  el = Sx1(selector) rescue nil if el.nil?
  el = Sx1("//*[@name=\"#{selector}\"]") rescue nil if el.nil?
  el = Sx1("//*[@title=\"#{selector}\"]") rescue nil if el.nil?
  return el.check if el.is_a?(Puppeteer::ElementHandle)
  false
end

#click_el(selector, button: 'left', click_count: 1, delay: nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/isomorfeus/puppeteer/page.rb', line 26

def click_el(selector, button: 'left', click_count: 1, delay: nil)
  el = query_selector(selector) rescue nil
  return click(selector, button: button, click_count: click_count, delay: delay) if el.is_a?(Puppeteer::ElementHandle)
  el = Sx1(selector) rescue 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?
  return el.click(button: button, click_count: click_count, delay: delay) if el.is_a?(Puppeteer::ElementHandle)
  false
end

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



38
39
40
# File 'lib/isomorfeus/puppeteer/page.rb', line 38

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

#double_click_el(selector, button: 'left', delay: nil) ⇒ Object



42
43
44
# File 'lib/isomorfeus/puppeteer/page.rb', line 42

def double_click_el(selector, button: 'left', delay: nil)
  click_el(selector, button: button, click_count: 2, delay: delay)
end

#eval(j, *args) ⇒ Object



62
63
64
65
66
# File 'lib/isomorfeus/puppeteer/page.rb', line 62

def eval(j, *args)
  r = evaluate_handle("function() { return #{j.strip} }", *args)
  return r if r.is_a?(Puppeteer::ElementHandle)
  r&.json_value
end

#eval_ruby(ruby_source = '', file = nil, line = nil, &block) ⇒ Object



46
47
48
# File 'lib/isomorfeus/puppeteer/page.rb', line 46

def eval_ruby(ruby_source = '', file = nil, line = nil, &block)
  main_frame.eval_ruby(ruby_source, file, line, &block)
end

#eval_with_opal(ruby_source = '', file = nil, line = nil, &block) ⇒ Object



50
51
52
# File 'lib/isomorfeus/puppeteer/page.rb', line 50

def eval_with_opal(ruby_source = '', file = nil, line = nil, &block)
  main_frame.eval_with_opal(ruby_source, file, line, &block)
end

#exec(j, *args) ⇒ Object



58
59
60
# File 'lib/isomorfeus/puppeteer/page.rb', line 58

def exec(j, *args)
  evaluate("function() { #{j.strip} }", *args)
end

#find_el(selector) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/isomorfeus/puppeteer/page.rb', line 73

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

#goto(uri, referer: nil, timeout: nil, wait_until: nil) ⇒ Object Also known as: visit



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/isomorfeus/puppeteer/page.rb', line 85

def goto(uri, referer: nil, timeout: nil, wait_until: nil)
  parsed_uri = URI.parse(uri)
  app = Isomorfeus::Puppetmaster.served_app
  unless parsed_uri.host && parsed_uri.scheme
    parsed_uri.host = app.host
    parsed_uri.port = app.port
    parsed_uri.scheme = app.scheme
  end
  response = _original_goto(parsed_uri.to_s, referer: referer, timeout: timeout, wait_until: wait_until)
  puppetmaster_session.response = response if puppetmaster_session
  response
end

#has_content?(c) ⇒ Boolean

rspec matcher methods

Returns:

  • (Boolean)


183
184
185
186
187
# File 'lib/isomorfeus/puppeteer/page.rb', line 183

def has_content?(c)
  return content.match?(c) if c.is_a?(Regexp)
  return content.include?(c) if c.is_a?(String)
  content.include?(c.to_s)
end

#has_no_content?(c) ⇒ Boolean

Returns:

  • (Boolean)


189
190
191
# File 'lib/isomorfeus/puppeteer/page.rb', line 189

def has_no_content?(c)
  !has_content?(c)
end

#has_no_path?(p) ⇒ Boolean

Returns:

  • (Boolean)


198
199
200
# File 'lib/isomorfeus/puppeteer/page.rb', line 198

def has_no_path?(p)
  !has_path?(p)
end

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

Returns:

  • (Boolean)


213
214
215
# File 'lib/isomorfeus/puppeteer/page.rb', line 213

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

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

Returns:

  • (Boolean)


229
230
231
# File 'lib/isomorfeus/puppeteer/page.rb', line 229

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_title?(t) ⇒ Boolean

Returns:

  • (Boolean)


239
240
241
# File 'lib/isomorfeus/puppeteer/page.rb', line 239

def has_no_title?(t)
  !has_title?(t)
end

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

Returns:

  • (Boolean)


249
250
251
# File 'lib/isomorfeus/puppeteer/page.rb', line 249

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

#has_path?(p) ⇒ Boolean

Returns:

  • (Boolean)


193
194
195
196
# File 'lib/isomorfeus/puppeteer/page.rb', line 193

def has_path?(p)
  return path.match?(p) if p.is_a?(Regexp)
  path == p
end

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

Returns:

  • (Boolean)


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

def has_selector?(s, count: nil, between: nil, max: nil, min: nil)
  n = nil
  n = query_selector_all(s).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 true if query_selector(s).is_a?(Puppeteer::ElementHandle)
  false
end

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

Returns:

  • (Boolean)


217
218
219
220
221
222
223
224
225
226
227
# File 'lib/isomorfeus/puppeteer/page.rb', line 217

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_title?(t) ⇒ Boolean

Returns:

  • (Boolean)


233
234
235
236
237
# File 'lib/isomorfeus/puppeteer/page.rb', line 233

def has_title?(t)
  ti = title
  return ti.match?(t) if t.is_a?(Regexp)
  ti == t
end

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

Returns:

  • (Boolean)


243
244
245
246
247
# File 'lib/isomorfeus/puppeteer/page.rb', line 243

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

#inner_textObject Also known as: text



99
100
101
# File 'lib/isomorfeus/puppeteer/page.rb', line 99

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

#isomorphic(ruby_source = '', &block) ⇒ Object



104
105
106
107
108
# File 'lib/isomorfeus/puppeteer/page.rb', line 104

def isomorphic(ruby_source = '', &block)
  ruby_source, file, line = Isomorfeus::Puppetmaster.block_source_code(&block) if block_given?
  Isomorfeus::Puppetmaster.served_app.on_server(ruby_source, file, line)
  eval_ruby(ruby_source, file, line)
end

#isomorphic_with_opal(ruby_source = '', &block) ⇒ Object



110
111
112
113
114
# File 'lib/isomorfeus/puppeteer/page.rb', line 110

def isomorphic_with_opal(ruby_source = '', &block)
  ruby_source, file, line = Isomorfeus::Puppetmaster.block_source_code(&block) if block_given?
  Isomorfeus::Puppetmaster.served_app.on_server(ruby_source, file, line)
  eval_with_opal(ruby_source, file, line)
end

#pathObject



116
117
118
# File 'lib/isomorfeus/puppeteer/page.rb', line 116

def path
  URI.parse(url).path.to_s
end

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



120
121
122
# File 'lib/isomorfeus/puppeteer/page.rb', line 120

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

#right_click_el(selector, click_count: 1, delay: nil) ⇒ Object



124
125
126
# File 'lib/isomorfeus/puppeteer/page.rb', line 124

def right_click_el(selector, click_count: 1, delay: nil)
  click_el(selector, button: 'right', click_count: click_count, delay: delay)
end

#save_page(f) ⇒ Object



128
129
130
131
132
# File 'lib/isomorfeus/puppeteer/page.rb', line 128

def save_page(f)
  raise "Invalid filename '#{f}'!" if f.include?('..')
  raise "Invalid Isomorfeus::Puppetmaster.download_path!" unless Isomorfeus::Puppetmaster.download_path.is_a?(String)
  File.write(File.join(Isomorfeus::Puppetmaster.download_path, f), content)
end

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



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

def save_pdf(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)
  pdf(options)
end

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



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

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

#select_el(selector, *args) ⇒ Object



149
150
151
152
153
154
155
156
157
# File 'lib/isomorfeus/puppeteer/page.rb', line 149

def select_el(selector, *args)
  el = query_selector(selector) rescue nil
  return select(selector, *args) if el.is_a?(Puppeteer::ElementHandle)
  el = Sx1(selector) rescue nil
  el = Sx1("//*[@name=\"#{selector}\"]") rescue nil if el.nil?
  el = Sx1("//*[@title=\"#{selector}\"]") rescue nil if el.nil?
  return el.select if el.is_a?(Puppeteer::ElementHandle)
  false
end

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



4
5
6
# File 'lib/isomorfeus/puppeteer/page.rb', line 4

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

#type_text_el(selector, texttt, delay: nil, clear: true) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/isomorfeus/puppeteer/page.rb', line 159

def type_text_el(selector, texttt, delay: nil, clear: true)
  el = query_selector(selector) rescue nil
  if el.is_a?(Puppeteer::ElementHandle)
    if clear
      el.type_text(' ') if texttt == ''
      el.set_value('')
    end
    return type_text(selector, texttt.to_s, delay: delay)
  end
  el = Sx1(selector) rescue nil
  el = Sx1("//*[@name=\"#{selector}\"]") rescue nil if el.nil?
  el = Sx1("//*[@title=\"#{selector}\"]") rescue nil if el.nil?
  if el.is_a?(Puppeteer::ElementHandle)
    if clear
      el.type_text(' ') if texttt == ''
      el.set_value('')
    end
    return el.type_text(texttt.to_s, delay: delay)
  end
  false
end

#uncheck_el(selector) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/isomorfeus/puppeteer/page.rb', line 17

def uncheck_el(selector)
  el = query_selector(selector) rescue nil
  el = Sx1(selector) rescue nil if el.nil?
  el = Sx1("//*[@name=\"#{selector}\"]") rescue nil if el.nil?
  el = Sx1("//*[@title=\"#{selector}\"]") rescue nil if el.nil?
  return el.uncheck if el.is_a?(Puppeteer::ElementHandle)
  false
end