Class: Capybara::Poltergeist::Driver

Inherits:
Driver::Base
  • Object
show all
Defined in:
lib/capybara/poltergeist/driver.rb

Constant Summary collapse

DEFAULT_TIMEOUT =
30

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Driver.


11
12
13
14
15
16
17
18
19
# File 'lib/capybara/poltergeist/driver.rb', line 11

def initialize(app, options = {})
  @app       = app
  @options   = options
  @browser   = nil
  @inspector = nil
  @server    = nil
  @client    = nil
  @started   = false
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app


9
10
11
# File 'lib/capybara/poltergeist/driver.rb', line 9

def app
  @app
end

#optionsObject (readonly)

Returns the value of attribute options


9
10
11
# File 'lib/capybara/poltergeist/driver.rb', line 9

def options
  @options
end

Instance Method Details

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


392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/capybara/poltergeist/driver.rb', line 392

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

  yield if block_given?

  find_modal(options)
end

#add_header(name, value, options = {}) ⇒ Object


280
281
282
# File 'lib/capybara/poltergeist/driver.rb', line 280

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

#add_headers(headers) ⇒ Object


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

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

#basic_authorize(user, password) ⇒ Object

  • PhantomJS with set settings doesn't send `Authorize` on POST request

  • With manually set header PhantomJS makes next request with

`Authorization: Basic Og==` header when settings are empty and the response was `401 Unauthorized` (which means Base64.encode64(':')). Combining both methods to reach proper behavior.


327
328
329
330
331
# File 'lib/capybara/poltergeist/driver.rb', line 327

def basic_authorize(user, password)
  browser.set_http_auth(user, password)
  credentials = ["#{user}:#{password}"].pack('m*').strip
  add_header('Authorization', "Basic #{credentials}")
end

#browserObject


25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/capybara/poltergeist/driver.rb', line 25

def browser
  @browser ||= begin
    browser = Browser.new(server, client, logger)
    browser.js_errors  = options[:js_errors] if options.key?(:js_errors)
    browser.extensions = options.fetch(:extensions, [])
    browser.debug      = true if options[:debug]
    browser.url_blacklist = options[:url_blacklist] if options.key?(:url_blacklist)
    browser.url_whitelist = options[:url_whitelist] if options.key?(:url_whitelist)
    browser.page_settings = options[:page_settings] if options.key?(:page_settings)
    browser
  end
end

#clear_cookiesObject


310
311
312
# File 'lib/capybara/poltergeist/driver.rb', line 310

def clear_cookies
  browser.clear_cookies
end

#clear_memory_cacheObject


318
319
320
# File 'lib/capybara/poltergeist/driver.rb', line 318

def clear_memory_cache
  browser.clear_memory_cache
end

#clear_network_trafficObject


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

def clear_network_traffic
  browser.clear_network_traffic
end

#click(x, y) ⇒ Object


152
153
154
# File 'lib/capybara/poltergeist/driver.rb', line 152

def click(x, y)
  browser.click_coordinates(x, y)
end

#clientObject


46
47
48
49
50
51
52
53
# File 'lib/capybara/poltergeist/driver.rb', line 46

def client
  @client ||= Client.start(server,
    :path              => options[:phantomjs],
    :window_size       => options[:window_size],
    :phantomjs_options => phantomjs_options,
    :phantomjs_logger  => phantomjs_logger
  )
end

#client_pidObject


67
68
69
# File 'lib/capybara/poltergeist/driver.rb', line 67

def client_pid
  client.pid
end

#close_window(handle) ⇒ Object


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

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

#cookiesObject


288
289
290
# File 'lib/capybara/poltergeist/driver.rb', line 288

def cookies
  browser.cookies
end

#cookies_enabled=(flag) ⇒ Object


314
315
316
# File 'lib/capybara/poltergeist/driver.rb', line 314

def cookies_enabled=(flag)
  browser.cookies_enabled = flag
end

#current_urlObject


103
104
105
106
107
108
109
# File 'lib/capybara/poltergeist/driver.rb', line 103

def current_url
  if Capybara::VERSION.to_f < 3.0
    frame_url
  else
    browser.current_url.gsub(' ', '%20') # PhantomJS < 2.1 doesn't escape spaces
  end
end

#current_window_handleObject


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

def current_window_handle
  browser.window_handle
end

#debugObject


333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/capybara/poltergeist/driver.rb', line 333

def debug
  if @options[:inspector]
    # Fall back to default scheme
    scheme = URI.parse(browser.current_url).scheme rescue nil
    scheme = 'http' if scheme != 'https'
    inspector.open(scheme)
    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


405
406
407
408
409
410
411
412
413
414
415
# File 'lib/capybara/poltergeist/driver.rb', line 405

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

  yield if block_given?
  find_modal(options)
end

#evaluate_async_script(script, *args) ⇒ Object


