Class: Capybara::Cuprite::Driver

Inherits:
Driver::Base
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/capybara/cuprite/driver.rb

Constant Summary collapse

DEFAULT_MAXIMIZE_SCREEN_SIZE =
[1366, 768].freeze
EXTENSION =
File.expand_path("javascripts/index.js", __dir__)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Driver


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/capybara/cuprite/driver.rb', line 17

def initialize(app, options = {})
  @app     = app
  @options = options.dup
  @started = false

  @options[:extensions] ||= []
  @options[:extensions] << EXTENSION

  @screen_size = @options.delete(:screen_size)
  @screen_size ||= DEFAULT_MAXIMIZE_SCREEN_SIZE

  @options[:save_path] = Capybara.save_path.to_s if Capybara.save_path
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app


15
16
17
# File 'lib/capybara/cuprite/driver.rb', line 15

def app
  @app
end

#optionsObject (readonly)

Returns the value of attribute options


15
16
17
# File 'lib/capybara/cuprite/driver.rb', line 15

def options
  @options
end

#screen_sizeObject (readonly)

Returns the value of attribute screen_size


15
16
17
# File 'lib/capybara/cuprite/driver.rb', line 15

def screen_size
  @screen_size
end

Instance Method Details

#accept_modal(type, options = {}) ⇒ Object


353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/capybara/cuprite/driver.rb', line 353

def accept_modal(type, options = {})
  case type
  when :alert, :confirm
    browser.accept_confirm
  when :prompt
    browser.accept_prompt(options[:with])
  end

  yield if block_given?

  browser.find_modal(options)
end

#add_header(name, value, permanent: true) ⇒ Object


256
257
258
# File 'lib/capybara/cuprite/driver.rb', line 256

def add_header(name, value, permanent: true)
  browser.headers.add({ name => value }, permanent: permanent)
end

#add_headers(headers) ⇒ Object


252
253
254
# File 'lib/capybara/cuprite/driver.rb', line 252

def add_headers(headers)
  browser.headers.add(headers)
end

#basic_authorize(user, password) ⇒ Object Also known as: authorize


289
290
291
# File 'lib/capybara/cuprite/driver.rb', line 289

def basic_authorize(user, password)
  browser.network.authorize(user: user, password: password)
end

#browserObject


35
36
37
# File 'lib/capybara/cuprite/driver.rb', line 35

def browser
  @browser ||= Browser.new(@options)
end

#clear_cookiesObject


281
282
283
# File 'lib/capybara/cuprite/driver.rb', line 281

def clear_cookies
  browser.cookies.clear
end

#clear_memory_cacheObject


285
286
287
# File 'lib/capybara/cuprite/driver.rb', line 285

def clear_memory_cache
  browser.network.clear(:cache)
end

#clear_network_trafficObject


232
233
234
# File 'lib/capybara/cuprite/driver.rb', line 232

def clear_network_traffic
  browser.network.clear(:traffic)
end

#click(x, y) ⇒ Object


89
90
91
# File 'lib/capybara/cuprite/driver.rb', line 89

def click(x, y)
  browser.mouse.click(x: x, y: y)
end

#close_window(handle) ⇒ Object


127
128
129
# File 'lib/capybara/cuprite/driver.rb', line 127

def close_window(handle)
  browser.close_window(handle)
end

#cookiesObject


264
265
266
# File 'lib/capybara/cuprite/driver.rb', line 264

def cookies
  browser.cookies.all
end

#current_urlObject


44
45
46
47
48
49
50
# File 'lib/capybara/cuprite/driver.rb', line 44

def current_url
  if Capybara::VERSION.to_f < 3.0
    frame_url
  else
    browser.current_url
  end
end

#current_window_handleObject


119
120
121
# File 'lib/capybara/cuprite/driver.rb', line 119

def current_window_handle
  browser.window_handle
end

#debugObject


