From 96314c05c57bee0320d6d19d8d78e0bac13fce6a Mon Sep 17 00:00:00 2001 From: Rob Holland Date: Tue, 23 Feb 2010 17:43:40 +0000 Subject: [PATCH 1/3] Add support for using HTTP verbs directly to the rack driver. --- lib/capybara/driver/base.rb | 16 ++++++++ lib/capybara/driver/rack_test_driver.rb | 29 ++++++++++---- lib/capybara/session.rb | 36 +++++++---------- spec/driver/celerity_driver_spec.rb | 1 + spec/driver/culerity_driver_spec.rb | 3 +- spec/driver/rack_test_driver_spec.rb | 1 + spec/driver/selenium_driver_spec.rb | 1 + spec/drivers_spec.rb | 52 +++++++++++++++++++++++++ 8 files changed, 108 insertions(+), 31 deletions(-) diff --git a/lib/capybara/driver/base.rb b/lib/capybara/driver/base.rb index 6cf8c00f5..3be28d4dc 100644 --- a/lib/capybara/driver/base.rb +++ b/lib/capybara/driver/base.rb @@ -34,4 +34,20 @@ def source def cleanup! end + def get(*args) + raise Capybara::NotSupportedByDriverError + end + + def post(*args) + raise Capybara::NotSupportedByDriverError + end + + def delete(*args) + raise Capybara::NotSupportedByDriverError + end + + def put(*args) + raise Capybara::NotSupportedByDriverError + end + end diff --git a/lib/capybara/driver/rack_test_driver.rb b/lib/capybara/driver/rack_test_driver.rb index 3dc9acc00..008d771bf 100644 --- a/lib/capybara/driver/rack_test_driver.rb +++ b/lib/capybara/driver/rack_test_driver.rb @@ -174,7 +174,7 @@ def merge_param!(params, key, value) end include ::Rack::Test::Methods - attr_reader :app, :html, :body + attr_reader :app alias_method :response, :last_response alias_method :request, :last_request @@ -188,7 +188,6 @@ def visit(path, attributes = {}) return if path.gsub(/^#{current_path}/, '') =~ /^#/ get(path, attributes, env) follow_redirects! - cache_body end def current_url @@ -203,13 +202,29 @@ def submit(method, path, attributes) path = current_path if not path or path.empty? send(method, path, attributes, env) follow_redirects! - cache_body end def find(selector) html.xpath(selector).map { |node| Node.new(self, node) } end - + + ['get', 'post', 'put', 'delete'].each do |method| + class_eval <<-RUBY, __FILE__, __LINE__+1 + def #{method}(*args, &block) + reset_cache + super + end + RUBY + end + + def body + @body ||= response.body + end + + def html + @html ||= Nokogiri::HTML(body) + end + private def current_path @@ -236,9 +251,9 @@ def env env end - def cache_body - @body = response.body - @html = Nokogiri::HTML(body) + def reset_cache + @body = nil + @html = nil end end diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index f98afd39e..f95f7a34d 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -1,10 +1,13 @@ +require 'forwardable' require 'capybara/wait_until' module Capybara class Session + extend Forwardable include Searchable DSL_METHODS = [ + :get, :put, :post, :delete, :all, :attach_file, :body, :check, :choose, :click, :click_button, :click_link, :current_url, :drag, :evaluate_script, :field_labeled, :fill_in, :find, :find_button, :find_by_id, :find_field, :find_link, :has_content?, :has_css?, :has_no_content?, :has_no_css?, :has_no_xpath?, :has_xpath?, :locate, :save_and_open_page, :select, :source, :uncheck, @@ -34,21 +37,16 @@ def driver end end - def cleanup! - driver.cleanup! - end - - def current_url - driver.current_url - end - - def response_headers - driver.response_headers - end - - def visit(path) - driver.visit(path) - end + def_delegator :driver, :cleanup! + def_delegator :driver, :current_url + def_delegator :driver, :response_headers + def_delegator :driver, :visit + def_delegator :driver, :body + def_delegator :driver, :source + def_delegator :driver, :get + def_delegator :driver, :post + def_delegator :driver, :put + def_delegator :driver, :delete def click(locator) msg = "no link or button '#{locator}' found" @@ -106,14 +104,6 @@ def attach_file(locator, path) locate(:xpath, XPath.file_field(locator), msg).set(path) end - def body - driver.body - end - - def source - driver.source - end - def within(kind, scope=nil) kind, scope = Capybara.default_selector, kind unless scope scope = XPath.from_css(scope) if kind == :css diff --git a/spec/driver/celerity_driver_spec.rb b/spec/driver/celerity_driver_spec.rb index bd31440a3..fa3ebfd3a 100644 --- a/spec/driver/celerity_driver_spec.rb +++ b/spec/driver/celerity_driver_spec.rb @@ -10,6 +10,7 @@ it_should_behave_like "driver with javascript support" it_should_behave_like "driver with header support" it_should_behave_like "driver with node path support" + it_should_behave_like "driver without direct HTTP support" end else diff --git a/spec/driver/culerity_driver_spec.rb b/spec/driver/culerity_driver_spec.rb index ec6a4e3dc..4753d48fd 100644 --- a/spec/driver/culerity_driver_spec.rb +++ b/spec/driver/culerity_driver_spec.rb @@ -8,6 +8,7 @@ it_should_behave_like "driver" it_should_behave_like "driver with javascript support" it_should_behave_like "driver with header support" - it_should_behave_like "driver with node path support" + it_should_behave_like "driver with node path support" + it_should_behave_like "driver without direct HTTP support" end diff --git a/spec/driver/rack_test_driver_spec.rb b/spec/driver/rack_test_driver_spec.rb index 7b2c8d673..9ce4b6925 100644 --- a/spec/driver/rack_test_driver_spec.rb +++ b/spec/driver/rack_test_driver_spec.rb @@ -8,5 +8,6 @@ it_should_behave_like "driver" it_should_behave_like "driver with header support" it_should_behave_like "driver with node path support" + it_should_behave_like "driver with direct HTTP support" end diff --git a/spec/driver/selenium_driver_spec.rb b/spec/driver/selenium_driver_spec.rb index a71047e0d..5bc8407b3 100644 --- a/spec/driver/selenium_driver_spec.rb +++ b/spec/driver/selenium_driver_spec.rb @@ -8,5 +8,6 @@ it_should_behave_like "driver" it_should_behave_like "driver with javascript support" it_should_behave_like "driver without node path support" + it_should_behave_like "driver without direct HTTP support" end diff --git a/spec/drivers_spec.rb b/spec/drivers_spec.rb index b5fbb892f..ee20a0952 100644 --- a/spec/drivers_spec.rb +++ b/spec/drivers_spec.rb @@ -137,3 +137,55 @@ end end + +shared_examples_for "driver with direct HTTP support" do + describe "direct HTTP calls" do + it "should create a response for a GET" do + @driver.get('/tables') + @driver.body.should_not be_nil + end + + it "should create a response for a PUT" do + @driver.put('/tables') + @driver.body.should_not be_nil + end + + it "should create a response for a POST" do + @driver.post('/table') + @driver.body.should_not be_nil + end + + it "should create a response for a DELETE" do + @driver.delete('/table') + @driver.body.should_not be_nil + end + end +end + +shared_examples_for "driver without direct HTTP support" do + describe "direct HTTP calls" do + it "should get NotSupportedByDriverError for a GET" do + running do + @driver.get('/tables') + end.should raise_error(Capybara::NotSupportedByDriverError) + end + + it "should get NotSupportedByDriverError for a PUT" do + running do + @driver.put('/tables') + end.should raise_error(Capybara::NotSupportedByDriverError) + end + + it "should get NotSupportedByDriverError for a POST" do + running do + @driver.post('/tables') + end.should raise_error(Capybara::NotSupportedByDriverError) + end + + it "should get NotSupportedByDriverError for a DELETE" do + running do + @driver.delete('/tables') + end.should raise_error(Capybara::NotSupportedByDriverError) + end + end +end From b204cc8ac6c91e96d30677c00933ff35df69657a Mon Sep 17 00:00:00 2001 From: Rob Holland Date: Wed, 17 Mar 2010 13:10:19 +0000 Subject: [PATCH 2/3] Remove direct HTTP support. --- lib/capybara/driver/base.rb | 16 -------- lib/capybara/driver/rack_test_driver.rb | 9 ----- lib/capybara/session.rb | 5 --- spec/driver/celerity_driver_spec.rb | 1 - spec/driver/culerity_driver_spec.rb | 1 - spec/driver/rack_test_driver_spec.rb | 1 - spec/driver/selenium_driver_spec.rb | 1 - spec/drivers_spec.rb | 52 ------------------------- 8 files changed, 86 deletions(-) diff --git a/lib/capybara/driver/base.rb b/lib/capybara/driver/base.rb index 3be28d4dc..6cf8c00f5 100644 --- a/lib/capybara/driver/base.rb +++ b/lib/capybara/driver/base.rb @@ -34,20 +34,4 @@ def source def cleanup! end - def get(*args) - raise Capybara::NotSupportedByDriverError - end - - def post(*args) - raise Capybara::NotSupportedByDriverError - end - - def delete(*args) - raise Capybara::NotSupportedByDriverError - end - - def put(*args) - raise Capybara::NotSupportedByDriverError - end - end diff --git a/lib/capybara/driver/rack_test_driver.rb b/lib/capybara/driver/rack_test_driver.rb index 69ae76a47..0c689ab56 100644 --- a/lib/capybara/driver/rack_test_driver.rb +++ b/lib/capybara/driver/rack_test_driver.rb @@ -212,15 +212,6 @@ def find(selector) html.xpath(selector).map { |node| Node.new(self, node) } end - ['get', 'post', 'put', 'delete'].each do |method| - class_eval <<-RUBY, __FILE__, __LINE__+1 - def #{method}(*args, &block) - reset_cache - super - end - RUBY - end - def body @body ||= response.body end diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index d4fb0d113..37ba45f63 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -7,7 +7,6 @@ class Session include Searchable DSL_METHODS = [ - :get, :put, :post, :delete, :all, :attach_file, :body, :check, :choose, :click, :click_button, :click_link, :current_url, :drag, :evaluate_script, :field_labeled, :fill_in, :find, :find_button, :find_by_id, :find_field, :find_link, :has_content?, :has_css?, :has_no_content?, :has_no_css?, :has_no_xpath?, :has_xpath?, :locate, :save_and_open_page, :select, :source, :uncheck, @@ -44,10 +43,6 @@ def driver def_delegator :driver, :visit def_delegator :driver, :body def_delegator :driver, :source - def_delegator :driver, :get - def_delegator :driver, :post - def_delegator :driver, :put - def_delegator :driver, :delete def click(locator) msg = "no link or button '#{locator}' found" diff --git a/spec/driver/celerity_driver_spec.rb b/spec/driver/celerity_driver_spec.rb index bf9346623..3ed2e096e 100644 --- a/spec/driver/celerity_driver_spec.rb +++ b/spec/driver/celerity_driver_spec.rb @@ -10,7 +10,6 @@ it_should_behave_like "driver with javascript support" it_should_behave_like "driver with header support" it_should_behave_like "driver with node path support" - it_should_behave_like "driver without direct HTTP support" end else diff --git a/spec/driver/culerity_driver_spec.rb b/spec/driver/culerity_driver_spec.rb index 7c88dd5e0..093b931b4 100644 --- a/spec/driver/culerity_driver_spec.rb +++ b/spec/driver/culerity_driver_spec.rb @@ -9,5 +9,4 @@ it_should_behave_like "driver with javascript support" it_should_behave_like "driver with header support" it_should_behave_like "driver with node path support" - it_should_behave_like "driver without direct HTTP support" end diff --git a/spec/driver/rack_test_driver_spec.rb b/spec/driver/rack_test_driver_spec.rb index 827783ab6..0ca7251d3 100644 --- a/spec/driver/rack_test_driver_spec.rb +++ b/spec/driver/rack_test_driver_spec.rb @@ -8,5 +8,4 @@ it_should_behave_like "driver" it_should_behave_like "driver with header support" it_should_behave_like "driver with node path support" - it_should_behave_like "driver with direct HTTP support" end diff --git a/spec/driver/selenium_driver_spec.rb b/spec/driver/selenium_driver_spec.rb index 9afaf1d12..d709af7ee 100644 --- a/spec/driver/selenium_driver_spec.rb +++ b/spec/driver/selenium_driver_spec.rb @@ -8,5 +8,4 @@ it_should_behave_like "driver" it_should_behave_like "driver with javascript support" it_should_behave_like "driver without node path support" - it_should_behave_like "driver without direct HTTP support" end diff --git a/spec/drivers_spec.rb b/spec/drivers_spec.rb index 57d72e3f5..341ee2a78 100644 --- a/spec/drivers_spec.rb +++ b/spec/drivers_spec.rb @@ -156,55 +156,3 @@ end end - -shared_examples_for "driver with direct HTTP support" do - describe "direct HTTP calls" do - it "should create a response for a GET" do - @driver.get('/tables') - @driver.body.should_not be_nil - end - - it "should create a response for a PUT" do - @driver.put('/tables') - @driver.body.should_not be_nil - end - - it "should create a response for a POST" do - @driver.post('/table') - @driver.body.should_not be_nil - end - - it "should create a response for a DELETE" do - @driver.delete('/table') - @driver.body.should_not be_nil - end - end -end - -shared_examples_for "driver without direct HTTP support" do - describe "direct HTTP calls" do - it "should get NotSupportedByDriverError for a GET" do - running do - @driver.get('/tables') - end.should raise_error(Capybara::NotSupportedByDriverError) - end - - it "should get NotSupportedByDriverError for a PUT" do - running do - @driver.put('/tables') - end.should raise_error(Capybara::NotSupportedByDriverError) - end - - it "should get NotSupportedByDriverError for a POST" do - running do - @driver.post('/tables') - end.should raise_error(Capybara::NotSupportedByDriverError) - end - - it "should get NotSupportedByDriverError for a DELETE" do - running do - @driver.delete('/tables') - end.should raise_error(Capybara::NotSupportedByDriverError) - end - end -end From 64d115a6f20f3d913daa3b87739671807e412d08 Mon Sep 17 00:00:00 2001 From: Rob Holland Date: Wed, 17 Mar 2010 13:19:45 +0000 Subject: [PATCH 3/3] Revert merge remnants. --- spec/driver/culerity_driver_spec.rb | 2 +- spec/driver/rack_test_driver_spec.rb | 2 +- spec/driver/selenium_driver_spec.rb | 2 +- spec/drivers_spec.rb | 31 ---------------------------- 4 files changed, 3 insertions(+), 34 deletions(-) diff --git a/spec/driver/culerity_driver_spec.rb b/spec/driver/culerity_driver_spec.rb index 093b931b4..c01ab97c3 100644 --- a/spec/driver/culerity_driver_spec.rb +++ b/spec/driver/culerity_driver_spec.rb @@ -8,5 +8,5 @@ it_should_behave_like "driver" it_should_behave_like "driver with javascript support" it_should_behave_like "driver with header support" - it_should_behave_like "driver with node path support" + end diff --git a/spec/driver/rack_test_driver_spec.rb b/spec/driver/rack_test_driver_spec.rb index 0ca7251d3..e4b03d5e1 100644 --- a/spec/driver/rack_test_driver_spec.rb +++ b/spec/driver/rack_test_driver_spec.rb @@ -7,5 +7,5 @@ it_should_behave_like "driver" it_should_behave_like "driver with header support" - it_should_behave_like "driver with node path support" + end diff --git a/spec/driver/selenium_driver_spec.rb b/spec/driver/selenium_driver_spec.rb index d709af7ee..c2b2b4c11 100644 --- a/spec/driver/selenium_driver_spec.rb +++ b/spec/driver/selenium_driver_spec.rb @@ -7,5 +7,5 @@ it_should_behave_like "driver" it_should_behave_like "driver with javascript support" - it_should_behave_like "driver without node path support" + end diff --git a/spec/drivers_spec.rb b/spec/drivers_spec.rb index 341ee2a78..c16146868 100644 --- a/spec/drivers_spec.rb +++ b/spec/drivers_spec.rb @@ -125,34 +125,3 @@ @driver.response_headers['Content-Type'].should == 'text/html' end end - -shared_examples_for "driver with node path support" do - describe "node relative searching" do - before do - @driver.visit('/tables') - @node = @driver.find('//body').first - end - - it "should be able to navigate/search child nodes" do - @node.all('//table').size.should == 3 - @node.find('//form').all('//table').size.should == 1 - @node.find('//form').find('//table//caption').text.should == 'Agent' - end - end -end - -shared_examples_for "driver without node path support" do - describe "node relative searching" do - before do - @driver.visit('/tables') - @node = @driver.find('//body').first - end - - it "should get NotSupportedByDriverError" do - running do - @node.all('//form') - end.should raise_error(Capybara::NotSupportedByDriverError) - end - - end -end