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.



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

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.



7
8
9
# File 'lib/capybara/poltergeist/driver.rb', line 7

def app
  @app
end

#browserObject (readonly)

Returns the value of attribute browser.



7
8
9
# File 'lib/capybara/poltergeist/driver.rb', line 7

def browser
  @browser
end

#clientObject (readonly)

Returns the value of attribute client.



7
8
9
# File 'lib/capybara/poltergeist/driver.rb', line 7

def client
  @client
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/capybara/poltergeist/driver.rb', line 7

def options
  @options
end

#serverObject (readonly)

Returns the value of attribute server.



7
8
9
# File 'lib/capybara/poltergeist/driver.rb', line 7

def server
  @server
end

Instance Method Details

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



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

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

#add_headers(headers) ⇒ Object



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

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.



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

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

#clear_cookiesObject



258
259
260
# File 'lib/capybara/poltergeist/driver.rb', line 258

def clear_cookies
  browser.clear_cookies
end

#clear_network_trafficObject



211
212
213
# File 'lib/capybara/poltergeist/driver.rb', line 211

def clear_network_traffic
  browser.clear_network_traffic
end

#click(x, y) ⇒ Object



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

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

#client_pidObject



62
63
64
# File 'lib/capybara/poltergeist/driver.rb', line 62

def client_pid
  client.pid
end

#close_window(handle) ⇒ Object



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

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

#cookiesObject



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

def cookies
  browser.cookies
end

#cookies_enabled=(flag) ⇒ Object



262
263
264
# File 'lib/capybara/poltergeist/driver.rb', line 262

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

#current_urlObject



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

def current_url
  browser.current_url
end

#current_window_handleObject



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

def current_window_handle
  browser.window_handle
end

#debugObject



277
278
279
280
281
282
283
284
285
# File 'lib/capybara/poltergeist/driver.rb', line 277

def debug
  if @options[:inspector]
    inspector.open
    pause
  else
    raise Error, "To use the remote debugging, you have to launch the driver " \
                 "with `:inspector => true` configuration option"
  end
end

#evaluate_script(script) ⇒ Object



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

def evaluate_script(script)
  browser.evaluate(script)
end

#execute_script(script) ⇒ Object



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

def execute_script(script)
  browser.execute(script)
  nil
end

#find(method, selector) ⇒ Object



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

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



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

def find_css(selector)
  find :css, selector
end

#find_xpath(selector) ⇒ Object



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

def find_xpath(selector)
  find :xpath, selector
end

#go_backObject



300
301
302
# File 'lib/capybara/poltergeist/driver.rb', line 300

def go_back
  browser.go_back
end

#go_forwardObject



304
305
306
# File 'lib/capybara/poltergeist/driver.rb', line 304

def go_forward
  browser.go_forward
end

#headersObject



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

def headers
  browser.get_headers
end

#headers=(headers) ⇒ Object



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

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

#htmlObject Also known as: body



106
107
108
# File 'lib/capybara/poltergeist/driver.rb', line 106

def html
  browser.body
end

#inspectorObject



33
34
35
# File 'lib/capybara/poltergeist/driver.rb', line 33

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

#invalid_element_errorsObject



296
297
298
# File 'lib/capybara/poltergeist/driver.rb', line 296

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

#loggerObject

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



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

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

#needs_server?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/capybara/poltergeist/driver.rb', line 19

def needs_server?
  true
end

#network_trafficObject



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

def network_traffic
  browser.network_traffic
end

#no_such_window_errorObject



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

def no_such_window_error
  NoSuchWindowError
end

#open_new_windowObject



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

def open_new_window
  browser.open_new_window
end

#paper_size=(size = {}) ⇒ Object



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

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

#pauseObject



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

def pause
  STDERR.puts "Poltergeist execution paused. Press enter to continue."
  STDIN.gets
end

#phantomjs_loggerObject

logger should be an object that behaves like IO or nil



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

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

#phantomjs_optionsObject



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/capybara/poltergeist/driver.rb', line 50

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 += ["--ssl-protocol=any"] unless list.grep(/ssl-protocol/).any?

  list += ["--remote-debugger-port=#{inspector.port}", "--remote-debugger-autorun=yes"] if inspector
  list
end

#quitObject



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

def quit
  server.stop
  client.stop
end


254
255
256
# File 'lib/capybara/poltergeist/driver.rb', line 254

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

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



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

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

#reset!Object



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

def reset!
  browser.reset
  @started = false
end

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



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

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

#response_headersObject



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

def response_headers
  browser.response_headers
end

#restartObject



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

def restart
  browser.restart
end

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



181
182
183
# File 'lib/capybara/poltergeist/driver.rb', line 181

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

#scroll_to(left, top) ⇒ Object



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

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


240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/capybara/poltergeist/driver.rb', line 240

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

  browser.set_cookie(options)
end

#sourceObject



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

def source
  browser.source.to_s
end

#status_codeObject



102
103
104
# File 'lib/capybara/poltergeist/driver.rb', line 102

def status_code
  browser.status_code
end

#switch_to_window(handle) ⇒ Object



164
165
166
# File 'lib/capybara/poltergeist/driver.rb', line 164

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

#timeoutObject



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

def timeout
  server.timeout
end

#timeout=(sec) ⇒ Object



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

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

#titleObject



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

def title
  browser.title
end

#visit(url) ⇒ Object



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

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

#wait?Boolean

Returns:

  • (Boolean)


292
293
294
# File 'lib/capybara/poltergeist/driver.rb', line 292

def wait?
  true
end

#window_handlesObject



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

def window_handles
  browser.window_handles
end

#within_frame(name, &block) ⇒ Object



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

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

#within_window(name, &block) ⇒ Object



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

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

#zoom_factor=(zoom_factor) ⇒ Object



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

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