294
295
296
297
298
299
300
301
302
# File 'lib/capybara/cuprite/driver.rb', line 294

def debug
  if @options[:inspector]
    Process.spawn(browser.process.path, "http://#{browser.process.host}:#{browser.process.port}")
    pause
  else
    raise Error, "To use the remote debugging, you have to launch " \
                 "the driver with `inspector: true` configuration option"
  end
end

#dismiss_modal(type, options = {}) ⇒ Object


366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/capybara/cuprite/driver.rb', line 366

def dismiss_modal(type, options = {})
  case type
  when :confirm
    browser.dismiss_confirm
  when :prompt
    browser.dismiss_prompt
  end

  yield if block_given?

  browser.find_modal(options)
end

#evaluate_async_script(script, *args) ⇒ Object


98
99
100
101
# File 'lib/capybara/cuprite/driver.rb', line 98

def evaluate_async_script(script, *args)
  result = browser.evaluate_async(script, session_wait_time, *native_args(args))
  unwrap_script_result(result)
end

#evaluate_script(script, *args) ⇒ Object


93
94
95
96
# File 'lib/capybara/cuprite/driver.rb', line 93

def evaluate_script(script, *args)
  result = browser.evaluate(script, *native_args(args))
  unwrap_script_result(result)
end

#execute_script(script, *args) ⇒ Object


103
104
105
106
# File 'lib/capybara/cuprite/driver.rb', line 103

def execute_script(script, *args)
  browser.execute(script, *native_args(args))
  nil
end

#find(method, selector) ⇒ Object


85
86
87
# File 'lib/capybara/cuprite/driver.rb', line 85

def find(method, selector)
  browser.find(method, selector).map { |native| Node.new(self, native) }
end

#find_css(selector) ⇒ Object


81
82
83
# File 'lib/capybara/cuprite/driver.rb', line 81

def find_css(selector)
  find(:css, selector)
end

#find_xpath(selector) ⇒ Object


77
78
79
# File 'lib/capybara/cuprite/driver.rb', line 77

def find_xpath(selector)
  find(:xpath, selector)
end

#frame_titleObject


73
74
75
# File 'lib/capybara/cuprite/driver.rb', line 73

def frame_title
  evaluate_script("document.title")
end

#frame_urlObject


52
53
54
# File 'lib/capybara/cuprite/driver.rb', line 52

def frame_url
  evaluate_script("window.location.href")
end

#fullscreen_window(handle) ⇒ Object


208
209
210
211
212
# File 'lib/capybara/cuprite/driver.rb', line 208

def fullscreen_window(handle)
  within_window(handle) do
    browser.resize(fullscreen: true)
  end
end

#go_backObject


341
342
343
# File 'lib/capybara/cuprite/driver.rb', line 341

def go_back
  browser.back
end

#go_forwardObject


345
346
347
# File 'lib/capybara/cuprite/driver.rb', line 345

def go_forward
  browser.forward
end

#headersObject


244
245
246
# File 'lib/capybara/cuprite/driver.rb', line 244

def headers
  browser.headers.get
end

#headers=(headers) ⇒ Object


248
249
250
# File 'lib/capybara/cuprite/driver.rb', line 248

def headers=(headers)
  browser.headers.set(headers)
end

#htmlObject Also known as: body


56
57
58
# File 'lib/capybara/cuprite/driver.rb', line 56

def html
  browser.body
end

#invalid_element_errorsObject


335
336
337
338
339
# File 'lib/capybara/cuprite/driver.rb', line 335

def invalid_element_errors
  [Capybara::Cuprite::ObsoleteNode,
   Capybara::Cuprite::MouseEventFailed,
   Ferrum::NoExecutionContextError]
end

#maximize_window(handle) ⇒ Object


198
199
200
# File 'lib/capybara/cuprite/driver.rb', line 198

def maximize_window(handle)
  resize_window_to(handle, *screen_size)
end

#needs_server?Boolean


