mirror of
https://github.com/teamcapybara/capybara.git
synced 2022-11-09 12:08:07 -05:00
Add cleanup method to clear cookies for Celerity/Culerity and RackTest, with tests for Selenium included
This commit is contained in:
parent
a94f9949d5
commit
5ad348d776
10 changed files with 59 additions and 27 deletions
|
@ -72,7 +72,7 @@ class Capybara::Driver::Celerity < Capybara::Driver::Base
|
|||
def path
|
||||
node.xpath
|
||||
end
|
||||
|
||||
|
||||
def trigger(event)
|
||||
node.fire_event(event.to_s)
|
||||
end
|
||||
|
@ -102,7 +102,7 @@ class Capybara::Driver::Celerity < Capybara::Driver::Base
|
|||
def current_url
|
||||
browser.url
|
||||
end
|
||||
|
||||
|
||||
def source
|
||||
browser.html
|
||||
end
|
||||
|
@ -114,7 +114,7 @@ class Capybara::Driver::Celerity < Capybara::Driver::Base
|
|||
def response_headers
|
||||
browser.response_headers
|
||||
end
|
||||
|
||||
|
||||
def status_code
|
||||
browser.status_code
|
||||
end
|
||||
|
@ -138,6 +138,10 @@ class Capybara::Driver::Celerity < Capybara::Driver::Base
|
|||
@_browser
|
||||
end
|
||||
|
||||
def cleanup!
|
||||
browser.clear_cookies
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def url(path)
|
||||
|
|
|
@ -204,7 +204,7 @@ class Capybara::Driver::RackTest < Capybara::Driver::Base
|
|||
def response_headers
|
||||
response.headers
|
||||
end
|
||||
|
||||
|
||||
def status_code
|
||||
response.status
|
||||
end
|
||||
|
@ -218,21 +218,25 @@ class Capybara::Driver::RackTest < Capybara::Driver::Base
|
|||
def find(selector)
|
||||
html.xpath(selector).map { |node| Node.new(self, node) }
|
||||
end
|
||||
|
||||
|
||||
def body
|
||||
@body ||= response.body
|
||||
end
|
||||
|
||||
|
||||
def html
|
||||
@html ||= Nokogiri::HTML(body)
|
||||
end
|
||||
alias_method :source, :body
|
||||
|
||||
def cleanup!
|
||||
clear_cookies
|
||||
end
|
||||
|
||||
def get(*args, &block); reset_cache; super; end
|
||||
def post(*args, &block); reset_cache; super; end
|
||||
def put(*args, &block); reset_cache; super; end
|
||||
def delete(*args, &block); reset_cache; super; end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def reset_cache
|
||||
|
@ -268,9 +272,4 @@ private
|
|||
env
|
||||
end
|
||||
|
||||
def reset_cache
|
||||
@body = nil
|
||||
@html = nil
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
require 'capybara/spec/test_app'
|
||||
|
||||
Dir[File.dirname(__FILE__)+'/driver/*'].each { |group|
|
||||
Dir[File.dirname(__FILE__)+'/driver/*'].each { |group|
|
||||
require group
|
||||
}
|
||||
|
||||
|
@ -13,7 +13,7 @@ shared_examples_for 'driver' do
|
|||
@driver.visit('/foo')
|
||||
@driver.body.should include('Another World')
|
||||
end
|
||||
|
||||
|
||||
it "should show the correct URL" do
|
||||
@driver.visit('/foo')
|
||||
@driver.current_url.should include('/foo')
|
||||
|
@ -66,10 +66,10 @@ shared_examples_for 'driver' do
|
|||
@driver.find('//a')[1].tag_name.should == 'a'
|
||||
@driver.find('//p')[1].tag_name.should == 'p'
|
||||
end
|
||||
|
||||
|
||||
it "should extract node visibility" do
|
||||
@driver.find('//a')[0].should be_visible
|
||||
|
||||
|
||||
@driver.find('//div[@id="hidden"]')[0].should_not be_visible
|
||||
@driver.find('//div[@id="hidden_via_ancestor"]')[0].should_not be_visible
|
||||
end
|
||||
|
@ -81,7 +81,7 @@ shared_examples_for 'driver' do
|
|||
@driver.visit('/tables')
|
||||
@node = @driver.find('//body').first
|
||||
end
|
||||
|
||||
|
||||
it "should be able to navigate/search child node" do
|
||||
@node.all('//table').size.should == 5
|
||||
@node.find('//form').all('.//table').size.should == 1
|
||||
|
@ -95,7 +95,6 @@ shared_examples_for 'driver' do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
shared_examples_for "driver with javascript support" do
|
||||
|
@ -176,3 +175,22 @@ shared_examples_for "driver with frame support" do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
shared_examples_for "driver with cookies support" do
|
||||
describe "#cleanup" do
|
||||
it "should set and clean cookies" do
|
||||
@driver.visit('/get_cookie')
|
||||
@driver.body.should_not include('test_cookie')
|
||||
|
||||
@driver.visit('/set_cookie')
|
||||
@driver.body.should include('Cookie set to test_cookie')
|
||||
|
||||
@driver.visit('/get_cookie')
|
||||
@driver.body.should include('test_cookie')
|
||||
|
||||
@driver.cleanup!
|
||||
@driver.visit('/get_cookie')
|
||||
@driver.body.should_not include('test_cookie')
|
||||
end
|
||||
end
|
||||
end
|
|
@ -13,7 +13,7 @@ class TestApp < Sinatra::Base
|
|||
get '/foo' do
|
||||
'Another World'
|
||||
end
|
||||
|
||||
|
||||
get '/redirect' do
|
||||
redirect '/redirect_again'
|
||||
end
|
||||
|
@ -33,7 +33,7 @@ class TestApp < Sinatra::Base
|
|||
get '/form/get' do
|
||||
'<pre id="results">' + params[:form].to_yaml + '</pre>'
|
||||
end
|
||||
|
||||
|
||||
get '/favicon.ico' do
|
||||
nil
|
||||
end
|
||||
|
@ -50,6 +50,16 @@ class TestApp < Sinatra::Base
|
|||
redirect back
|
||||
end
|
||||
|
||||
get '/set_cookie' do
|
||||
cookie_value = 'test_cookie'
|
||||
response.set_cookie('capybara', cookie_value)
|
||||
"Cookie set to #{cookie_value}"
|
||||
end
|
||||
|
||||
get '/get_cookie' do
|
||||
request.cookies['capybara']
|
||||
end
|
||||
|
||||
get '/:view' do |view|
|
||||
erb view.to_sym
|
||||
end
|
||||
|
|
|
@ -10,7 +10,7 @@ if RUBY_PLATFORM =~ /java/
|
|||
it_should_behave_like "driver with javascript support"
|
||||
it_should_behave_like "driver with header support"
|
||||
it_should_behave_like "driver with status code support"
|
||||
|
||||
it_should_behave_like "driver with cookies support"
|
||||
end
|
||||
else
|
||||
puts "#{File.basename(__FILE__)} requires JRuby; skipping.."
|
||||
|
|
|
@ -4,10 +4,10 @@ describe Capybara::Driver::Culerity do
|
|||
before(:all) do
|
||||
@driver = Capybara::Driver::Culerity.new(TestApp)
|
||||
end
|
||||
|
||||
|
||||
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 status code support"
|
||||
|
||||
it_should_behave_like "driver with cookies support"
|
||||
end
|
||||
|
|
|
@ -10,9 +10,9 @@ describe Capybara::Driver::RackTest do
|
|||
Capybara::Driver::RackTest.new(nil)
|
||||
end.should raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
|
||||
it_should_behave_like "driver"
|
||||
it_should_behave_like "driver with header support"
|
||||
it_should_behave_like "driver with status code support"
|
||||
|
||||
it_should_behave_like "driver with cookies support"
|
||||
end
|
||||
|
|
|
@ -9,4 +9,5 @@ describe Capybara::Driver::Selenium do
|
|||
it_should_behave_like "driver with javascript support"
|
||||
it_should_behave_like "driver with frame support"
|
||||
it_should_behave_like "driver without status code support"
|
||||
it_should_behave_like "driver with cookies support"
|
||||
end
|
||||
|
|
|
@ -7,7 +7,7 @@ if RUBY_PLATFORM =~ /java/
|
|||
end
|
||||
|
||||
describe '#driver' do
|
||||
it "should be a rack test driver" do
|
||||
it "should be a celerity driver" do
|
||||
@session.driver.should be_an_instance_of(Capybara::Driver::Celerity)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -7,7 +7,7 @@ describe Capybara::Session do
|
|||
end
|
||||
|
||||
describe '#driver' do
|
||||
it "should be a rack test driver" do
|
||||
it "should be a culerity driver" do
|
||||
@session.driver.should be_an_instance_of(Capybara::Driver::Culerity)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue