Class: Capybara::Poltergeist::Browser

Inherits:
Object
  • Object
show all
Defined in:
lib/capybara/poltergeist/browser.rb

Constant Summary collapse

ERROR_MAPPINGS =
{
  'Poltergeist.JavascriptError' => JavascriptError,
  'Poltergeist.FrameNotFound'   => FrameNotFound,
  'Poltergeist.InvalidSelector' => InvalidSelector,
  'Poltergeist.StatusFailError' => StatusFailError,
  'Poltergeist.NoSuchWindowError' => NoSuchWindowError,
  'Poltergeist.ScriptTimeoutError' => ScriptTimeoutError,
  'Poltergeist.UnsupportedFeature' => UnsupportedFeature,
  'Poltergeist.KeyError' => KeyError,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, client, logger = nil) ⇒ Browser

Returns a new instance of Browser.



23
24
25
26
27
# File 'lib/capybara/poltergeist/browser.rb', line 23

def initialize(server, client, logger = nil)
  @server = server
  @client = client
  @logger = logger
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

#serverObject (readonly)

Returns the value of attribute server.



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

def server
  @server
end

Instance Method Details

#accept_confirmObject



417
418
419
# File 'lib/capybara/poltergeist/browser.rb', line 417

def accept_confirm
  command 'set_confirm_process', true
end

#accept_prompt(response) ⇒ Object

press “OK” with text (response) or default value



428
429
430
# File 'lib/capybara/poltergeist/browser.rb', line 428

def accept_prompt(response)
  command 'set_prompt_response', response || false
end

#add_header(header, options = {}) ⇒ Object



316
317
318
# File 'lib/capybara/poltergeist/browser.rb', line 316

def add_header(header, options={})
  command 'add_header', header, options
end

#add_headers(headers) ⇒ Object



312
313
314
# File 'lib/capybara/poltergeist/browser.rb', line 312

def add_headers(headers)
  command 'add_headers', headers
end

#all_text(page_id, id) ⇒ Object



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

def all_text(page_id, id)
  command 'all_text', page_id, id
end

#attribute(page_id, id, name) ⇒ Object



103
104
105
# File 'lib/capybara/poltergeist/browser.rb', line 103

def attribute(page_id, id, name)
  command 'attribute', page_id, id, name.to_s
end

#attributes(page_id, id) ⇒ Object



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

def attributes(page_id, id)
  command 'attributes', page_id, id
end

#bodyObject



54
55
56
# File 'lib/capybara/poltergeist/browser.rb', line 54

def body
  command 'body'
end

#clear_cookiesObject



340
341
342
# File 'lib/capybara/poltergeist/browser.rb', line 340

def clear_cookies
  command 'clear_cookies'
end

#clear_memory_cacheObject



381
382
383
# File 'lib/capybara/poltergeist/browser.rb', line 381

def clear_memory_cache
  command 'clear_memory_cache'
end

#clear_network_trafficObject



289
290
291
# File 'lib/capybara/poltergeist/browser.rb', line 289

def clear_network_traffic
  command('clear_network_traffic')
end

#click(page_id, id, keys = [], offset = {}) ⇒ Object



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

def click(page_id, id, keys=[], offset={})
  command 'click', page_id, id, keys, offset
end

#click_coordinates(x, y) ⇒ Object



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

def click_coordinates(x, y)
  command 'click_coordinates', x, y
end

#close_window(handle) ⇒ Object



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

def close_window(handle)
  command 'close_window', handle
end

#command(name, *args) ⇒ Object



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/capybara/poltergeist/browser.rb', line 385

def command(name, *args)
  cmd = Command.new(name, *args)
  log cmd.message

  response = server.send(cmd)
  log response

  json = JSON.load(response)

  if json['error']
    klass = ERROR_MAPPINGS[json['error']['name']] || BrowserError
    raise klass.new(json['error'])
  else
    json['response']
  end
rescue DeadClient
  restart
  raise
end

#cookiesObject



324
325
326
# File 'lib/capybara/poltergeist/browser.rb', line 324

def cookies
  Hash[command('cookies').map { |cookie| [cookie['name'], Cookie.new(cookie)] }]
end

#cookies_enabled=(flag) ⇒ Object



