Class: Capybara::Webkit::Browser

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

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Browser

Returns a new instance of Browser.



6
7
8
9
# File 'lib/capybara/webkit/browser.rb', line 6

def initialize(connection)
  @connection = connection
  apply_defaults
end

Instance Method Details

#accept_alert(options) ⇒ Object



200
201
202
# File 'lib/capybara/webkit/browser.rb', line 200

def accept_alert(options)
  command("AcceptAlert", options[:text])
end

#accept_confirm(options) ⇒ Object



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

def accept_confirm(options)
  command("SetConfirmAction", "Yes", options[:text])
end

#accept_js_confirmsObject



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

def accept_js_confirms
  command("SetConfirmAction", "Yes")
end

#accept_js_promptsObject



180
181
182
# File 'lib/capybara/webkit/browser.rb', line 180

def accept_js_prompts
  command("SetPromptAction", "Yes")
end

#accept_prompt(options) ⇒ Object



172
173
174
175
176
177
178
# File 'lib/capybara/webkit/browser.rb', line 172

def accept_prompt(options)
  if options[:with]
    command("SetPromptAction", "Yes", options[:text], options[:with])
  else
    command("SetPromptAction", "Yes", options[:text])
  end
end

#alert_messagesObject



64
65
66
# File 'lib/capybara/webkit/browser.rb', line 64

def alert_messages
  JSON.parse(command("JavascriptAlertMessages"))
end

#allow_unknown_urlsObject



313
314
315
# File 'lib/capybara/webkit/browser.rb', line 313

def allow_unknown_urls
  allow_url("*")
end

#allow_url(url) ⇒ Object



301
302
303
# File 'lib/capybara/webkit/browser.rb', line 301

def allow_url(url)
  command("AllowUrl", url)
end

#authenticate(username, password) ⇒ Object



11
12
13
# File 'lib/capybara/webkit/browser.rb', line 11

def authenticate(username, password)
  command("Authenticate", username, password)
end

#block_unknown_urlsObject



309
310
311
# File 'lib/capybara/webkit/browser.rb', line 309

def block_unknown_urls
  command("SetUnknownUrlMode", "block")
end

#block_url(url) ⇒ Object



305
306
307
# File 'lib/capybara/webkit/browser.rb', line 305

def block_url(url)
  command("BlockUrl", url)
end

#bodyObject



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

def body
  command("Body")
end

#clear_cookiesObject



268
269
270
# File 'lib/capybara/webkit/browser.rb', line 268

def clear_cookies
  command "ClearCookies"
end

#clear_prompt_textObject



196
197
198
# File 'lib/capybara/webkit/browser.rb', line 196

def clear_prompt_text
  command("ClearPromptText")
end

#clear_proxyObject



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

def clear_proxy
  command("SetProxy")
end

#command(name, *args) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/capybara/webkit/browser.rb', line 214

def command(name, *args)
  @connection.puts name
  @connection.puts args.size
  args.each do |arg|
    @connection.puts arg.to_s.bytesize
    @connection.print arg.to_s
  end
  check
  response = read_response
  check_javascript_errors(name)
  response
rescue SystemCallError => exception
  @connection.restart
  raise(Capybara::Webkit::CrashError, <<-MESSAGE.strip)
The webkit_server process crashed!

  #{exception.message}

This is a bug in capybara-webkit. For help with this crash, please visit:

https://github.com/thoughtbot/capybara-webkit/wiki/Reporting-Crashes
  MESSAGE
end

#confirm_messagesObject



68
69
70
# File 'lib/capybara/webkit/browser.rb', line 68

def confirm_messages
  JSON.parse(command("JavascriptConfirmMessages"))
end

#console_messagesObject



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

def console_messages
  JSON.parse(command("ConsoleMessages")).map do |message|
    message.inject({}) { |m,(k,v)| m.merge(k.to_sym => v) }
  end
end

#current_urlObject



80
81
82
# File 'lib/capybara/webkit/browser.rb', line 80

def current_url
  command("CurrentUrl")
end

#enable_loggingObject



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

def enable_logging
  command "EnableLogging"
