Class: Capybara::Poltergeist::Driver
- Inherits:
-
Driver::Base
- Object
- Driver::Base
- Capybara::Poltergeist::Driver
- Defined in:
- lib/capybara/poltergeist/driver.rb
Constant Summary collapse
- DEFAULT_TIMEOUT =
30
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #accept_modal(type, options = {}) ⇒ Object
- #add_header(name, value, options = {}) ⇒ Object
- #add_headers(headers) ⇒ Object
-
#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(’:‘)).
-
- #browser ⇒ Object
- #clear_cookies ⇒ Object
- #clear_memory_cache ⇒ Object
- #clear_network_traffic ⇒ Object
- #click(x, y) ⇒ Object
- #client ⇒ Object
- #client_pid ⇒ Object
- #close_window(handle) ⇒ Object
- #cookies ⇒ Object
- #cookies_enabled=(flag) ⇒ Object
- #current_url ⇒ Object
- #current_window_handle ⇒ Object
- #debug ⇒ Object
- #dismiss_modal(type, options = {}) ⇒ Object
- #evaluate_async_script(script, *args) ⇒ Object
- #evaluate_script(script, *args) ⇒ Object
- #execute_script(script, *args) ⇒ Object
- #find(method, selector) ⇒ Object
- #find_css(selector) ⇒ Object
- #find_xpath(selector) ⇒ Object
- #frame_title ⇒ Object
- #frame_url ⇒ Object
- #go_back ⇒ Object
- #go_forward ⇒ Object
- #headers ⇒ Object
- #headers=(headers) ⇒ Object
- #html ⇒ Object (also: #body)
-
#initialize(app, options = {}) ⇒ Driver
constructor
A new instance of Driver.
- #inspector ⇒ Object
- #invalid_element_errors ⇒ Object
-
#logger ⇒ Object
logger should be an object that responds to puts, or nil.
- #maximize_window(handle) ⇒ Object
- #needs_server? ⇒ Boolean
- #network_traffic(type = nil) ⇒ Object
- #no_such_window_error ⇒ Object
- #open_new_window ⇒ Object
- #paper_size=(size = {}) ⇒ Object
- #pause ⇒ Object
-
#phantomjs_logger ⇒ Object
logger should be an object that behaves like IO or nil.
- #phantomjs_options ⇒ Object
- #quit ⇒ Object
- #refresh ⇒ Object
- #remove_cookie(name) ⇒ Object
- #render_base64(format = :png, options = {}) ⇒ Object
- #reset! ⇒ Object
- #resize(width, height) ⇒ Object (also: #resize_window)
- #resize_window_to(handle, width, height) ⇒ Object
- #response_headers ⇒ Object
- #restart ⇒ Object
- #save_screenshot(path, options = {}) ⇒ Object (also: #render)
- #scroll_to(left, top) ⇒ Object
- #server ⇒ Object
- #set_cookie(name, value, options = {}) ⇒ Object
- #set_proxy(ip, port, type = "http", user = nil, password = nil) ⇒ Object
- #source ⇒ Object
- #status_code ⇒ Object
- #switch_to_frame(locator) ⇒ Object
- #switch_to_window(handle) ⇒ Object
- #timeout ⇒ Object
- #timeout=(sec) ⇒ Object
- #title ⇒ Object
- #visit(url) ⇒ Object
- #wait? ⇒ Boolean
- #window_handles ⇒ Object
- #window_size(handle) ⇒ Object
- #within_frame(name, &block) ⇒ Object
- #within_window(name, &block) ⇒ Object
- #zoom_factor=(zoom_factor) ⇒ Object
Constructor Details
#initialize(app, options = {}) ⇒ Driver
Returns a new instance of Driver.
11 12 13 14 15 16 17 18 19 |
# File 'lib/capybara/poltergeist/driver.rb', line 11 def initialize(app, = {}) @app = app @options = @browser = nil @inspector = nil @server = nil @client = nil @started = false end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
9 10 11 |
# File 'lib/capybara/poltergeist/driver.rb', line 9 def app @app end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/capybara/poltergeist/driver.rb', line 9 def @options end |
Instance Method Details
#accept_modal(type, options = {}) ⇒ Object
392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/capybara/poltergeist/driver.rb', line 392 def accept_modal(type, = {}) case type when :confirm browser.accept_confirm when :prompt browser.accept_prompt [:with] end yield if block_given? find_modal() end |
#add_header(name, value, options = {}) ⇒ Object
280 281 282 |
# File 'lib/capybara/poltergeist/driver.rb', line 280 def add_header(name, value, = {}) browser.add_header({ name => value }, { permanent: true }.merge()) end |
#add_headers(headers) ⇒ Object
276 277 278 |
# File 'lib/capybara/poltergeist/driver.rb', line 276 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.
327 328 329 330 331 |
# File 'lib/capybara/poltergeist/driver.rb', line 327 def (user, password) browser.set_http_auth(user, password) credentials = ["#{user}:#{password}"].pack('m*').strip add_header('Authorization', "Basic #{credentials}") end |
#browser ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/capybara/poltergeist/driver.rb', line 25 def browser @browser ||= begin browser = Browser.new(server, client, logger) browser.js_errors = [:js_errors] if .key?(:js_errors) browser.extensions = .fetch(:extensions, []) browser.debug = true if [:debug] browser.url_blacklist = [:url_blacklist] if .key?(:url_blacklist) browser.url_whitelist = [:url_whitelist] if .key?(:url_whitelist) browser.page_settings = [:page_settings] if .key?(:page_settings) browser end end |
#clear_cookies ⇒ Object
310 311 312 |
# File 'lib/capybara/poltergeist/driver.rb', line 310 def browser. end |
#clear_memory_cache ⇒ Object
318 319 320 |
# File 'lib/capybara/poltergeist/driver.rb', line 318 def clear_memory_cache browser.clear_memory_cache end |
#clear_network_traffic ⇒ Object
260 261 262 |
# File 'lib/capybara/poltergeist/driver.rb', line 260 def clear_network_traffic browser.clear_network_traffic end |
#click(x, y) ⇒ Object
152 153 154 |
# File 'lib/capybara/poltergeist/driver.rb', line 152 def click(x, y) browser.click_coordinates(x, y) end |
#client ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/capybara/poltergeist/driver.rb', line 46 def client @client ||= Client.start(server, :path => [:phantomjs], :window_size => [:window_size], :phantomjs_options => , :phantomjs_logger => phantomjs_logger ) end |
#client_pid ⇒ Object
67 68 69 |
# File 'lib/capybara/poltergeist/driver.rb', line 67 def client_pid client.pid end |
#close_window(handle) ⇒ Object
187 188 189 |
# File 'lib/capybara/poltergeist/driver.rb', line 187 def close_window(handle) browser.close_window(handle) end |
#cookies ⇒ Object
288 289 290 |
# File 'lib/capybara/poltergeist/driver.rb', line 288 def browser. end |
#cookies_enabled=(flag) ⇒ Object
314 315 316 |
# File 'lib/capybara/poltergeist/driver.rb', line 314 def (flag) browser. = flag end |
#current_url ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/capybara/poltergeist/driver.rb', line 103 def current_url if Capybara::VERSION.to_f < 3.0 frame_url else browser.current_url.gsub(' ', '%20') # PhantomJS < 2.1 doesn't escape spaces end end |
#current_window_handle ⇒ Object
179 180 181 |
# File 'lib/capybara/poltergeist/driver.rb', line 179 def current_window_handle browser.window_handle end |
#debug ⇒ Object
333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/capybara/poltergeist/driver.rb', line 333 def debug if @options[:inspector] # Fall back to default scheme scheme = URI.parse(browser.current_url).scheme rescue nil scheme = 'http' if scheme != 'https' inspector.open(scheme) pause else raise Error, "To use the remote debugging, you have to launch the driver " \ "with `:inspector => true` configuration option" end end |
#dismiss_modal(type, options = {}) ⇒ Object
405 406 407 408 409 410 411 412 413 414 415 |
# File 'lib/capybara/poltergeist/driver.rb', line 405 def dismiss_modal(type, = {}) case type when :confirm browser.dismiss_confirm when :prompt browser.dismiss_prompt end yield if block_given? find_modal() end |
#evaluate_async_script(script, *args) ⇒ Object
161 162 163 164 |
# File 'lib/capybara/poltergeist/driver.rb', line 161 def evaluate_async_script(script, *args) result = browser.evaluate_async(script, session_wait_time, *args.map { |arg| arg.is_a?(Capybara::Poltergeist::Node) ? arg.native : arg}) unwrap_script_result(result) end |
#evaluate_script(script, *args) ⇒ Object
156 157 158 159 |
# File 'lib/capybara/poltergeist/driver.rb', line 156 def evaluate_script(script, *args) result = browser.evaluate(script, *args.map { |arg| arg.is_a?(Capybara::Poltergeist::Node) ? arg.native : arg}) unwrap_script_result(result) end |
#execute_script(script, *args) ⇒ Object
166 167 168 169 |
# File 'lib/capybara/poltergeist/driver.rb', line 166 def execute_script(script, *args) browser.execute(script, *args.map { |arg| arg.is_a?(Capybara::Poltergeist::Node) ? arg.native : arg}) nil end |
#find(method, selector) ⇒ Object
140 141 142 |
# File 'lib/capybara/poltergeist/driver.rb', line 140 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
148 149 150 |
# File 'lib/capybara/poltergeist/driver.rb', line 148 def find_css(selector) find :css, selector end |
#find_xpath(selector) ⇒ Object
144 145 146 |
# File 'lib/capybara/poltergeist/driver.rb', line 144 def find_xpath(selector) find :xpath, selector end |
#frame_title ⇒ Object
136 137 138 |
# File 'lib/capybara/poltergeist/driver.rb', line 136 def frame_title browser.frame_title end |
#frame_url ⇒ Object
111 112 113 |
# File 'lib/capybara/poltergeist/driver.rb', line 111 def frame_url browser.frame_url.gsub(' ', '%20') # PhantomJS < 2.1 doesn't escape spaces end |
#go_back ⇒ Object
380 381 382 |
# File 'lib/capybara/poltergeist/driver.rb', line 380 def go_back browser.go_back end |
#go_forward ⇒ Object
384 385 386 |
# File 'lib/capybara/poltergeist/driver.rb', line 384 def go_forward browser.go_forward end |
#headers ⇒ Object
268 269 270 |
# File 'lib/capybara/poltergeist/driver.rb', line 268 def headers browser.get_headers end |
#headers=(headers) ⇒ Object
272 273 274 |
# File 'lib/capybara/poltergeist/driver.rb', line 272 def headers=(headers) browser.set_headers(headers) end |
#html ⇒ Object Also known as: body
119 120 121 |
# File 'lib/capybara/poltergeist/driver.rb', line 119 def html browser.body end |
#inspector ⇒ Object
38 39 40 |
# File 'lib/capybara/poltergeist/driver.rb', line 38 def inspector @inspector ||= [:inspector] && Inspector.new([:inspector]) end |
#invalid_element_errors ⇒ Object
376 377 378 |
# File 'lib/capybara/poltergeist/driver.rb', line 376 def invalid_element_errors [Capybara::Poltergeist::ObsoleteNode, Capybara::Poltergeist::MouseEventFailed] end |
#logger ⇒ Object
logger should be an object that responds to puts, or nil
89 90 91 |
# File 'lib/capybara/poltergeist/driver.rb', line 89 def logger [:logger] || ([:debug] && STDERR) end |
#maximize_window(handle) ⇒ Object
242 243 244 |
# File 'lib/capybara/poltergeist/driver.rb', line 242 def maximize_window(handle) resize_window_to(handle, *screen_size) end |
#needs_server? ⇒ Boolean
21 22 23 |
# File 'lib/capybara/poltergeist/driver.rb', line 21 def needs_server? true end |
#network_traffic(type = nil) ⇒ Object
256 257 258 |
# File 'lib/capybara/poltergeist/driver.rb', line 256 def network_traffic(type = nil) browser.network_traffic(type) end |
#no_such_window_error ⇒ Object
203 204 205 |
# File 'lib/capybara/poltergeist/driver.rb', line 203 def no_such_window_error NoSuchWindowError end |
#open_new_window ⇒ Object
191 192 193 |
# File 'lib/capybara/poltergeist/driver.rb', line 191 def open_new_window browser.open_new_window end |
#paper_size=(size = {}) ⇒ Object
223 224 225 |
# File 'lib/capybara/poltergeist/driver.rb', line 223 def paper_size=(size = {}) browser.set_paper_size(size) end |
#pause ⇒ Object
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/capybara/poltergeist/driver.rb', line 346 def pause # STDIN is not necessarily connected to a keyboard. It might even be closed. # So we need a method other than keypress to continue. # In jRuby - STDIN returns immediately from select # see https://github.com/jruby/jruby/issues/1783 read, write = IO.pipe Thread.new { IO.copy_stream(STDIN, write); write.close } STDERR.puts "Poltergeist execution paused. Press enter (or run 'kill -CONT #{Process.pid}') to continue." signal = false old_trap = trap('SIGCONT') { signal = true; STDERR.puts "\nSignal SIGCONT received" } keyboard = IO.select([read], nil, nil, 1) until keyboard || signal # wait for data on STDIN or signal SIGCONT received begin input = read.read_nonblock(80) # clear out the read buffer puts unless input && input =~ /\n\z/ rescue EOFError, IO::WaitReadable # Ignore problems reading from STDIN. end unless signal ensure trap('SIGCONT', old_trap) # Restore the previous signal handler, if there was one. STDERR.puts 'Continuing' end |
#phantomjs_logger ⇒ Object
logger should be an object that behaves like IO or nil
94 95 96 |
# File 'lib/capybara/poltergeist/driver.rb', line 94 def phantomjs_logger .fetch(:phantomjs_logger, nil) end |
#phantomjs_options ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/capybara/poltergeist/driver.rb', line 55 def list = [:phantomjs_options] || [] # PhantomJS defaults to only using SSLv3, which since POODLE (Oct 2014) # many sites have dropped from their supported protocols (eg PayPal, # Braintree). list += ["--ignore-ssl-errors=yes"] unless list.grep(/ignore-ssl-errors/).any? list += ["--ssl-protocol=TLSv1"] unless list.grep(/ssl-protocol/).any? list += ["--remote-debugger-port=#{inspector.port}", "--remote-debugger-autorun=yes"] if inspector list end |
#quit ⇒ Object
83 84 85 86 |
# File 'lib/capybara/poltergeist/driver.rb', line 83 def quit server.stop client.stop end |
#refresh ⇒ Object
388 389 390 |
# File 'lib/capybara/poltergeist/driver.rb', line 388 def refresh browser.refresh end |
#remove_cookie(name) ⇒ Object
306 307 308 |
# File 'lib/capybara/poltergeist/driver.rb', line 306 def (name) browser.(name) end |
#render_base64(format = :png, options = {}) ⇒ Object
219 220 221 |
# File 'lib/capybara/poltergeist/driver.rb', line 219 def render_base64(format = :png, = {}) browser.render_base64(format, ) end |
#reset! ⇒ Object
207 208 209 210 211 212 |
# File 'lib/capybara/poltergeist/driver.rb', line 207 def reset! browser.reset browser.url_blacklist = [:url_blacklist] if .key?(:url_blacklist) browser.url_whitelist = [:url_whitelist] if .key?(:url_whitelist) @started = false end |
#resize(width, height) ⇒ Object Also known as: resize_window
231 232 233 |
# File 'lib/capybara/poltergeist/driver.rb', line 231 def resize(width, height) browser.resize(width, height) end |
#resize_window_to(handle, width, height) ⇒ Object
236 237 238 239 240 |
# File 'lib/capybara/poltergeist/driver.rb', line 236 def resize_window_to(handle, width, height) within_window(handle) do resize(width, height) end end |
#response_headers ⇒ Object
284 285 286 |
# File 'lib/capybara/poltergeist/driver.rb', line 284 def response_headers browser.response_headers end |
#restart ⇒ Object
79 80 81 |
# File 'lib/capybara/poltergeist/driver.rb', line 79 def restart browser.restart end |
#save_screenshot(path, options = {}) ⇒ Object Also known as: render
214 215 216 |
# File 'lib/capybara/poltergeist/driver.rb', line 214 def save_screenshot(path, = {}) browser.render(path, ) end |
#scroll_to(left, top) ⇒ Object
252 253 254 |
# File 'lib/capybara/poltergeist/driver.rb', line 252 def scroll_to(left, top) browser.scroll_to(left, top) end |
#server ⇒ Object
42 43 44 |
# File 'lib/capybara/poltergeist/driver.rb', line 42 def server @server ||= Server.new([:port], .fetch(:timeout) { DEFAULT_TIMEOUT }, [:host]) end |
#set_cookie(name, value, options = {}) ⇒ Object
292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/capybara/poltergeist/driver.rb', line 292 def (name, value, = {}) [:name] ||= name [:value] ||= value [:domain] ||= begin if @started URI.parse(browser.current_url).host else URI.parse().host || "127.0.0.1" end end browser.() end |
#set_proxy(ip, port, type = "http", user = nil, password = nil) ⇒ Object
264 265 266 |
# File 'lib/capybara/poltergeist/driver.rb', line 264 def set_proxy(ip, port, type = "http", user = nil, password = nil) browser.set_proxy(ip, port, type, user, password) end |
#source ⇒ Object
124 125 126 |
# File 'lib/capybara/poltergeist/driver.rb', line 124 def source browser.source.to_s end |
#status_code ⇒ Object
115 116 117 |
# File 'lib/capybara/poltergeist/driver.rb', line 115 def status_code browser.status_code end |
#switch_to_frame(locator) ⇒ Object
175 176 177 |
# File 'lib/capybara/poltergeist/driver.rb', line 175 def switch_to_frame(locator) browser.switch_to_frame(locator) end |
#switch_to_window(handle) ⇒ Object
195 196 197 |
# File 'lib/capybara/poltergeist/driver.rb', line 195 def switch_to_window(handle) browser.switch_to_window(handle) end |
#timeout ⇒ Object
71 72 73 |
# File 'lib/capybara/poltergeist/driver.rb', line 71 def timeout server.timeout end |
#timeout=(sec) ⇒ Object
75 76 77 |
# File 'lib/capybara/poltergeist/driver.rb', line 75 def timeout=(sec) server.timeout = sec end |
#title ⇒ Object
128 129 130 131 132 133 134 |
# File 'lib/capybara/poltergeist/driver.rb', line 128 def title if Capybara::VERSION.to_f < 3.0 frame_title else browser.title end end |
#visit(url) ⇒ Object
98 99 100 101 |
# File 'lib/capybara/poltergeist/driver.rb', line 98 def visit(url) @started = true browser.visit(url) end |
#wait? ⇒ Boolean
372 373 374 |
# File 'lib/capybara/poltergeist/driver.rb', line 372 def wait? true end |
#window_handles ⇒ Object
183 184 185 |
# File 'lib/capybara/poltergeist/driver.rb', line 183 def window_handles browser.window_handles end |
#window_size(handle) ⇒ Object
246 247 248 249 250 |
# File 'lib/capybara/poltergeist/driver.rb', line 246 def window_size(handle) within_window(handle) do evaluate_script('[window.innerWidth, window.innerHeight]') end end |
#within_frame(name, &block) ⇒ Object
171 172 173 |
# File 'lib/capybara/poltergeist/driver.rb', line 171 def within_frame(name, &block) browser.within_frame(name, &block) end |
#within_window(name, &block) ⇒ Object
199 200 201 |
# File 'lib/capybara/poltergeist/driver.rb', line 199 def within_window(name, &block) browser.within_window(name, &block) end |
#zoom_factor=(zoom_factor) ⇒ Object
227 228 229 |
# File 'lib/capybara/poltergeist/driver.rb', line 227 def zoom_factor=(zoom_factor) browser.set_zoom_factor(zoom_factor) end |