344
345
346
# File 'lib/capybara/poltergeist/browser.rb', line 344

def cookies_enabled=(flag)
  command 'cookies_enabled', !!flag
end

#current_urlObject



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

def current_url
  command 'current_url'
end

#debug=(val) ⇒ Object



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

def debug=(val)
  @debug = val
  command 'set_debug', !!val
end

#delete_text(page_id, id) ⇒ Object



91
92
93
# File 'lib/capybara/poltergeist/browser.rb', line 91

def delete_text(page_id, id)
  command 'delete_text', page_id, id
end

#disabled?(page_id, id) ⇒ Boolean

Returns:

  • (Boolean)


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

def disabled?(page_id, id)
  command 'disabled', page_id, id
end

#dismiss_confirmObject



421
422
423
# File 'lib/capybara/poltergeist/browser.rb', line 421

def dismiss_confirm
  command 'set_confirm_process', false
end

#dismiss_promptObject

press “Cancel”



435
436
437
# File 'lib/capybara/poltergeist/browser.rb', line 435

def dismiss_prompt
  command 'set_prompt_response', nil
end

#double_click(page_id, id, keys = [], offset = {}) ⇒ Object



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

def double_click(page_id, id, keys=[], offset={})
  command 'double_click', page_id, id, keys, offset
end

#drag(page_id, id, other_id) ⇒ Object



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

def drag(page_id, id, other_id)
  command 'drag', page_id, id, other_id
end

#drag_by(page_id, id, x, y) ⇒ Object



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

def drag_by(page_id, id, x, y)
  command 'drag_by', page_id, id, x, y
end

#equals(page_id, id, other_id) ⇒ Object



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

def equals(page_id, id, other_id)
  command('equals', page_id, id, other_id)
end

#evaluate(script, *args) ⇒ Object



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

def evaluate(script, *args)
  command 'evaluate', script, *args
end

#evaluate_async(script, wait_time, *args) ⇒ Object



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

def evaluate_async(script, wait_time, *args)
  command 'evaluate_async', script, wait_time, *args
end

#execute(script, *args) ⇒ Object



143
144
145
# File 'lib/capybara/poltergeist/browser.rb', line 143

def execute(script, *args)
  command 'execute', script, *args
end

#extensions=(names) ⇒ Object



361
362
363
364
365
366
# File 'lib/capybara/poltergeist/browser.rb', line 361

def extensions=(names)
  @extensions = names
  Array(names).each do |name|
    command 'add_extension', name
  end
end

#find(method, selector) ⇒ Object



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

def find(method, selector)
  result = command('find', method, selector)
  result['ids'].map { |id| [result['page_id'], id] }
end

#find_window_handle(locator) ⇒ Object

Raises:



190
191
192
193
194
195
196
# File 'lib/capybara/poltergeist/browser.rb', line 190

def find_window_handle(locator)
  return locator if window_handles.include? locator

  handle = command 'window_handle', locator
  raise NoSuchWindowError unless handle
  return handle
end

#find_within(page_id, id, method, selector) ⇒ Object



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

def find_within(page_id, id, method, selector)
  command 'find_within', page_id, id, method, selector
end

#frame_titleObject



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

def frame_title
  command 'frame_title'
end

#frame_urlObject



46
47
48
# File 'lib/capybara/poltergeist/browser.rb', line 46

def frame_url
  command 'frame_url'
end

#get_headersObject



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

def get_headers
  command 'get_headers'
end

#go_backObject



405
406
407
# File 'lib/capybara/poltergeist/browser.rb', line 405

def go_back
  command 'go_back'
end

#go_forwardObject



409
410
411
# File 'lib/capybara/poltergeist/browser.rb', line 409

def go_forward
  command 'go_forward'
end

#hover(page_id, id) ⇒ Object



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

def hover(page_id, id)
  command 'hover', page_id, id
end

#js_errors=(val) ⇒ Object



352
353
354
355
# File 'lib/capybara/poltergeist/browser.rb', line 352

def js_errors=(val)
  @js_errors = val
  command 'set_js_errors', !!val
end


439
440
441
# File 'lib/capybara/poltergeist/browser.rb', line 439

def modal_message
  command 'modal_message'
end