end

#error_messagesObject



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

def error_messages
  console_messages.select do |message|
    message[:message] =~ /Error:/
  end
end

#evaluate_async_script(script, *args) ⇒ Object



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

def evaluate_async_script(script, *args)
  json = command("EvaluateAsync", script, args.to_json)
  JSON.parse("[#{json}]").first
end

#evaluate_script(script, *args) ⇒ Object



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

def evaluate_script(script, *args)
  json = command("Evaluate", script, args.to_json)
  JSON.parse("[#{json}]").first
end

#execute_script(script, *args) ⇒ Object



248
249
250
# File 'lib/capybara/webkit/browser.rb', line 248

def execute_script(script, *args)
  command("Execute", script, args.to_json)
end

#find_css(query) ⇒ Object



35
36
37
# File 'lib/capybara/webkit/browser.rb', line 35

def find_css(query)
  command("FindCss", query).split(",")
end

#find_modal(id) ⇒ Object



204
205
206
# File 'lib/capybara/webkit/browser.rb', line 204

def find_modal(id)
  command("FindModal", id)
end

#find_xpath(query) ⇒ Object



31
32
33
# File 'lib/capybara/webkit/browser.rb', line 31

def find_xpath(query)
  command("FindXpath", query).split(",")
end

#frame_focus(selector = nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/capybara/webkit/browser.rb', line 84

def frame_focus(selector=nil)
  if selector.respond_to?(:base)
    selector.base.invoke("focus_frame")
  elsif selector.is_a? Integer
    command("FrameFocus", "", selector.to_s)
  elsif selector
    command("FrameFocus", selector)
  else
    command("FrameFocus")
  end
end

#get_cookiesObject



272
273
274
# File 'lib/capybara/webkit/browser.rb', line 272

def get_cookies
  command("GetCookies").lines.map{ |line| line.strip }.select{ |line| !line.empty? }
end

#get_window_handleObject



146
147
148
# File 'lib/capybara/webkit/browser.rb', line 146

def get_window_handle
  command("GetWindowHandle")
end

#get_window_handlesObject



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

def get_window_handles
  JSON.parse(command("GetWindowHandles"))
end

#go_backObject



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

def go_back
  command("GoBack")
end

#go_forwardObject



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

def go_forward
  command("GoForward")
end

#header(key, value) ⇒ Object



23
24
25
# File 'lib/capybara/webkit/browser.rb', line 23

def header(key, value)
  command("Header", key, value)
end

#ignore_ssl_errorsObject



96
97
98
# File 'lib/capybara/webkit/browser.rb', line 96

def ignore_ssl_errors
  command("IgnoreSslErrors")
end

#prompt_messagesObject



72
73
74
# File 'lib/capybara/webkit/browser.rb', line 72

def prompt_messages
  JSON.parse(command("JavascriptPromptMessages"))
end

#raise_javascript_errors?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/capybara/webkit/browser.rb', line 108

def raise_javascript_errors?
  @raise_javascript_errors
end

#refreshObject



297
298
299
# File 'lib/capybara/webkit/browser.rb', line 297

def refresh
  command("Refresh")
end

#reject_confirm(options) ⇒ Object



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

def reject_confirm(options)
  command("SetConfirmAction", "No", options[:text])
end

#reject_js_confirmsObject



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

def reject_js_confirms
  command("SetConfirmAction", "No")
end

#reject_js_promptsObject



188
189
190
# File 'lib/capybara/webkit/browser.rb', line 188

def reject_js_prompts
  command("SetPromptAction", "No")
end

#reject_prompt(options) ⇒ Object



184
185
186
# File 'lib/capybara/webkit/browser.rb', line 184

def reject_prompt(options)
  command("SetPromptAction", "No", options[:text])
end

#render(path, width, height) ⇒ Object



252
253
254
# File 'lib/capybara/webkit/browser.rb', line 252

def render(path, width, height)
  command "Render", path, width, height
end

#reset!Object



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

def reset!
  apply_defaults
  command("Reset")
end

#response_headersObject



76
77
78
# File 'lib/capybara/webkit/browser.rb', line 76

