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


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

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

#add_headers(headers) ⇒ Object


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

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.


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

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

#clear_network_trafficObject


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

def clear_network_traffic
  browser.clear_network_traffic
end

#click(x, y) ⇒ Object


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

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

#client_pidObject


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

def client_pid
  client.pid
end

#cookiesObject


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

def cookies
  browser.cookies
end

#cookies_enabled=(flag) ⇒ Object


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

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

#current_urlObject


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

def current_url
  browser.current_url
end

#debugObject


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

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


129
130
131
# File 'lib/capybara/poltergeist/driver.rb', line 129

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

#execute_script(script) ⇒ Object


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

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

#find(method, selector) ⇒ Object


113
114
115
# File 'lib/capybara/poltergeist/driver.rb', line 113

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


121
122
123
# File 'lib/capybara/poltergeist/driver.rb', line 121

def find_css(selector)
  find :css, selector
end

#find_xpath(selector) ⇒ Object


117
118
119
# File 'lib/capybara/poltergeist/driver.rb', line 117

def find_xpath(selector)
  find :xpath, selector
end

#go_backObject


266
267
268
# File 'lib/capybara/poltergeist/driver.rb', line 266

def go_back
  browser.go_back
end

#go_forwardObject


270
271
272
# File 'lib/capybara/poltergeist/driver.rb', line 270

def go_forward
  browser.go_forward
end

#headersObject


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

def headers
  browser.get_headers
end

#headers=(headers) ⇒ Object


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

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

#htmlObject Also known as: body


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

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


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

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

#loggerObject

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


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

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


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

def network_traffic
  browser.network_traffic
end

#paper_size=(size = {}) ⇒ Object


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

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

#pauseObject


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

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


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

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

#phantomjs_optionsObject


50
51
52
53
54
# File 'lib/capybara/poltergeist/driver.rb', line 50

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

#quitObject


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

def quit
  server.stop
  client.stop
end

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

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

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


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

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

#reset!Object


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

def reset!
  browser.reset
  @started = false
end

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


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

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

#response_headersObject


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

def response_headers
  browser.response_headers
end

#restartObject


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

def restart
  browser.restart
end

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


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

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

#scroll_to(left, top) ⇒ Object


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

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

210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/capybara/poltergeist/driver.rb', line 210

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


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

def source
  browser.source.to_s
end

#status_codeObject


96
97
98
# File 'lib/capybara/poltergeist/driver.rb', line 96

def status_code
  browser.status_code
end

#timeoutObject


60
61
62
# File 'lib/capybara/poltergeist/driver.rb', line 60

def timeout
  server.timeout
end

#timeout=(sec) ⇒ Object


64
65
66
# File 'lib/capybara/poltergeist/driver.rb', line 64

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

#titleObject


109
110
111
# File 'lib/capybara/poltergeist/driver.rb', line 109

def title
  browser.title
end

#visit(url) ⇒ Object


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

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

#wait?Boolean

Returns:

  • (Boolean)

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

def wait?
  true
end

#window_handlesObject


146
147
148
# File 'lib/capybara/poltergeist/driver.rb', line 146

def window_handles
  browser.window_handles
end

#within_frame(name, &block) ⇒ Object


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

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

#within_window(name, &block) ⇒ Object


142
143
144
# File 'lib/capybara/poltergeist/driver.rb', line 142

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