31
32
33
# File 'lib/capybara/cuprite/driver.rb', line 31

def needs_server?
  true
end

#network_traffic(type = nil) ⇒ Object


218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/capybara/cuprite/driver.rb', line 218

def network_traffic(type = nil)
  traffic = browser.network.traffic

  case type.to_s
  when "all"
    traffic
  when "blocked"
    traffic.select(&:blocked?)
  else
    # when request isn't blocked
    traffic.reject(&:blocked?)
  end
end

#no_such_window_errorObject


143
144
145
# File 'lib/capybara/cuprite/driver.rb', line 143

def no_such_window_error
  Ferrum::NoSuchPageError
end

#open_new_windowObject


131
132
133
# File 'lib/capybara/cuprite/driver.rb', line 131

def open_new_window
  browser.create_page
end

#paper_size=(value) ⇒ Object


183
184
185
# File 'lib/capybara/cuprite/driver.rb', line 183

def paper_size=(value)
  @paper_size = value
end

#pauseObject


304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/capybara/cuprite/driver.rb', line 304

def pause
  # STDIN is not necessarily connected to a keyboard. It might even be closed.
  # So we need a method other than keypress to continue.

  # In jRuby - STDIN returns immediately from select
  # see https://github.com/jruby/jruby/issues/1783
  read, write = IO.pipe
  Thread.new { IO.copy_stream(STDIN, write); write.close }

  STDERR.puts "Cuprite execution paused. Press enter (or run 'kill -CONT #{Process.pid}') to continue."

  signal = false
  old_trap = trap("SIGCONT") { signal = true; STDERR.puts "\nSignal SIGCONT received" }
  keyboard = IO.select([read], nil, nil, 1) until keyboard || signal # wait for data on STDIN or signal SIGCONT received

  unless signal
    begin
      input = read.read_nonblock(80) # clear out the read buffer
      puts unless input&.end_with?("\n")
    rescue EOFError, IO::WaitReadable # Ignore problems reading from STDIN.
    end
  end
ensure
  trap("SIGCONT", old_trap) # Restore the previous signal handler, if there was one.
  STDERR.puts "Continuing"
end

#refreshObject


349
350
351
# File 'lib/capybara/cuprite/driver.rb', line 349

def refresh
  browser.refresh
end

276
277
278
279
# File 'lib/capybara/cuprite/driver.rb', line 276

def remove_cookie(name, **options)
  options[:domain] = default_domain if options.empty?
  browser.cookies.remove(**options.merge(name: name))
end

#render_base64(format = :png, options = {}) ⇒ Object


169
170
171
172
173
174
175
176
177
# File 'lib/capybara/cuprite/driver.rb', line 169

def render_base64(format = :png, options = {})
  if pdf?(nil, options)
    options[:paperWidth] = @paper_size[:width].to_f if @paper_size
    options[:paperHeight] = @paper_size[:height].to_f if @paper_size
    browser.pdf(encoding: :base64, **options)
  else
    browser.screenshot(format: format, encoding: :base64, **options)
  end
end

#reset!Object


147
148
149
150
151
152
153
154
# File 'lib/capybara/cuprite/driver.rb', line 147

def reset!
  @zoom_factor = nil
  @paper_size = nil
  browser.url_blacklist = @options[:url_blacklist]
  browser.url_whitelist = @options[:url_whitelist]
  browser.reset
  @started = false
end

#resize(width, height) ⇒ Object Also known as: resize_window


187
188
189
# File 'lib/capybara/cuprite/driver.rb', line 187

def resize(width, height)
  browser.resize(width: width, height: height)
end

#resize_window_to(handle, width, height) ⇒ Object


192
193
194
195
196
# File 'lib/capybara/cuprite/driver.rb', line 192

def resize_window_to(handle, width, height)
  within_window(handle) do
    resize(width, height)
  end
end

#response_headersObject


