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.
-
#browser ⇒ Object
readonly
Returns the value of attribute browser.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
Instance Method Summary collapse
- #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(’:‘)).
-
- #clear_cookies ⇒ Object
- #clear_network_traffic ⇒ Object
- #click(x, y) ⇒ Object
- #client_pid ⇒ Object
- #close_window(handle) ⇒ Object
- #cookies ⇒ Object
- #cookies_enabled=(flag) ⇒ Object
- #current_url ⇒ Object
- #current_window_handle ⇒ Object
- #debug ⇒ Object
- #evaluate_script(script) ⇒ Object
- #execute_script(script) ⇒ Object
- #find(method, selector) ⇒ Object
- #find_css(selector) ⇒ Object
- #find_xpath(selector) ⇒ 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.
- #needs_server? ⇒ Boolean
- #network_traffic ⇒ 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
- #remove_cookie(name) ⇒ Object
- #render_base64(format = :png, options = {}) ⇒ Object
- #reset! ⇒ Object
- #resize(width, height) ⇒ Object (also: #resize_window)
- #response_headers ⇒ Object
- #restart ⇒ Object
- #save_screenshot(path, options = {}) ⇒ Object (also: #render)
- #scroll_to(left, top) ⇒ Object
- #set_cookie(name, value, options = {}) ⇒ Object
- #source ⇒ Object
- #status_code ⇒ Object
- #switch_to_window(handle) ⇒ Object
- #timeout ⇒ Object
- #timeout=(sec) ⇒ Object
- #title ⇒ Object
- #visit(url) ⇒ Object
- #wait? ⇒ Boolean
- #window_handles ⇒ 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.
9 10 11 12 13 14 15 16 17 |
# File 'lib/capybara/poltergeist/driver.rb', line 9 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.
7 8 9 |
# File 'lib/capybara/poltergeist/driver.rb', line 7 def app @app end |
#browser ⇒ Object (readonly)
Returns the value of attribute browser.
7 8 9 |
# File 'lib/capybara/poltergeist/driver.rb', line 7 def browser @browser end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
7 8 9 |
# File 'lib/capybara/poltergeist/driver.rb', line 7 def client @client end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/capybara/poltergeist/driver.rb', line 7 def @options end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
7 8 9 |
# File 'lib/capybara/poltergeist/driver.rb', line 7 def server @server end |
Instance Method Details
#add_header(name, value, options = {}) ⇒ Object
227 228 229 230 |
# File 'lib/capybara/poltergeist/driver.rb', line 227 def add_header(name, value, = {}) permanent = .fetch(:permanent, true) browser.add_header({ name => value }, permanent) end |
#add_headers(headers) ⇒ Object
223 224 225 |
# File 'lib/capybara/poltergeist/driver.rb', line 223 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.
271 272 273 274 275 |
# File 'lib/capybara/poltergeist/driver.rb', line 271 def (user, password) browser.set_http_auth(user, password) credentials = ["#{user}:#{password}"].pack('m*').strip add_header('Authorization', "Basic #{credentials}") end |
#clear_cookies ⇒ Object
258 259 260 |
# File 'lib/capybara/poltergeist/driver.rb', line 258 def browser. end |
#clear_network_traffic ⇒ Object
211 212 213 |
# File 'lib/capybara/poltergeist/driver.rb', line 211 def clear_network_traffic browser.clear_network_traffic end |
#click(x, y) ⇒ Object
131 132 133 |
# File 'lib/capybara/poltergeist/driver.rb', line 131 def click(x, y) browser.click_coordinates(x, y) end |
#client_pid ⇒ Object
62 63 64 |
# File 'lib/capybara/poltergeist/driver.rb', line 62 def client_pid client.pid end |
#close_window(handle) ⇒ Object
156 157 158 |
# File 'lib/capybara/poltergeist/driver.rb', line 156 def close_window(handle) browser.close_window(handle) end |
#cookies ⇒ Object
236 237 238 |
# File 'lib/capybara/poltergeist/driver.rb', line 236 def browser. end |
#cookies_enabled=(flag) ⇒ Object
262 263 264 |
# File 'lib/capybara/poltergeist/driver.rb', line 262 def (flag) browser. = flag end |
#current_url ⇒ Object
98 99 100 |
# File 'lib/capybara/poltergeist/driver.rb', line 98 def current_url browser.current_url end |
#current_window_handle ⇒ Object
148 149 150 |
# File 'lib/capybara/poltergeist/driver.rb', line 148 def current_window_handle browser.window_handle end |
#debug ⇒ Object
277 278 279 280 281 282 283 284 285 |
# File 'lib/capybara/poltergeist/driver.rb', line 277 def debug if @options[:inspector] inspector.open pause else raise Error, "To use the remote debugging, you have to launch the driver " \ "with `:inspector => true` configuration option" end end |
#evaluate_script(script) ⇒ Object
135 136 137 |
# File 'lib/capybara/poltergeist/driver.rb', line 135 def evaluate_script(script) browser.evaluate(script) end |
#execute_script(script) ⇒ Object
139 140 141 142 |
# File 'lib/capybara/poltergeist/driver.rb', line 139 def execute_script(script) browser.execute(script) nil end |
#find(method, selector) ⇒ Object
119 120 121 |
# File 'lib/capybara/poltergeist/driver.rb', line 119 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
127 128 129 |
# File 'lib/capybara/poltergeist/driver.rb', line 127 def find_css(selector) find :css, selector end |
#find_xpath(selector) ⇒ Object
123 124 125 |
# File 'lib/capybara/poltergeist/driver.rb', line 123 def find_xpath(selector) find :xpath, selector end |
#go_back ⇒ Object
300 301 302 |
# File 'lib/capybara/poltergeist/driver.rb', line 300 def go_back browser.go_back end |
#go_forward ⇒ Object
304 305 306 |
# File 'lib/capybara/poltergeist/driver.rb', line 304 def go_forward browser.go_forward end |
#headers ⇒ Object
215 216 217 |
# File 'lib/capybara/poltergeist/driver.rb', line 215 def headers browser.get_headers end |
#headers=(headers) ⇒ Object
219 220 221 |
# File 'lib/capybara/poltergeist/driver.rb', line 219 def headers=(headers) browser.set_headers(headers) end |
#html ⇒ Object Also known as: body
106 107 108 |
# File 'lib/capybara/poltergeist/driver.rb', line 106 def html browser.body end |
#inspector ⇒ Object
33 34 35 |
# File 'lib/capybara/poltergeist/driver.rb', line 33 def inspector @inspector ||= [:inspector] && Inspector.new([:inspector]) end |
#invalid_element_errors ⇒ Object
296 297 298 |
# File 'lib/capybara/poltergeist/driver.rb', line 296 def invalid_element_errors [Capybara::Poltergeist::ObsoleteNode, Capybara::Poltergeist::MouseEventFailed] end |
#logger ⇒ Object
logger should be an object that responds to puts, or nil
84 85 86 |
# File 'lib/capybara/poltergeist/driver.rb', line 84 def logger [:logger] || ([:debug] && STDERR) end |
#needs_server? ⇒ Boolean
19 20 21 |
# File 'lib/capybara/poltergeist/driver.rb', line 19 def needs_server? true end |
#network_traffic ⇒ Object
207 208 209 |
# File 'lib/capybara/poltergeist/driver.rb', line 207 def network_traffic browser.network_traffic end |
#no_such_window_error ⇒ Object
172 173 174 |
# File 'lib/capybara/poltergeist/driver.rb', line 172 def no_such_window_error NoSuchWindowError end |
#open_new_window ⇒ Object
160 161 162 |
# File 'lib/capybara/poltergeist/driver.rb', line 160 def open_new_window browser.open_new_window end |
#paper_size=(size = {}) ⇒ Object
190 191 192 |
# File 'lib/capybara/poltergeist/driver.rb', line 190 def paper_size=(size = {}) browser.set_paper_size(size) end |
#pause ⇒ Object
287 288 289 290 |
# File 'lib/capybara/poltergeist/driver.rb', line 287 def pause STDERR.puts "Poltergeist execution paused. Press enter to continue." STDIN.gets end |
#phantomjs_logger ⇒ Object
logger should be an object that behaves like IO or nil
89 90 91 |
# File 'lib/capybara/poltergeist/driver.rb', line 89 def phantomjs_logger .fetch(:phantomjs_logger, nil) end |
#phantomjs_options ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/capybara/poltergeist/driver.rb', line 50 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 += ["--ssl-protocol=any"] unless list.grep(/ssl-protocol/).any? list += ["--remote-debugger-port=#{inspector.port}", "--remote-debugger-autorun=yes"] if inspector list end |
#quit ⇒ Object
78 79 80 81 |
# File 'lib/capybara/poltergeist/driver.rb', line 78 def quit server.stop client.stop end |
#remove_cookie(name) ⇒ Object
254 255 256 |
# File 'lib/capybara/poltergeist/driver.rb', line 254 def (name) browser.(name) end |
#render_base64(format = :png, options = {}) ⇒ Object
186 187 188 |
# File 'lib/capybara/poltergeist/driver.rb', line 186 def render_base64(format = :png, = {}) browser.render_base64(format, ) end |
#reset! ⇒ Object
176 177 178 179 |
# File 'lib/capybara/poltergeist/driver.rb', line 176 def reset! browser.reset @started = false end |
#resize(width, height) ⇒ Object Also known as: resize_window
198 199 200 |
# File 'lib/capybara/poltergeist/driver.rb', line 198 def resize(width, height) browser.resize(width, height) end |
#response_headers ⇒ Object
232 233 234 |
# File 'lib/capybara/poltergeist/driver.rb', line 232 def response_headers browser.response_headers end |
#restart ⇒ Object
74 75 76 |
# File 'lib/capybara/poltergeist/driver.rb', line 74 def restart browser.restart end |
#save_screenshot(path, options = {}) ⇒ Object Also known as: render
181 182 183 |
# File 'lib/capybara/poltergeist/driver.rb', line 181 def save_screenshot(path, = {}) browser.render(path, ) end |
#scroll_to(left, top) ⇒ Object
203 204 205 |
# File 'lib/capybara/poltergeist/driver.rb', line 203 def scroll_to(left, top) browser.scroll_to(left, top) end |
#set_cookie(name, value, options = {}) ⇒ Object
240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/capybara/poltergeist/driver.rb', line 240 def (name, value, = {}) [:name] ||= name [:value] ||= value [:domain] ||= begin if @started URI.parse(browser.current_url).host else Capybara.app_host || "127.0.0.1" end end browser.() end |
#source ⇒ Object
111 112 113 |
# File 'lib/capybara/poltergeist/driver.rb', line 111 def source browser.source.to_s end |
#status_code ⇒ Object
102 103 104 |
# File 'lib/capybara/poltergeist/driver.rb', line 102 def status_code browser.status_code end |
#switch_to_window(handle) ⇒ Object
164 165 166 |
# File 'lib/capybara/poltergeist/driver.rb', line 164 def switch_to_window(handle) browser.switch_to_window(handle) end |
#timeout ⇒ Object
66 67 68 |
# File 'lib/capybara/poltergeist/driver.rb', line 66 def timeout server.timeout end |
#timeout=(sec) ⇒ Object
70 71 72 |
# File 'lib/capybara/poltergeist/driver.rb', line 70 def timeout=(sec) server.timeout = sec end |
#title ⇒ Object
115 116 117 |
# File 'lib/capybara/poltergeist/driver.rb', line 115 def title browser.title end |
#visit(url) ⇒ Object
93 94 95 96 |
# File 'lib/capybara/poltergeist/driver.rb', line 93 def visit(url) @started = true browser.visit(url) end |
#wait? ⇒ Boolean
292 293 294 |
# File 'lib/capybara/poltergeist/driver.rb', line 292 def wait? true end |
#window_handles ⇒ Object
152 153 154 |
# File 'lib/capybara/poltergeist/driver.rb', line 152 def window_handles browser.window_handles end |
#within_frame(name, &block) ⇒ Object
144 145 146 |
# File 'lib/capybara/poltergeist/driver.rb', line 144 def within_frame(name, &block) browser.within_frame(name, &block) end |
#within_window(name, &block) ⇒ Object
168 169 170 |
# File 'lib/capybara/poltergeist/driver.rb', line 168 def within_window(name, &block) browser.within_window(name, &block) end |
#zoom_factor=(zoom_factor) ⇒ Object
194 195 196 |
# File 'lib/capybara/poltergeist/driver.rb', line 194 def zoom_factor=(zoom_factor) browser.set_zoom_factor(zoom_factor) end |