#network_traffic(type = nil) ⇒ Object



279
280
281
282
283
284
285
286
287
# File 'lib/capybara/poltergeist/browser.rb', line 279

def network_traffic(type = nil)
  command('network_traffic', type).map do |event|
    NetworkTraffic::Request.new(
      event['request'],
      event['responseParts'].map { |response| NetworkTraffic::Response.new(response) },
      event['error'] ? NetworkTraffic::Error.new(event['error']) : nil
    )
  end
end

#open_new_windowObject



182
183
184
# File 'lib/capybara/poltergeist/browser.rb', line 182

def open_new_window
  command 'open_new_window'
end

#page_settings=(settings) ⇒ Object



357
358
359
# File 'lib/capybara/poltergeist/browser.rb', line 357

def page_settings=(settings)
  command 'set_page_settings', settings
end

#parents(page_id, id) ⇒ Object



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

def parents(page_id, id)
  command 'parents', page_id, id
end

#path(page_id, id) ⇒ Object



275
276
277
# File 'lib/capybara/poltergeist/browser.rb', line 275

def path(page_id, id)
  command 'path', page_id, id
end

#property(page_id, id, name) ⇒ Object



95
96
97
# File 'lib/capybara/poltergeist/browser.rb', line 95

def property(page_id, id, name)
  command 'property', page_id, id, name.to_s
end

#refreshObject



413
414
415
# File 'lib/capybara/poltergeist/browser.rb', line 413

def refresh
  command 'refresh'
end


336
337
338
# File 'lib/capybara/poltergeist/browser.rb', line 336

def remove_cookie(name)
  command 'remove_cookie', name
end

#render(path, options = {}) ⇒ Object



247
248
249
250
251
# File 'lib/capybara/poltergeist/browser.rb', line 247

def render(path, options = {})
  check_render_options!(options)
  options[:full] = !!options[:full]
  command 'render', path.to_s, options
end

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



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

def render_base64(format, options = {})
  check_render_options!(options)
  options[:full] = !!options[:full]
  command 'render_base64', format.to_s, options
end

#resetObject



239
240
241
# File 'lib/capybara/poltergeist/browser.rb', line 239

def reset
  command 'reset'
end

#resize(width, height) ⇒ Object



267
268
269
# File 'lib/capybara/poltergeist/browser.rb', line 267

def resize(width, height)
  command 'resize', width, height
end

#response_headersObject



320
321
322
# File 'lib/capybara/poltergeist/browser.rb', line 320

def response_headers
  command 'response_headers'
end

#restartObject



29
30
31
32
33
34
35
36
# File 'lib/capybara/poltergeist/browser.rb', line 29

def restart
  server.restart
  client.restart

  self.debug = @debug if defined?(@debug)
  self.js_errors = @js_errors if defined?(@js_errors)
  self.extensions = @extensions if @extensions
end

#right_click(page_id, id, keys = [], offset = {}) ⇒ Object



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

def right_click(page_id, id, keys=[], offset={})
  command 'right_click', page_id, id, keys, offset
end

#scroll_to(left, top) ⇒ Object



243
244
245
# File 'lib/capybara/poltergeist/browser.rb', line 243

def scroll_to(left, top)
  command 'scroll_to', left, top
end

#select(page_id, id, value) ⇒ Object



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

def select(page_id, id, value)
  command 'select', page_id, id, value
end

#select_file(page_id, id, value) ⇒ Object



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

def select_file(page_id, id, value)
  command 'select_file', page_id, id, value
end

#send_keys(page_id, id, keys) ⇒ Object



271
272
273
# File 'lib/capybara/poltergeist/browser.rb', line 271

def send_keys(page_id, id, keys)
  command 'send_keys', page_id, id, normalize_keys(keys)
end

#set(page_id, id, value) ⇒ Object



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

def set(page_id, id, value)
  command 'set', page_id, id, value
end


328
329
330
331
332
333
334
# File 'lib/capybara/poltergeist/browser.rb', line 328

def set_cookie(cookie)
  if cookie[:expires]
    cookie[:expires] = cookie[:expires].to_i * 1000
  end

  command 'set_cookie', cookie
end

#set_headers(headers) ⇒ Object



308
309
310
# File 'lib/capybara/poltergeist/browser.rb', line 308