260
261
262
# File 'lib/capybara/cuprite/driver.rb', line 260

def response_headers
  browser.network.response&.headers
end

#save_screenshot(path, options = {}) ⇒ Object Also known as: render


156
157
158
159
160
161
162
163
164
165
166
# File 'lib/capybara/cuprite/driver.rb', line 156

def save_screenshot(path, options = {})
  options[:scale] = @zoom_factor if @zoom_factor

  if pdf?(path, options)
    options[:paperWidth] = @paper_size[:width].to_f if @paper_size
    options[:paperHeight] = @paper_size[:height].to_f if @paper_size
    browser.pdf(path: path, **options)
  else
    browser.screenshot(path: path, **options)
  end
end

#scroll_to(left, top) ⇒ Object


214
215
216
# File 'lib/capybara/cuprite/driver.rb', line 214

def scroll_to(left, top)
  browser.mouse.scroll_to(left, top)
end

268
269
270
271
272
273
274
# File 'lib/capybara/cuprite/driver.rb', line 268

def set_cookie(name, value, options = {})
  options = options.dup
  options[:name]   ||= name
  options[:value]  ||= value
  options[:domain] ||= default_domain
  browser.cookies.set(**options)
end

#set_proxy(ip, port, type = nil, user = nil, password = nil, bypass = nil) ⇒ Object


236
237
238
239
240
241
242
# File 'lib/capybara/cuprite/driver.rb', line 236

def set_proxy(ip, port, type = nil, user = nil, password = nil, bypass = nil)
  @options[:browser_options] ||= {}
  server = type ? "#{type}=#{ip}:#{port}" : "#{ip}:#{port}"
  @options[:browser_options].merge!("proxy-server" => server)
  @options[:browser_options].merge!("proxy-bypass-list" => bypass) if bypass
  browser.network.authorize(type: :proxy, user: user, password: password)
end

#sourceObject


61
62
63
# File 'lib/capybara/cuprite/driver.rb', line 61

def source
  browser.source.to_s
end

#switch_to_frame(locator) ⇒ Object


108
109
110
111
112
113
114
115
116
117
# File 'lib/capybara/cuprite/driver.rb', line 108

def switch_to_frame(locator)
  handle = case locator
  when Capybara::Node::Element
    locator.native.description["frameId"]
  when :parent, :top
    locator
  end

  browser.switch_to_frame(handle)
end

#switch_to_window(handle) ⇒ Object


135
136
137
# File 'lib/capybara/cuprite/driver.rb', line 135

def switch_to_window(handle)
  browser.switch_to_window(handle)
end

#titleObject


65
66
67
68
69
70
71
# File 'lib/capybara/cuprite/driver.rb', line 65

def title
  if Capybara::VERSION.to_f < 3.0
    frame_title
  else
    browser.title
  end
end

#visit(url) ⇒ Object


39
40
41
42
# File 'lib/capybara/cuprite/driver.rb', line 39

def visit(url)
  @started = true
  browser.visit(url)
end

#wait?Boolean


331
332
333
# File 'lib/capybara/cuprite/driver.rb', line 331

def wait?
  true
end

#window_handlesObject


123
124
125
# File 'lib/capybara/cuprite/driver.rb', line 123

def window_handles
  browser.window_handles
end

#window_size(handle) ⇒ Object


202
203
204
205
206
# File 'lib/capybara/cuprite/driver.rb', line 202

def window_size(handle)
  within_window(handle) do
    evaluate_script("[window.innerWidth, window.innerHeight]")
  end
end

#within_window(name, &block) ⇒ Object


139
140
141
# File 'lib/capybara/cuprite/driver.rb', line 139

def within_window(name, &block)
  browser.within_window(name, &block)
end

#zoom_factor=(value) ⇒ Object


179
180
181
# File 'lib/capybara/cuprite/driver.rb', line 179

def zoom_factor=(value)
  @zoom_factor = value.to_f
end