diff --git a/lib/capybara/webkit/driver.rb b/lib/capybara/webkit/driver.rb index 4cfc248..cfa42a5 100644 --- a/lib/capybara/webkit/driver.rb +++ b/lib/capybara/webkit/driver.rb @@ -8,8 +8,6 @@ require "capybara/webkit/errors" module Capybara::Webkit class Driver < Capybara::Driver::Base - attr_reader :browser - def initialize(app, options={}) @app = app @options = options @@ -17,98 +15,102 @@ module Capybara::Webkit end def enable_logging - browser.enable_logging + @browser.enable_logging end def allow_url(url) - browser.allow_url(url) + @browser.allow_url(url) end def block_url(url) - browser.block_url(url) + @browser.block_url(url) end def block_unknown_urls - browser.block_unknown_urls + @browser.block_unknown_urls end def allow_unknown_urls - browser.allow_url("*") + @browser.allow_url("*") end def current_url - browser.current_url + @browser.current_url end def visit(path) - browser.visit(path) + @browser.visit(path) end def find_xpath(xpath) - browser.find_xpath(xpath).map { |native| Node.new(self, native) } + @browser. + find_xpath(xpath). + map { |native| Node.new(self, native, @browser) } end alias_method :find, :find_xpath def find_css(selector) - browser.find_css(selector).map { |native| Node.new(self, native) } + @browser. + find_css(selector). + map { |native| Node.new(self, native, @browser) } end def html - browser.body + @browser.body end def header(key, value) - browser.header(key, value) + @browser.header(key, value) end def title - browser.title + @browser.title end def execute_script(script) - value = browser.execute_script script + value = @browser.execute_script script value.empty? ? nil : value end def evaluate_script(script) - browser.evaluate_script script + @browser.evaluate_script script end def console_messages - browser.console_messages + @browser.console_messages end def error_messages - browser.error_messages + @browser.error_messages end def alert_messages warn '[DEPRECATION] Capybara::Webkit::Driver#alert_messages ' \ 'is deprecated. Please use Capybara::Session#accept_alert instead.' - browser.alert_messages + @browser.alert_messages end def confirm_messages warn '[DEPRECATION] Capybara::Webkit::Driver#confirm_messages ' \ 'is deprecated. Please use Capybara::Session#accept_confirm ' \ 'or Capybara::Session#dismiss_confirm instead.' - browser.confirm_messages + @browser.confirm_messages end def prompt_messages warn '[DEPRECATION] Capybara::Webkit::Driver#prompt_messages ' \ 'is deprecated. Please use Capybara::Session#accept_prompt ' \ 'or Capybara::Session#dismiss_prompt instead.' - browser.prompt_messages + @browser.prompt_messages end def response_headers - browser.response_headers + @browser.response_headers end def status_code - browser.status_code + @browser.status_code end def resize_window(width, height) @@ -118,19 +120,19 @@ module Capybara::Webkit end def resize_window_to(handle, width, height) - browser.window_resize(handle, width, height) + @browser.window_resize(handle, width, height) end def window_size(handle) - browser.window_size(handle) + @browser.window_size(handle) end def within_frame(selector) - browser.frame_focus(selector) + @browser.frame_focus(selector) begin yield ensure - browser.frame_focus + @browser.frame_focus end end @@ -140,74 +142,74 @@ module Capybara::Webkit begin yield ensure - browser.window_focus(current_window) + @browser.window_focus(current_window) end end def switch_to_window(selector) - browser.window_focus(selector) + @browser.window_focus(selector) end def window_handles - browser.get_window_handles + @browser.get_window_handles end def current_window_handle - browser.get_window_handle + @browser.get_window_handle end def open_new_window - browser.window_open + @browser.window_open end def close_window(selector) - browser.window_close(selector) + @browser.window_close(selector) end def maximize_window(selector) - browser.window_maximize(selector) + @browser.window_maximize(selector) end def accept_js_confirms! warn '[DEPRECATION] Capybara::Webkit::Driver#accept_js_confirms! ' \ 'is deprecated. Please use Capybara::Session#accept_confirm instead.' - browser.accept_js_confirms + @browser.accept_js_confirms end def dismiss_js_confirms! warn '[DEPRECATION] Capybara::Webkit::Driver#dismiss_js_confirms! ' \ 'is deprecated. Please use Capybara::Session#dismiss_confirm instead.' - browser.reject_js_confirms + @browser.reject_js_confirms end def accept_js_prompts! warn '[DEPRECATION] Capybara::Webkit::Driver#accept_js_prompts! ' \ 'is deprecated. Please use Capybara::Session#accept_prompt instead.' - browser.accept_js_prompts + @browser.accept_js_prompts end def dismiss_js_prompts! warn '[DEPRECATION] Capybara::Webkit::Driver#dismiss_js_prompts! ' \ 'is deprecated. Please use Capybara::Session#dismiss_prompt instead.' - browser.reject_js_prompts + @browser.reject_js_prompts end def js_prompt_input=(value) warn '[DEPRECATION] Capybara::Webkit::Driver#js_prompt_input= ' \ 'is deprecated. Please use Capybara::Session#accept_prompt instead.' if value.nil? - browser.clear_prompt_text + @browser.clear_prompt_text else - browser.set_prompt_text_to(value) + @browser.set_prompt_text_to(value) end end def go_back - browser.go_back + @browser.go_back end def go_forward - browser.go_forward + @browser.go_forward end def accept_modal(type, options={}) @@ -215,11 +217,11 @@ module Capybara::Webkit case type when :confirm - id = browser.accept_confirm(options) + id = @browser.accept_confirm(options) when :prompt - id = browser.accept_prompt(options) + id = @browser.accept_prompt(options) else - id = browser.accept_alert(options) + id = @browser.accept_alert(options) end yield @@ -232,9 +234,9 @@ module Capybara::Webkit case type when :confirm - id = browser.reject_confirm(options) + id = @browser.reject_confirm(options) else - id = browser.reject_prompt(options) + id = @browser.reject_prompt(options) end yield @@ -251,7 +253,7 @@ module Capybara::Webkit end def reset! - browser.reset! + @browser.reset! end def has_shortcircuit_timeout? @@ -262,11 +264,19 @@ module Capybara::Webkit options[:width] ||= 1000 options[:height] ||= 10 - browser.render path, options[:width], options[:height] + @browser.render path, options[:width], options[:height] end def cookies - @cookie_jar ||= CookieJar.new(browser) + @cookie_jar ||= CookieJar.new(@browser) + end + + def set_cookie(cookie) + @browser.set_cookie(cookie) + end + + def clear_cookies + @browser.clear_cookies end def invalid_element_errors @@ -281,10 +291,27 @@ module Capybara::Webkit [ "Capybara: #{Capybara::VERSION}", "capybara-webkit: #{Capybara::Driver::Webkit::VERSION}", - browser.version + @browser.version ].join("\n") end + def authenticate(username, password) + @browser.authenticate(username, password) + end + + def timeout + @browser.timeout + end + + def timeout=(timeout) + @browser.timeout = timeout + end + + def browser + warn "[DEPRECATION] Capybara::Webkit::Driver#browser is deprecated." + @browser + end + private def modal_action_options_for_browser(options) @@ -297,7 +324,7 @@ module Capybara::Webkit def find_modal(type, id, options) Timeout::timeout(options[:wait] || Capybara.default_wait_time) do - browser.find_modal(id) + @browser.find_modal(id) end rescue ModalNotFound raise Capybara::ModalNotFound, diff --git a/lib/capybara/webkit/node.rb b/lib/capybara/webkit/node.rb index 3864c82..945615a 100644 --- a/lib/capybara/webkit/node.rb +++ b/lib/capybara/webkit/node.rb @@ -1,5 +1,10 @@ module Capybara::Webkit class Node < Capybara::Driver::Node + def initialize(session, base, browser) + super(session, base) + @browser = browser + end + def visible_text Capybara::Helpers.normalize_whitespace(invoke("text")) end @@ -113,7 +118,7 @@ module Capybara::Webkit def find_xpath(xpath) invoke("findXpathWithin", xpath).split(',').map do |native| - self.class.new(driver, native) + self.class.new(driver, native, @browser) end end @@ -121,12 +126,12 @@ module Capybara::Webkit def find_css(selector) invoke("findCssWithin", selector).split(',').map do |native| - self.class.new(driver, native) + self.class.new(driver, native, @browser) end end def invoke(name, *args) - browser.command "Node", name, allow_unattached_nodes?, native, *args + @browser.command "Node", name, allow_unattached_nodes?, native, *args end def allow_unattached_nodes? @@ -138,11 +143,7 @@ module Capybara::Webkit end def attached? - browser.command("Node", "isAttached", native) == "true" - end - - def browser - driver.browser + @browser.command("Node", "isAttached", native) == "true" end def multiple_select? diff --git a/spec/driver_spec.rb b/spec/driver_spec.rb index 618d7f0..846759e 100644 --- a/spec/driver_spec.rb +++ b/spec/driver_spec.rb @@ -1804,7 +1804,7 @@ describe Capybara::Webkit::Driver do context "no response app" do let(:driver) do - driver_for_html(<<-HTML) + driver_for_html(<<-HTML, browser)
@@ -1823,16 +1823,19 @@ describe Capybara::Webkit::Driver do end def make_the_server_come_back - driver.browser.instance_variable_get(:@connection).unstub(:gets) - driver.browser.instance_variable_get(:@connection).unstub(:puts) - driver.browser.instance_variable_get(:@connection).unstub(:print) + connection.unstub(:gets) + connection.unstub(:puts) + connection.unstub(:print) end def make_the_server_go_away - driver.browser.instance_variable_get(:@connection).stub(:gets).and_return(nil) - driver.browser.instance_variable_get(:@connection).stub(:puts) - driver.browser.instance_variable_get(:@connection).stub(:print) + connection.stub(:gets).and_return(nil) + connection.stub(:puts) + connection.stub(:print) end + + let(:browser) { Capybara::Webkit::Browser.new(connection) } + let(:connection) { Capybara::Webkit::Connection.new } end context "custom font app" do @@ -1902,30 +1905,21 @@ describe Capybara::Webkit::Driver do end it "uses a custom cookie" do - driver.browser.set_cookie 'cookie=abc; domain=127.0.0.1; path=/' + driver.set_cookie 'cookie=abc; domain=127.0.0.1; path=/' visit "/" echoed_cookie.should eq "abc" end it "clears cookies" do - driver.browser.clear_cookies + driver.clear_cookies visit "/" echoed_cookie.should eq "" end - it "allows enumeration of cookies" do - cookies = driver.browser.get_cookies - - cookies.size.should eq 1 - - cookie = Hash[cookies[0].split(/\s*;\s*/).map { |x| x.split("=", 2) }] - cookie["cookie"].should eq "abc" - cookie["domain"].should include "127.0.0.1" - cookie["path"].should eq "/" - end - - it "allows reading access to cookies using a nice syntax" do + it "allows reading cookies" do driver.cookies["cookie"].should eq "abc" + driver.cookies.find("cookie").path.should eq "/" + driver.cookies.find("cookie").domain.should include "127.0.0.1" end end @@ -2597,30 +2591,27 @@ CACHE MANIFEST end it "can authenticate a request" do - driver.browser.authenticate('user', 'password') + driver.authenticate('user', 'password') visit("/") driver.html.should include("Basic "+Base64.encode64("user:password").strip) end it "returns 401 for incorrectly authenticated request" do - driver.browser.authenticate('user1', 'password1') - driver.browser.timeout = 2 + driver.authenticate('user1', 'password1') lambda { visit("/") }.should_not raise_error driver.status_code.should eq 401 end it "returns 401 for unauthenticated request" do - driver.browser.timeout = 2 lambda { visit("/") }.should_not raise_error driver.status_code.should eq 401 end it "can be reset with subsequent authenticate call", skip_on_qt4: true do - driver.browser.authenticate('user', 'password') + driver.authenticate('user', 'password') visit("/") driver.html.should include("Basic "+Base64.encode64("user:password").strip) - driver.browser.authenticate('user1', 'password1') - driver.browser.timeout = 2 + driver.authenticate('user1', 'password1') lambda { visit("/") }.should_not raise_error driver.status_code.should eq 401 end @@ -2825,49 +2816,49 @@ CACHE MANIFEST end it "should not raise a timeout error when zero" do - driver.browser.timeout = 0 + driver.timeout = 0 lambda { visit("/") }.should_not raise_error end it "should raise a timeout error" do - driver.browser.timeout = 1 + driver.timeout = 1 lambda { visit("/") }.should raise_error(Timeout::Error, "Request timed out after 1 second(s)") end it "should not raise an error when the timeout is high enough" do - driver.browser.timeout = 10 + driver.timeout = 10 lambda { visit("/") }.should_not raise_error end it "should set the timeout for each request" do - driver.browser.timeout = 10 + driver.timeout = 10 lambda { visit("/") }.should_not raise_error - driver.browser.timeout = 1 + driver.timeout = 1 lambda { visit("/") }.should raise_error(Timeout::Error) end it "should set the timeout for each request" do - driver.browser.timeout = 1 + driver.timeout = 1 lambda { visit("/") }.should raise_error(Timeout::Error) driver.reset! - driver.browser.timeout = 10 + driver.timeout = 10 lambda { visit("/") }.should_not raise_error end it "should raise a timeout on a slow form" do - driver.browser.timeout = 3 + driver.timeout = 3 visit("/") driver.status_code.should eq 200 - driver.browser.timeout = 1 + driver.timeout = 1 driver.find_xpath("//input").first.click lambda { driver.status_code }.should raise_error(Timeout::Error) end it "get timeout" do - driver.browser.timeout = 10 - driver.browser.timeout.should eq 10 - driver.browser.timeout = 3 - driver.browser.timeout.should eq 3 + driver.timeout = 10 + driver.timeout.should eq 10 + driver.timeout = 3 + driver.timeout.should eq 3 end end