161
162
163
164
# File 'lib/capybara/poltergeist/driver.rb', line 161

def evaluate_async_script(script, *args)
  result = browser.evaluate_async(script, session_wait_time, *args.map { |arg| arg.is_a?(Capybara::Poltergeist::Node) ?  arg.native : arg})
  unwrap_script_result(result)
end

#evaluate_script(script, *args) ⇒ Object


156
157
158
159
# File 'lib/capybara/poltergeist/driver.rb', line 156

def evaluate_script(script, *args)
  result = browser.evaluate(script, *args.map { |arg| arg.is_a?(Capybara::Poltergeist::Node) ?  arg.native : arg})
  unwrap_script_result(result)
end

#execute_script(script, *args) ⇒ Object


166
167
168
169
# File 'lib/capybara/poltergeist/driver.rb', line 166

def execute_script(script, *args)
  browser.execute(script, *args.map { |arg| arg.is_a?(Capybara::Poltergeist::Node) ?  arg.native : arg})
  nil
end

#find(method, selector) ⇒ Object


140
141
142
# File 'lib/capybara/poltergeist/driver.rb', line 140

def find(method, selector)
  browser.find(method, selector).map { |page_id, id| Capybara::Poltergeist::Node.new(self, page_id, id) }
end

#find_css(selector) ⇒ Object


148
149
150
# File 'lib/capybara/poltergeist/driver.rb', line 148

def find_css(selector)
  find :css, selector
end

#find_xpath(selector) ⇒ Object


144
145
146
# File 'lib/capybara/poltergeist/driver.rb', line 144

def find_xpath(selector)
  find :xpath, selector
end

#frame_titleObject


136
137
138
# File 'lib/capybara/poltergeist/driver.rb', line 136

def frame_title
  browser.frame_title
end

#frame_urlObject


111
112
113
# File 'lib/capybara/poltergeist/driver.rb', line 111

def frame_url
  browser.frame_url.gsub(' ', '%20') # PhantomJS < 2.1 doesn't escape spaces
end

#go_backObject


380
381
382
# File 'lib/capybara/poltergeist/driver.rb', line 380

def go_back
  browser.go_back
end

#go_forwardObject


384
385
386
# File 'lib/capybara/poltergeist/driver.rb', line 384

def go_forward
  browser.go_forward
end

#headersObject


268
269
270
# File 'lib/capybara/poltergeist/driver.rb', line 268

def headers
  browser.get_headers
end

#headers=(headers) ⇒ Object


272
273
274
# File 'lib/capybara/poltergeist/driver.rb', line 272

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

#htmlObject Also known as: body


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

def html
  browser.body
end

#inspectorObject


38
39
40
# File 'lib/capybara/poltergeist/driver.rb', line 38

def inspector
  @inspector ||= options[:inspector] && Inspector.new(options[:inspector])
end

#invalid_element_errorsObject


376
377
378
# File 'lib/capybara/poltergeist/driver.rb', line 376

def invalid_element_errors
  [Capybara::Poltergeist::ObsoleteNode, Capybara::Poltergeist::MouseEventFailed]
end

#loggerObject

logger should be an object that responds to puts, or nil


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

def logger
  options[:logger] || (options[:debug] && STDERR)
end

#maximize_window(handle) ⇒ Object


242
243
244
# File 'lib/capybara/poltergeist/driver.rb', line 242

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

#needs_server?Boolean

Returns:

  • (Boolean)

21
22
23
# File 'lib/capybara/poltergeist/driver.rb', line 21

def needs_server?
  true
end

#network_traffic(type = nil) ⇒ Object


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

def network_traffic(type = nil)
  browser.network_traffic(type)
end

#no_such_window_errorObject


203
204
205
# File 'lib/capybara/poltergeist/driver.rb', line 203

def no_such_window_error
  NoSuchWindowError
end

#open_new_windowObject


191
192
193
# File 'lib/capybara/poltergeist/driver.rb', line 191

def open_new_window
  browser.open_new_window
end

#paper_size=(size = {}) ⇒ Object


223
224
225
# File 'lib/capybara/poltergeist/driver.rb', line 223

def paper_size=(size = {})
  browser.set_paper_size(size)
end

#pauseObject


346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/capybara/poltergeist/driver.rb', line 346

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 "Poltergeist 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

  begin
    input = read.read_nonblock(80) # clear out the read buffer
    puts unless input && input =~ /\n\z/
  rescue EOFError, IO::WaitReadable # Ignore problems reading from STDIN.
  end unless signal

ensure
  trap('SIGCONT', old_trap) # Restore the previous signal handler, if there was one.
  STDERR.puts 'Continuing'
end

#phantomjs_loggerObject

logger should be an object that behaves like IO or nil


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

def phantomjs_logger
  options.fetch(:phantomjs_logger, nil)
end

#phantomjs_optionsObject


