Class: Capybara::Grid::Driver
- Inherits:
-
Driver::Base
- Object
- Driver::Base
- Capybara::Grid::Driver
- Defined in:
- lib/grid/extensions/capybara/driver.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{ :resynchronize => false, :resynchronization_timeout => 10, :browser => :firefox }
- SPECIAL_OPTIONS =
[:browser, :resynchronize, :resynchronization_timeout]
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#rack_server ⇒ Object
readonly
Returns the value of attribute rack_server.
Instance Method Summary collapse
- #body ⇒ Object
- #browser ⇒ Object
- #current_url ⇒ Object
- #evaluate_script(script) ⇒ Object
- #execute_script(script) ⇒ Object
- #find(selector) ⇒ Object
- #find_window(selector) ⇒ Object
-
#initialize(app, options = {}) ⇒ Driver
constructor
A new instance of Driver.
- #reset! ⇒ Object
- #resynchronize ⇒ Object
- #source ⇒ Object
- #visit(path) ⇒ Object
- #wait? ⇒ Boolean
- #within_frame(frame_id) ⇒ Object
- #within_window(selector, &blk) ⇒ Object
Constructor Details
#initialize(app, options = {}) ⇒ Driver
Returns a new instance of Driver.
28 29 30 31 32 33 |
# File 'lib/grid/extensions/capybara/driver.rb', line 28 def initialize(app, ={}) @app = app @options = DEFAULT_OPTIONS.merge() @rack_server = Capybara::Server.new(@app) @rack_server.boot if Capybara.run_server end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
9 10 11 |
# File 'lib/grid/extensions/capybara/driver.rb', line 9 def app @app end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/grid/extensions/capybara/driver.rb', line 9 def @options end |
#rack_server ⇒ Object (readonly)
Returns the value of attribute rack_server.
9 10 11 |
# File 'lib/grid/extensions/capybara/driver.rb', line 9 def rack_server @rack_server end |
Instance Method Details
#body ⇒ Object
43 44 45 |
# File 'lib/grid/extensions/capybara/driver.rb', line 43 def body browser.page_source end |
#browser ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/grid/extensions/capybara/driver.rb', line 11 def browser unless @browsers @options[:browser] = 'firefox' @grid = Grid.new(@options) @browsers = [] @grid.providers.each_with_index do |provider, index| @browsers[index] = provider[:object].new_browser :firefox end at_exit do @browsers.each do |browser| browser.quit end end end @browsers.first end |
#current_url ⇒ Object
47 48 49 |
# File 'lib/grid/extensions/capybara/driver.rb', line 47 def current_url browser.current_url end |
#evaluate_script(script) ⇒ Object
75 76 77 |
# File 'lib/grid/extensions/capybara/driver.rb', line 75 def evaluate_script(script) browser.execute_script "return #{script}" end |
#execute_script(script) ⇒ Object
71 72 73 |
# File 'lib/grid/extensions/capybara/driver.rb', line 71 def execute_script(script) browser.execute_script script end |
#find(selector) ⇒ Object
51 52 53 54 55 |
# File 'lib/grid/extensions/capybara/driver.rb', line 51 def find(selector) browser.find_elements(:xpath, selector).map do |node| Capybara::Grid::Node.new(self, node) end end |
#find_window(selector) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/grid/extensions/capybara/driver.rb', line 100 def find_window( selector ) original_handle = browser.window_handle browser.window_handles.each do |handle| browser.switch_to.window handle if( selector == browser.execute_script("return window.name") || browser.title.include?(selector) || browser.current_url.include?(selector) || (selector == handle) ) browser.switch_to.window original_handle return handle end end raise Capybara::ElementNotFound, "Could not find a window identified by #{selector}" end |
#reset! ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/grid/extensions/capybara/driver.rb', line 79 def reset! # Use instance variable directly so we avoid starting the browser just to reset the session if @browser begin @browser.manage. rescue Selenium::WebDriver::Error::UnhandledError => e # delete_all_cookies fails when we've previously gone # to about:blank, so we rescue this error and do nothing # instead. end @browser.navigate.to('about:blank') end end |
#resynchronize ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/grid/extensions/capybara/driver.rb', line 59 def resynchronize if [:resynchronize] load_wait_for_ajax_support yield Capybara.timeout([:resynchronization_timeout], self, "failed to resynchronize, ajax request timed out") do evaluate_script("!window.capybaraRequestsOutstanding") end else yield end end |
#source ⇒ Object
39 40 41 |
# File 'lib/grid/extensions/capybara/driver.rb', line 39 def source browser.page_source end |
#visit(path) ⇒ Object
35 36 37 |
# File 'lib/grid/extensions/capybara/driver.rb', line 35 def visit(path) browser.navigate.to(url(path)) end |
#wait? ⇒ Boolean
57 |
# File 'lib/grid/extensions/capybara/driver.rb', line 57 def wait?; true; end |
#within_frame(frame_id) ⇒ Object
93 94 95 96 97 98 |
# File 'lib/grid/extensions/capybara/driver.rb', line 93 def within_frame(frame_id) old_window = browser.window_handle browser.switch_to.frame(frame_id) yield browser.switch_to.window old_window end |
#within_window(selector, &blk) ⇒ Object
115 116 117 118 |
# File 'lib/grid/extensions/capybara/driver.rb', line 115 def within_window(selector, &blk) handle = find_window( selector ) browser.switch_to.window(handle, &blk) end |