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
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Browser.



17
18
19
20
21
# File 'lib/capybara/poltergeist/browser.rb', line 17

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

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



15
16
17
# File 'lib/capybara/poltergeist/browser.rb', line 15

def client
  @client
end

#loggerObject (readonly)

Returns the value of attribute logger.



15
16
17
# File 'lib/capybara/poltergeist/browser.rb', line 15

def logger
  @logger
end

#serverObject (readonly)

Returns the value of attribute server.



15
16
17
# File 'lib/capybara/poltergeist/browser.rb', line 15

def server
  @server
end

Instance Method Details

#add_header(header, permanent) ⇒ Object



257
258
259
# File 'lib/capybara/poltergeist/browser.rb', line 257

def add_header(header, permanent)
  command 'add_header', header, permanent
end

#add_headers(headers) ⇒ Object



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

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

#all_text(page_id, id) ⇒ Object



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

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

#attribute(page_id, id, name) ⇒ Object



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

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

#attributes(page_id, id) ⇒ Object



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

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

#bodyObject



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

def body
  command 'body'
end

#clear_cookiesObject



281
282
283
# File 'lib/capybara/poltergeist/browser.rb', line 281

def clear_cookies
  command 'clear_cookies'
end

#clear_network_trafficObject



237
238
239
# File 'lib/capybara/poltergeist/browser.rb', line 237

def clear_network_traffic
  command('clear_network_traffic')
end

#click(page_id, id) ⇒ Object



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

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

#click_coordinates(x, y) ⇒ Object



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

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

#close_window(handle) ⇒ Object



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

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

#command(name, *args) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/capybara/poltergeist/browser.rb', line 312

def command(name, *args)
  message = JSON.dump({ 'name' => name, 'args' => args })
  log message

  response = server.send(message)
  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



265
266
267
# File 'lib/capybara/poltergeist/browser.rb', line 265

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

#cookies_enabled=(flag) ⇒ Object



285
286
287
# File 'lib/capybara/poltergeist/browser.rb', line 285

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

#current_urlObject



34
35
36
# File 'lib/capybara/poltergeist/browser.rb', line 34

def current_url
  command 'current_url'
end

#debug=(val) ⇒ Object



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

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

#delete_text(page_id, id) ⇒ Object



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

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

#disabled?(page_id, id) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#double_click(page_id, id) ⇒ Object



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

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

#drag(page_id, id, other_id) ⇒ Object



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

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

#equals(page_id, id, other_id) ⇒ Object



241
242
243
# File 'lib/capybara/poltergeist/browser.rb', line 241

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

#evaluate(script) ⇒ Object



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

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

#execute(script) ⇒ Object



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

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

#extensions=(names) ⇒ Object



297
298
299
300
301
# File 'lib/capybara/poltergeist/browser.rb', line 297

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

#find(method, selector) ⇒ Object



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

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

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



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

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

#get_headersObject



245
246
247
# File 'lib/capybara/poltergeist/browser.rb', line 245

def get_headers
  command 'get_headers'
end

#go_backObject



332
333
334
# File 'lib/capybara/poltergeist/browser.rb', line 332

def go_back
  command 'go_back'
end

#go_forwardObject



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

def go_forward
  command 'go_forward'
end

#hover(page_id, id) ⇒ Object



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

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

#js_errors=(val) ⇒ Object



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

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

#network_trafficObject



228
229
230
231
232
233
234
235
# File 'lib/capybara/poltergeist/browser.rb', line 228

def network_traffic
  command('network_traffic').values.map do |event|
    NetworkTraffic::Request.new(
      event['request'],
      event['responseParts'].map { |response| NetworkTraffic::Response.new(response) }
    )
  end
end

#open_new_windowObject



147
148
149
# File 'lib/capybara/poltergeist/browser.rb', line 147

def open_new_window
  command 'open_new_window'
end

#parents(page_id, id) ⇒ Object



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

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


277
278
279
# File 'lib/capybara/poltergeist/browser.rb', line 277

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

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



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

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

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



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

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

#resetObject



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

def reset
  command 'reset'
end

#resize(width, height) ⇒ Object



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

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

#response_headersObject



261
262
263
# File 'lib/capybara/poltergeist/browser.rb', line 261

def response_headers
  command 'response_headers'
end

#restartObject



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

def restart
  server.restart
  client.restart

  self.debug = @debug if @debug
end

#right_click(page_id, id) ⇒ Object



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

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

#scroll_to(left, top) ⇒ Object



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

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

#select(page_id, id, value) ⇒ Object



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

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

#select_file(page_id, id, value) ⇒ Object



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

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

#send_keys(page_id, id, keys) ⇒ Object



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

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

#set(page_id, id, value) ⇒ Object



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

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


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

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

  command 'set_cookie', cookie
end

#set_headers(headers) ⇒ Object



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

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

#set_http_auth(user, password) ⇒ Object



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

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

#set_paper_size(size) ⇒ Object



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

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

#set_zoom_factor(zoom_factor) ⇒ Object



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

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

#sourceObject



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

def source
  command 'source'
end

#status_codeObject



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

def status_code
  command 'status_code'
end

#switch_to_window(handle) ⇒ Object



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

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

#tag_name(page_id, id) ⇒ Object



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

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

#titleObject



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

def title
  command 'title'
end

#trigger(page_id, id, event) ⇒ Object



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

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

#url_blacklist=(blacklist) ⇒ Object



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

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

#value(page_id, id) ⇒ Object



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

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

#visible?(page_id, id) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#visible_text(page_id, id) ⇒ Object



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

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

#visit(url) ⇒ Object



30
31
32
# File 'lib/capybara/poltergeist/browser.rb', line 30

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

#window_handleObject



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

def window_handle
  command 'window_handle'
end

#window_handlesObject



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

def window_handles
  command 'window_handles'
end

#within_frame(handle, &block) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/capybara/poltergeist/browser.rb', line 123

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

  yield
ensure
  command 'pop_frame'
end

#within_window(name, &block) ⇒ Object



155
156
157
158
159
160
161
162
163
164
# File 'lib/capybara/poltergeist/browser.rb', line 155

def within_window(name, &block)
  original = window_handle
  handle = command 'window_handle', name
  handle = name if handle.nil? && window_handles.include?(name)
  raise NoSuchWindowError unless handle
  switch_to_window(handle)
  yield
ensure
  switch_to_window(original)
end