def set_headers(headers)
  command 'set_headers', headers
end

#set_http_auth(user, password) ⇒ Object



348
349
350
# File 'lib/capybara/poltergeist/browser.rb', line 348

def set_http_auth(user, password)
  command 'set_http_auth', user, password
end

#set_paper_size(size) ⇒ Object



263
264
265
# File 'lib/capybara/poltergeist/browser.rb', line 263

def set_paper_size(size)
  command 'set_paper_size', size
end

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



293
294
295
296
297
298
# File 'lib/capybara/poltergeist/browser.rb', line 293

def set_proxy(ip, port, type, user, password)
  args = [ip, port, type]
  args << user if user
  args << password if password
  command('set_proxy', *args)
end

#set_zoom_factor(zoom_factor) ⇒ Object



259
260
261
# File 'lib/capybara/poltergeist/browser.rb', line 259

def set_zoom_factor(zoom_factor)
  command 'set_zoom_factor', zoom_factor
end

#sourceObject



58
59
60
# File 'lib/capybara/poltergeist/browser.rb', line 58

def source
  command 'source'
end

#status_codeObject



50
51
52
# File 'lib/capybara/poltergeist/browser.rb', line 50

def status_code
  command 'status_code'
end

#switch_to_frame(handle) ⇒ Object



159
160
161
162
163
164
165
166
167
168
# File 'lib/capybara/poltergeist/browser.rb', line 159

def switch_to_frame(handle)
  case handle
  when Capybara::Node::Base
    command 'push_frame', [handle.native.page_id, handle.native.id]
  when :parent
    command 'pop_frame'
  when :top
    command 'pop_frame', true
  end
end

#switch_to_window(handle) ⇒ Object



178
179
180
# File 'lib/capybara/poltergeist/browser.rb', line 178

def switch_to_window(handle)
  command 'switch_to_window', handle
end

#tag_name(page_id, id) ⇒ Object



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

def tag_name(page_id, id)
  command('tag_name', page_id, id).downcase
end

#titleObject



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

def title
  command 'title'
end

#trigger(page_id, id, event) ⇒ Object



235
236
237
# File 'lib/capybara/poltergeist/browser.rb', line 235

def trigger(page_id, id, event)
  command 'trigger', page_id, id, event.to_s
end

#url_blacklist=(blacklist) ⇒ Object



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

def url_blacklist=(blacklist)
  command 'set_url_blacklist', *blacklist
end

#url_whitelist=(whitelist) ⇒ Object



368
369
370
# File 'lib/capybara/poltergeist/browser.rb', line 368

def url_whitelist=(whitelist)
  command 'set_url_whitelist', *whitelist
end

#value(page_id, id) ⇒ Object



107
108
109
# File 'lib/capybara/poltergeist/browser.rb', line 107

def value(page_id, id)
  command 'value', page_id, id
end

#visible?(page_id, id) ⇒ Boolean

Returns:

  • (Boolean)


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

def visible?(page_id, id)
  command 'visible', page_id, id
end

#visible_text(page_id, id) ⇒ Object



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

def visible_text(page_id, id)
  command 'visible_text', page_id, id
end

#visit(url) ⇒ Object



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

def visit(url)
  command 'visit', url
end

#window_handleObject



170
171
172
# File 'lib/capybara/poltergeist/browser.rb', line 170

def window_handle
  command 'window_handle'
end

#window_handlesObject



174
175
176
# File 'lib/capybara/poltergeist/browser.rb', line 174

def window_handles
  command 'window_handles'
end

#within_frame(handle, &block) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/capybara/poltergeist/browser.rb', line 147

def within_frame(handle, &block)
  if handle.is_a?(Capybara::Node::Base)
    command 'push_frame', [handle.native.page_id, handle.native.id]
  else
    command 'push_frame', handle
  end

  yield
ensure
  command 'pop_frame'
end

#within_window(locator, &block) ⇒ Object



198
199
200
201
202
203
204
205
# File 'lib/capybara/poltergeist/browser.rb', line 198

def within_window(locator, &block)
  original = window_handle
  handle = find_window_handle(locator)
  switch_to_window(handle)
  yield
ensure
  switch_to_window(original)
end