55
56
57
58
59
60
61
62
63
64
65
# File 'lib/capybara/poltergeist/driver.rb', line 55

def phantomjs_options
  list = options[:phantomjs_options] || []

  # PhantomJS defaults to only using SSLv3, which since POODLE (Oct 2014)
  # many sites have dropped from their supported protocols (eg PayPal,
  # Braintree).
  list += ["--ignore-ssl-errors=yes"] unless list.grep(/ignore-ssl-errors/).any?
  list += ["--ssl-protocol=TLSv1"] unless list.grep(/ssl-protocol/).any?
  list += ["--remote-debugger-port=#{inspector.port}", "--remote-debugger-autorun=yes"] if inspector
  list
end

#quitObject


83
84
85
86
# File 'lib/capybara/poltergeist/driver.rb', line 83

def quit
  server.stop
  client.stop
end

#refreshObject


388
389
390
# File 'lib/capybara/poltergeist/driver.rb', line 388

def refresh
  browser.refresh
end

306
307
308
# File 'lib/capybara/poltergeist/driver.rb', line 306

def remove_cookie(name)
  browser.remove_cookie(name)
end

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


219
220
221
# File 'lib/capybara/poltergeist/driver.rb', line 219

def render_base64(format = :png, options = {})
  browser.render_base64(format, options)
end

#reset!Object


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

def reset!
  browser.reset
  browser.url_blacklist = options[:url_blacklist] if options.key?(:url_blacklist)
  browser.url_whitelist = options[:url_whitelist] if options.key?(:url_whitelist)
  @started = false
end

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


231
232
233
# File 'lib/capybara/poltergeist/driver.rb', line 231

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

#resize_window_to(handle, width, height) ⇒ Object


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

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

#response_headersObject


284
285
286
# File 'lib/capybara/poltergeist/driver.rb', line 284

def response_headers
  browser.response_headers
end

#restartObject


79
80
81
# File 'lib/capybara/poltergeist/driver.rb', line 79

def restart
  browser.restart
end

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


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

def save_screenshot(path, options = {})
  browser.render(path, options)
end

#scroll_to(left, top) ⇒ Object


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

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

#serverObject


42
43
44
# File 'lib/capybara/poltergeist/driver.rb', line 42

def server
  @server ||= Server.new(options[:port], options.fetch(:timeout) { DEFAULT_TIMEOUT }, options[:host])
end

292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/capybara/poltergeist/driver.rb', line 292

def set_cookie(name, value, options = {})
  options[:name]  ||= name
  options[:value] ||= value
  options[:domain] ||= begin
    if @started
      URI.parse(browser.current_url).host
    else
      URI.parse(default_cookie_host).host || "127.0.0.1"
    end
  end

  browser.set_cookie(options)
end

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


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

def set_proxy(ip, port, type = "http", user = nil, password = nil)
  browser.set_proxy(ip, port, type, user, password)
end

#sourceObject


124
125
126
# File 'lib/capybara/poltergeist/driver.rb', line 124

def source
  browser.source.to_s
end

#status_codeObject


115
116
117
# File 'lib/capybara/poltergeist/driver.rb', line 115

def status_code
  browser.status_code
end

#switch_to_frame(locator) ⇒ Object


175
176
177
# File 'lib/capybara/poltergeist/driver.rb', line 175

def switch_to_frame(locator)
  browser.switch_to_frame(locator)
end

#switch_to_window(handle) ⇒ Object


195
196
197
# File 'lib/capybara/poltergeist/driver.rb', line 195

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

#timeoutObject


71
72
73
# File 'lib/capybara/poltergeist/driver.rb', line 71

def timeout
  server.timeout
end

#timeout=(sec) ⇒ Object


75
76
77
# File 'lib/capybara/poltergeist/driver.rb', line 75

def timeout=(sec)
  server.timeout = sec
end

#titleObject


128
129
130
131
132
133
134
# File 'lib/capybara/poltergeist/driver.rb', line 128

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

#visit(url) ⇒ Object


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

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

#wait?Boolean

Returns:

  • (Boolean)

372
373
374
# File 'lib/capybara/poltergeist/driver.rb', line 372

def wait?
  true
end

#window_handlesObject


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

def window_handles
  browser.window_handles
end

#window_size(handle) ⇒ Object


246
247
248
249
250
# File 'lib/capybara/poltergeist/driver.rb', line 246

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

#within_frame(name, &block) ⇒ Object


171
172
173
# File 'lib/capybara/poltergeist/driver.rb', line 171

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

#within_window(name, &block) ⇒ Object


199
200
201
# File 'lib/capybara/poltergeist/driver.rb', line 199

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

#zoom_factor=(zoom_factor) ⇒ Object


227
228
229
# File 'lib/capybara/poltergeist/driver.rb', line 227

def zoom_factor=(zoom_factor)
  browser.set_zoom_factor(zoom_factor)
end