Class: Capybara::Cuprite::Driver
- Inherits:
-
Driver::Base
- Object
- Driver::Base
- Capybara::Cuprite::Driver
- Extended by:
- Forwardable
- Defined in:
- lib/capybara/cuprite/driver.rb
Constant Summary collapse
- DEFAULT_MAXIMIZE_SCREEN_SIZE =
[1366, 768].freeze
- EXTENSION =
File.("javascripts/index.js", __dir__)
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#screen_size ⇒ Object
readonly
Returns the value of attribute screen_size.
Instance Method Summary collapse
- #accept_modal(type, options = {}) ⇒ Object
- #add_header(name, value, permanent: true) ⇒ Object
- #add_headers(headers) ⇒ Object
- #basic_authorize(user, password) ⇒ Object (also: #authorize)
- #browser ⇒ Object
- #clear_cookies ⇒ Object
- #clear_memory_cache ⇒ Object
- #clear_network_traffic ⇒ Object
- #click(x, y) ⇒ Object
- #close_window(handle) ⇒ Object
- #cookies ⇒ Object
- #current_url ⇒ Object
- #current_window_handle ⇒ Object
- #debug(binding = nil) ⇒ Object
- #debug_url ⇒ 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
- #fullscreen_window(handle) ⇒ 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.
- #invalid_element_errors ⇒ Object
- #maximize_window(handle) ⇒ Object
- #needs_server? ⇒ Boolean
- #network_traffic(type = nil) ⇒ Object
- #no_such_window_error ⇒ Object
- #open_new_window ⇒ Object
- #paper_size=(value) ⇒ Object
- #pause ⇒ Object
- #refresh ⇒ Object
- #remove_cookie(name, **options) ⇒ 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
- #save_screenshot(path, options = {}) ⇒ Object (also: #render)
- #scroll_to(left, top) ⇒ Object
- #set_cookie(name, value, options = {}) ⇒ Object
- #set_proxy(ip, port, type = nil, user = nil, password = nil, bypass = nil) ⇒ Object
- #source ⇒ Object
- #switch_to_frame(locator) ⇒ Object
- #switch_to_window(handle) ⇒ Object
- #title ⇒ Object
- #visit(url) ⇒ Object
- #wait? ⇒ Boolean
- #wait_for_network_idle(**options) ⇒ Object
- #window_handles ⇒ Object
- #window_size(handle) ⇒ Object
- #within_window(name, &block) ⇒ Object
- #zoom_factor=(value) ⇒ Object
Constructor Details
#initialize(app, options = {}) ⇒ Driver
Returns a new instance of Driver.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/capybara/cuprite/driver.rb', line 17 def initialize(app, = {}) @app = app @options = .dup @started = false @options[:extensions] ||= [] @options[:extensions] << EXTENSION @screen_size = @options.delete(:screen_size) @screen_size ||= DEFAULT_MAXIMIZE_SCREEN_SIZE @options[:save_path] = Capybara.save_path.to_s if Capybara.save_path ENV["FERRUM_DEBUG"] = "true" if ENV["CUPRITE_DEBUG"] end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
15 16 17 |
# File 'lib/capybara/cuprite/driver.rb', line 15 def app @app end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
15 16 17 |
# File 'lib/capybara/cuprite/driver.rb', line 15 def @options end |
#screen_size ⇒ Object (readonly)
Returns the value of attribute screen_size.
15 16 17 |
# File 'lib/capybara/cuprite/driver.rb', line 15 def screen_size @screen_size end |
Instance Method Details
#accept_modal(type, options = {}) ⇒ Object
377 378 379 380 381 382 383 384 385 386 387 388 |
# File 'lib/capybara/cuprite/driver.rb', line 377 def accept_modal(type, = {}) case type when :alert, :confirm browser.accept_confirm when :prompt browser.accept_prompt([:with]) end yield if block_given? browser.find_modal() end |
#add_header(name, value, permanent: true) ⇒ Object
261 262 263 |
# File 'lib/capybara/cuprite/driver.rb', line 261 def add_header(name, value, permanent: true) browser.headers.add({ name => value }, permanent: permanent) end |
#add_headers(headers) ⇒ Object
257 258 259 |
# File 'lib/capybara/cuprite/driver.rb', line 257 def add_headers(headers) browser.headers.add(headers) end |
#basic_authorize(user, password) ⇒ Object Also known as:
298 299 300 |
# File 'lib/capybara/cuprite/driver.rb', line 298 def (user, password) browser.network.(user: user, password: password) end |
#browser ⇒ Object
37 38 39 |
# File 'lib/capybara/cuprite/driver.rb', line 37 def browser @browser ||= Browser.new(@options) end |
#clear_cookies ⇒ Object
286 287 288 |
# File 'lib/capybara/cuprite/driver.rb', line 286 def browser..clear end |
#clear_memory_cache ⇒ Object
294 295 296 |
# File 'lib/capybara/cuprite/driver.rb', line 294 def clear_memory_cache browser.network.clear(:cache) end |
#clear_network_traffic ⇒ Object
237 238 239 |
# File 'lib/capybara/cuprite/driver.rb', line 237 def clear_network_traffic browser.network.clear(:traffic) end |
#click(x, y) ⇒ Object
91 92 93 |
# File 'lib/capybara/cuprite/driver.rb', line 91 def click(x, y) browser.mouse.click(x: x, y: y) end |
#close_window(handle) ⇒ Object
129 130 131 |
# File 'lib/capybara/cuprite/driver.rb', line 129 def close_window(handle) browser.close_window(handle) end |
#cookies ⇒ Object
269 270 271 |
# File 'lib/capybara/cuprite/driver.rb', line 269 def browser..all end |
#current_url ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/capybara/cuprite/driver.rb', line 46 def current_url if Capybara::VERSION.to_f < 3.0 frame_url else browser.current_url end end |
#current_window_handle ⇒ Object
121 122 123 |
# File 'lib/capybara/cuprite/driver.rb', line 121 def current_window_handle browser.window_handle end |
#debug(binding = nil) ⇒ Object
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/capybara/cuprite/driver.rb', line 307 def debug(binding = nil) if @options[:inspector] Process.spawn(browser.process.path, debug_url) if binding&.respond_to?(:pry) Pry.start(binding) elsif binding&.respond_to?(:irb) binding.irb else pause end else raise Error, "To use the remote debugging, you have to launch " \ "the driver with `inspector: ENV['INSPECTOR']` " \ "configuration option and run your test suite passing " \ "env variable" end end |
#debug_url ⇒ Object
303 304 305 |
# File 'lib/capybara/cuprite/driver.rb', line 303 def debug_url "http://#{browser.process.host}:#{browser.process.port}" end |
#dismiss_modal(type, options = {}) ⇒ Object
390 391 392 393 394 395 396 397 398 399 400 401 |
# File 'lib/capybara/cuprite/driver.rb', line 390 def dismiss_modal(type, = {}) case type when :confirm browser.dismiss_confirm when :prompt browser.dismiss_prompt end yield if block_given? browser.find_modal() end |
#evaluate_async_script(script, *args) ⇒ Object
100 101 102 103 |
# File 'lib/capybara/cuprite/driver.rb', line 100 def evaluate_async_script(script, *args) result = browser.evaluate_async(script, session_wait_time, *native_args(args)) unwrap_script_result(result) end |
#evaluate_script(script, *args) ⇒ Object
95 96 97 98 |
# File 'lib/capybara/cuprite/driver.rb', line 95 def evaluate_script(script, *args) result = browser.evaluate(script, *native_args(args)) unwrap_script_result(result) end |
#execute_script(script, *args) ⇒ Object
105 106 107 108 |
# File 'lib/capybara/cuprite/driver.rb', line 105 def execute_script(script, *args) browser.execute(script, *native_args(args)) nil end |
#find(method, selector) ⇒ Object
87 88 89 |
# File 'lib/capybara/cuprite/driver.rb', line 87 def find(method, selector) browser.find(method, selector).map { |native| Node.new(self, native) } end |
#find_css(selector) ⇒ Object
83 84 85 |
# File 'lib/capybara/cuprite/driver.rb', line 83 def find_css(selector) find(:css, selector) end |
#find_xpath(selector) ⇒ Object
79 80 81 |
# File 'lib/capybara/cuprite/driver.rb', line 79 def find_xpath(selector) find(:xpath, selector) end |
#frame_title ⇒ Object
75 76 77 |
# File 'lib/capybara/cuprite/driver.rb', line 75 def frame_title evaluate_script("document.title") end |
#frame_url ⇒ Object
54 55 56 |
# File 'lib/capybara/cuprite/driver.rb', line 54 def frame_url evaluate_script("window.location.href") end |
#fullscreen_window(handle) ⇒ Object
213 214 215 216 217 |
# File 'lib/capybara/cuprite/driver.rb', line 213 def fullscreen_window(handle) within_window(handle) do browser.resize(fullscreen: true) end end |
#go_back ⇒ Object
365 366 367 |
# File 'lib/capybara/cuprite/driver.rb', line 365 def go_back browser.back end |
#go_forward ⇒ Object
369 370 371 |
# File 'lib/capybara/cuprite/driver.rb', line 369 def go_forward browser.forward end |
#headers ⇒ Object
249 250 251 |
# File 'lib/capybara/cuprite/driver.rb', line 249 def headers browser.headers.get end |
#headers=(headers) ⇒ Object
253 254 255 |
# File 'lib/capybara/cuprite/driver.rb', line 253 def headers=(headers) browser.headers.set(headers) end |
#html ⇒ Object Also known as: body
58 59 60 |
# File 'lib/capybara/cuprite/driver.rb', line 58 def html browser.body end |
#invalid_element_errors ⇒ Object
359 360 361 362 363 |
# File 'lib/capybara/cuprite/driver.rb', line 359 def invalid_element_errors [Capybara::Cuprite::ObsoleteNode, Capybara::Cuprite::MouseEventFailed, Ferrum::NoExecutionContextError] end |
#maximize_window(handle) ⇒ Object
203 204 205 |
# File 'lib/capybara/cuprite/driver.rb', line 203 def maximize_window(handle) resize_window_to(handle, *screen_size) end |
#needs_server? ⇒ Boolean
33 34 35 |
# File 'lib/capybara/cuprite/driver.rb', line 33 def needs_server? true end |
#network_traffic(type = nil) ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/capybara/cuprite/driver.rb', line 223 def network_traffic(type = nil) traffic = browser.network.traffic case type.to_s when "all" traffic when "blocked" traffic.select(&:blocked?) else # when request isn't blocked traffic.reject(&:blocked?) end end |
#no_such_window_error ⇒ Object
148 149 150 |
# File 'lib/capybara/cuprite/driver.rb', line 148 def no_such_window_error Ferrum::NoSuchPageError end |
#open_new_window ⇒ Object
133 134 135 136 137 138 |
# File 'lib/capybara/cuprite/driver.rb', line 133 def open_new_window target = browser.default_context.create_target target.maybe_sleep_if_new_window target.page = Page.new(target.id, browser) target.page end |
#paper_size=(value) ⇒ Object
188 189 190 |
# File 'lib/capybara/cuprite/driver.rb', line 188 def paper_size=(value) @paper_size = value end |
#pause ⇒ Object
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/capybara/cuprite/driver.rb', line 326 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 = Thread.new { IO.copy_stream(STDIN, write); write.close } STDERR.puts "Cuprite 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 unless signal begin input = read.read_nonblock(80) # clear out the read buffer puts unless input&.end_with?("\n") rescue EOFError, IO::WaitReadable # Ignore problems reading from STDIN. end end ensure thread.kill read.close trap("SIGCONT", old_trap) # Restore the previous signal handler, if there was one. STDERR.puts "Continuing" end |
#refresh ⇒ Object
373 374 375 |
# File 'lib/capybara/cuprite/driver.rb', line 373 def refresh browser.refresh end |
#remove_cookie(name, **options) ⇒ Object
281 282 283 284 |
# File 'lib/capybara/cuprite/driver.rb', line 281 def (name, **) [:domain] = default_domain if .empty? browser..remove(**.merge(name: name)) end |
#render_base64(format = :png, options = {}) ⇒ Object
174 175 176 177 178 179 180 181 182 |
# File 'lib/capybara/cuprite/driver.rb', line 174 def render_base64(format = :png, = {}) if pdf?(nil, ) [:paperWidth] = @paper_size[:width].to_f if @paper_size [:paperHeight] = @paper_size[:height].to_f if @paper_size browser.pdf(encoding: :base64, **) else browser.screenshot(format: format, encoding: :base64, **) end end |
#reset! ⇒ Object
152 153 154 155 156 157 158 159 |
# File 'lib/capybara/cuprite/driver.rb', line 152 def reset! @zoom_factor = nil @paper_size = nil browser.url_blacklist = @options[:url_blacklist] browser.url_whitelist = @options[:url_whitelist] browser.reset @started = false end |
#resize(width, height) ⇒ Object Also known as: resize_window
192 193 194 |
# File 'lib/capybara/cuprite/driver.rb', line 192 def resize(width, height) browser.resize(width: width, height: height) end |
#resize_window_to(handle, width, height) ⇒ Object
197 198 199 200 201 |
# File 'lib/capybara/cuprite/driver.rb', line 197 def resize_window_to(handle, width, height) within_window(handle) do resize(width, height) end end |
#response_headers ⇒ Object
265 266 267 |
# File 'lib/capybara/cuprite/driver.rb', line 265 def response_headers browser.network.response&.headers end |
#save_screenshot(path, options = {}) ⇒ Object Also known as: render
161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/capybara/cuprite/driver.rb', line 161 def save_screenshot(path, = {}) [:scale] = @zoom_factor if @zoom_factor if pdf?(path, ) [:paperWidth] = @paper_size[:width].to_f if @paper_size [:paperHeight] = @paper_size[:height].to_f if @paper_size browser.pdf(path: path, **) else browser.screenshot(path: path, **) end end |
#scroll_to(left, top) ⇒ Object
219 220 221 |
# File 'lib/capybara/cuprite/driver.rb', line 219 def scroll_to(left, top) browser.mouse.scroll_to(left, top) end |
#set_cookie(name, value, options = {}) ⇒ Object
273 274 275 276 277 278 279 |
# File 'lib/capybara/cuprite/driver.rb', line 273 def (name, value, = {}) = .dup [:name] ||= name [:value] ||= value [:domain] ||= default_domain browser..set(**) end |
#set_proxy(ip, port, type = nil, user = nil, password = nil, bypass = nil) ⇒ Object
241 242 243 244 245 246 247 |
# File 'lib/capybara/cuprite/driver.rb', line 241 def set_proxy(ip, port, type = nil, user = nil, password = nil, bypass = nil) @options[:browser_options] ||= {} server = type ? "#{type}=#{ip}:#{port}" : "#{ip}:#{port}" @options[:browser_options].merge!("proxy-server" => server) @options[:browser_options].merge!("proxy-bypass-list" => bypass) if bypass browser.network.(type: :proxy, user: user, password: password) end |
#source ⇒ Object
63 64 65 |
# File 'lib/capybara/cuprite/driver.rb', line 63 def source browser.source.to_s end |
#switch_to_frame(locator) ⇒ Object
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/capybara/cuprite/driver.rb', line 110 def switch_to_frame(locator) handle = case locator when Capybara::Node::Element locator.native.description["frameId"] when :parent, :top locator end browser.switch_to_frame(handle) end |
#switch_to_window(handle) ⇒ Object
140 141 142 |
# File 'lib/capybara/cuprite/driver.rb', line 140 def switch_to_window(handle) browser.switch_to_window(handle) end |
#title ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/capybara/cuprite/driver.rb', line 67 def title if Capybara::VERSION.to_f < 3.0 frame_title else browser.title end end |
#visit(url) ⇒ Object
41 42 43 44 |
# File 'lib/capybara/cuprite/driver.rb', line 41 def visit(url) @started = true browser.visit(url) end |
#wait? ⇒ Boolean
355 356 357 |
# File 'lib/capybara/cuprite/driver.rb', line 355 def wait? true end |
#wait_for_network_idle(**options) ⇒ Object
290 291 292 |
# File 'lib/capybara/cuprite/driver.rb', line 290 def wait_for_network_idle(**) browser.network.wait_for_idle(**) end |
#window_handles ⇒ Object
125 126 127 |
# File 'lib/capybara/cuprite/driver.rb', line 125 def window_handles browser.window_handles end |
#window_size(handle) ⇒ Object
207 208 209 210 211 |
# File 'lib/capybara/cuprite/driver.rb', line 207 def window_size(handle) within_window(handle) do evaluate_script("[window.innerWidth, window.innerHeight]") end end |
#within_window(name, &block) ⇒ Object
144 145 146 |
# File 'lib/capybara/cuprite/driver.rb', line 144 def within_window(name, &block) browser.within_window(name, &block) end |
#zoom_factor=(value) ⇒ Object
184 185 186 |
# File 'lib/capybara/cuprite/driver.rb', line 184 def zoom_factor=(value) @zoom_factor = value.to_f end |