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
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Browser.


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

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

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client


13
14
15
# File 'lib/capybara/poltergeist/browser.rb', line 13

def client
  @client
end

#loggerObject (readonly)

Returns the value of attribute logger


13
14
15
# File 'lib/capybara/poltergeist/browser.rb', line 13

def logger
  @logger
end

#serverObject (readonly)

Returns the value of attribute server


13
14
15
# File 'lib/capybara/poltergeist/browser.rb', line 13

def server
  @server
end

Instance Method Details

#add_header(header, permanent) ⇒ Object


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

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

#add_headers(headers) ⇒ Object


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

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

#all_text(page_id, id) ⇒ Object


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

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

#attribute(page_id, id, name) ⇒ Object


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

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

#bodyObject


40
41
42
# File 'lib/capybara/poltergeist/browser.rb', line 40

def body
  command 'body'
end

#clear_network_trafficObject


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

def clear_network_traffic
  command('clear_network_traffic')
end

#click(page_id, id) ⇒ Object


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

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

#click_coordinates(x, y) ⇒ Object


101
102
103
# File 'lib/capybara/poltergeist/browser.rb', line 101

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

#command(name, *args) ⇒ Object


266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/capybara/poltergeist/browser.rb', line 266

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

  json = JSON.load(server.send(JSON.dump(message)))
  log json.inspect

  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


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

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

#cookies_enabled=(flag) ⇒ Object


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

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

#current_urlObject


32
33
34
# File 'lib/capybara/poltergeist/browser.rb', line 32

def current_url
  command 'current_url'
end

#debug=(val) ⇒ Object


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

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

#delete_text(page_id, id) ⇒ Object


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

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

#disabled?(page_id, id) ⇒ Boolean

Returns:

  • (Boolean)

97
98
99
# File 'lib/capybara/poltergeist/browser.rb', line 97

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

#double_click(page_id, id) ⇒ Object


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

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

#drag(page_id, id, other_id) ⇒ Object


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

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

#equals(page_id, id, other_id) ⇒ Object


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

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

#evaluate(script) ⇒ Object


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

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

#execute(script) ⇒ Object


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

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

#extensions=(names) ⇒ Object


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

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

#find(method, selector) ⇒ Object


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

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


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

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

#get_headersObject


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

def get_headers
  command 'get_headers'
end

#go_backObject


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

def go_back
  command 'go_back'
end

#go_forwardObject


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

def go_forward
  command 'go_forward'
end

#hover(page_id, id) ⇒ Object


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

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

#js_errors=(val) ⇒ Object


251
252
253
# File 'lib/capybara/poltergeist/browser.rb', line 251

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

#network_trafficObject


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

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

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

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

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


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

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

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


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

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

#resetObject


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

def reset
  command 'reset'
end

#resize(width, height) ⇒ Object


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

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

#response_headersObject


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

def response_headers
  command 'response_headers'
end

#restartObject


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

def restart
  server.restart
  client.restart

  self.debug = @debug if @debug
end

#scroll_to(left, top) ⇒ Object


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

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

#select(page_id, id, value) ⇒ Object


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

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

#select_file(page_id, id, value) ⇒ Object


85
86
87
# File 'lib/capybara/poltergeist/browser.rb', line 85

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

#send_keys(page_id, id, keys) ⇒ Object


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

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

#set(page_id, id, value) ⇒ Object


81
82
83
# File 'lib/capybara/poltergeist/browser.rb', line 81

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

231
232
233
234
235
236
237
# File 'lib/capybara/poltergeist/browser.rb', line 231

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

  command 'set_cookie', cookie
end

#set_headers(headers) ⇒ Object


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

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

#set_http_auth(user, password) ⇒ Object


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

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

#set_paper_size(size) ⇒ Object


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

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

#sourceObject


44
45
46
# File 'lib/capybara/poltergeist/browser.rb', line 44

def source
  command 'source'
end

#status_codeObject


36
37
38
# File 'lib/capybara/poltergeist/browser.rb', line 36

def status_code
  command 'status_code'
end

#tag_name(page_id, id) ⇒ Object


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

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

#titleObject


48
49
50
# File 'lib/capybara/poltergeist/browser.rb', line 48

def title
  command 'title'
end

#trigger(page_id, id, event) ⇒ Object


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

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

#value(page_id, id) ⇒ Object


77
78
79
# File 'lib/capybara/poltergeist/browser.rb', line 77

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

#visible?(page_id, id) ⇒ Boolean

Returns:

  • (Boolean)

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

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

#visible_text(page_id, id) ⇒ Object


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

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

#visit(url) ⇒ Object


28
29
30
# File 'lib/capybara/poltergeist/browser.rb', line 28

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

#window_handlesObject


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

def window_handles
  command 'pages'
end

#within_frame(handle, &block) ⇒ Object


113
114
115
116
117
118
119
120
121
122
123
# File 'lib/capybara/poltergeist/browser.rb', line 113

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


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

def within_window(name, &block)
  command 'push_window', name
  yield
ensure
  command 'pop_window'
end