def response_headers
  JSON.parse(command("Headers"))
end


264
265
266
# File 'lib/capybara/webkit/browser.rb', line 264

def set_cookie(cookie)
  command "SetCookie", cookie
end

#set_prompt_text_to(string) ⇒ Object



192
193
194
# File 'lib/capybara/webkit/browser.rb', line 192

def set_prompt_text_to(string)
  command("SetPromptText", string)
end

#set_proxy(options = {}) ⇒ Object



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

def set_proxy(options = {})
  options = default_proxy_options.merge(options)
  command("SetProxy", options[:host], options[:port], options[:user], options[:pass])
end

#set_raise_javascript_errors(is_enabled) ⇒ Object



104
105
106
# File 'lib/capybara/webkit/browser.rb', line 104

def set_raise_javascript_errors(is_enabled)
  @raise_javascript_errors = is_enabled
end

#set_skip_image_loading(skip_image_loading) ⇒ Object



100
101
102
# File 'lib/capybara/webkit/browser.rb', line 100

def set_skip_image_loading(skip_image_loading)
  command("SetSkipImageLoading", skip_image_loading)
end

#status_codeObject



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

def status_code
  command("Status").to_i
end

#timeoutObject



260
261
262
# File 'lib/capybara/webkit/browser.rb', line 260

def timeout
  command("GetTimeout").to_i
end

#timeout=(timeout_in_seconds) ⇒ Object



256
257
258
# File 'lib/capybara/webkit/browser.rb', line 256

def timeout=(timeout_in_seconds)
  command "SetTimeout", timeout_in_seconds
end

#titleObject



27
28
29
# File 'lib/capybara/webkit/browser.rb', line 27

def title
  command("Title")
end

#url_blacklist=(black_list) ⇒ Object



208
209
210
211
212
# File 'lib/capybara/webkit/browser.rb', line 208

def url_blacklist=(black_list)
  warn '[DEPRECATION] Capybara::Webkit::Browser#url_blacklist= ' \
    'is deprecated. Please use page.driver.block_url instead.'
  command("SetUrlBlacklist", *Array(black_list))
end

#versionObject



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

def version
  command("Version")
end

#visit(url) ⇒ Object



19
20
21
# File 'lib/capybara/webkit/browser.rb', line 19

def visit(url)
  command "Visit", url
end

#window_close(selector) ⇒ Object



120
121
122
# File 'lib/capybara/webkit/browser.rb', line 120

def window_close(selector)
  command("WindowClose", selector)
end

#window_focus(selector) ⇒ Object



112
113
114
# File 'lib/capybara/webkit/browser.rb', line 112

def window_focus(selector)
  command("WindowFocus", selector)
end

#window_handleObject



150
151
152
153
154
# File 'lib/capybara/webkit/browser.rb', line 150

def window_handle
  warn '[DEPRECATION] Capybara::Webkit::Browser#window_handle ' \
    'is deprecated. Please use Capybara::Session#current_window instead.'
  get_window_handle
end

#window_handlesObject



140
141
142
143
144
# File 'lib/capybara/webkit/browser.rb', line 140

def window_handles
  warn '[DEPRECATION] Capybara::Webkit::Browser#window_handles ' \
    'is deprecated. Please use Capybara::Session#windows instead.'
  get_window_handles
end

#window_maximize(handle) ⇒ Object



132
133
134
# File 'lib/capybara/webkit/browser.rb', line 132

def window_maximize(handle)
  command("WindowMaximize", handle)
end

#window_openObject



116
117
118
# File 'lib/capybara/webkit/browser.rb', line 116

def window_open
  command("WindowOpen")
end

#window_resize(handle, width, height) ⇒ Object



124
125
126
# File 'lib/capybara/webkit/browser.rb', line 124

def window_resize(handle, width, height)
  command("WindowResize", handle, width.to_i, height.to_i)
end

#window_size(handle) ⇒ Object



128
129
130
# File 'lib/capybara/webkit/browser.rb', line 128

def window_size(handle)
  JSON.parse(command("WindowSize", handle))
end