1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

removed whitespace and added dsl shortcut to evaluate_script method

This commit is contained in:
Dennis Rogenius 2009-12-12 13:33:00 +01:00
parent 7867084063
commit 525ba2d654
8 changed files with 92 additions and 56 deletions

View file

@ -6,6 +6,7 @@ module Capybara
class CapybaraError < StandardError; end
class DriverNotFoundError < CapybaraError; end
class ElementNotFound < CapybaraError; end
class NotSupportedByDriverError < CapybaraError; end
class << self
attr_accessor :debug, :asset_root
@ -14,13 +15,13 @@ module Capybara
def default_selector
@default_selector ||= :xpath
end
def log(message)
puts "[capybara] #{message}" if debug
true
end
end
autoload :Server, 'capybara/server'
autoload :Session, 'capybara/session'
autoload :Node, 'capybara/node'

View file

@ -12,7 +12,7 @@ module Capybara
@current_driver || default_driver
end
alias_method :mode, :current_driver
def javascript_driver
@javascript_driver || :selenium
end
@ -24,11 +24,11 @@ module Capybara
def current_session
session_pool["#{current_driver}#{app.object_id}"] ||= Capybara::Session.new(current_driver, app)
end
def current_session?
session_pool.has_key?("#{current_driver}#{app.object_id}")
end
def reset_sessions!
@session_pool = nil
end
@ -50,7 +50,7 @@ module Capybara
:visit, :body, :click_link, :click_button, :fill_in, :choose, :has_xpath?, :has_css?,
:check, :uncheck, :attach_file, :select, :has_content?, :within, :within_fieldset,
:within_table, :save_and_open_page, :find, :find_field, :find_link, :find_button,
:field_labeled, :all
:field_labeled, :all, :evaluate_script
]
SESSION_METHODS.each do |method|
class_eval <<-RUBY, __FILE__, __LINE__+1

View file

@ -138,7 +138,7 @@ module Capybara
locator = current_scope.to_s + locator
driver.find(locator)
end
def find(locator)
all(locator).first
end
@ -158,6 +158,14 @@ module Capybara
button || find("//button[@id='#{locator}' or @value='#{locator}' or contains(.,'#{locator}')]")
end
def evaluate_script(script)
begin
driver.evaluate_script(script)
rescue NoMethodError
raise NotSupportedByDriverError
end
end
private
def css_to_xpath(css)

View file

@ -36,7 +36,7 @@ describe Capybara do
Capybara.current_driver.should == :culerity
end
end
describe '#javascript_driver' do
it "should default to selenium" do
Capybara.javascript_driver.should == :selenium
@ -98,7 +98,7 @@ describe Capybara do
Capybara.current_session.app.should == Capybara.app
end
end
describe '.reset_sessions!' do
it "should clear any persisted sessions" do
object_id = Capybara.current_session.object_id
@ -114,6 +114,7 @@ describe Capybara do
end
it_should_behave_like "session"
it_should_behave_like "session without javascript driver"
it "should be possible to include it in another class" do
klass = Class.new do
@ -124,7 +125,7 @@ describe Capybara do
foo.click_link('ullamco')
foo.body.should include('Another World')
end
it "should provide a 'page' shortcut for more expressive tests" do
klass = Class.new do
include Capybara

View file

@ -5,19 +5,20 @@ describe Capybara::Session do
before do
@session = Capybara::Session.new(:culerity, TestApp)
end
describe '#driver' do
it "should be a rack test driver" do
@session.driver.should be_an_instance_of(Capybara::Driver::Culerity)
end
end
describe '#mode' do
it "should remember the mode" do
@session.mode.should == :culerity
end
end
it_should_behave_like "session"
it_should_behave_like "session with javascript driver"
end
end

View file

@ -5,19 +5,20 @@ describe Capybara::Session do
before do
@session = Capybara::Session.new(:rack_test, TestApp)
end
describe '#driver' do
it "should be a rack test driver" do
@session.driver.should be_an_instance_of(Capybara::Driver::RackTest)
end
end
describe '#mode' do
it "should remember the mode" do
@session.mode.should == :rack_test
end
end
it_should_behave_like "session"
it_should_behave_like "session without javascript driver"
end
end

View file

@ -19,5 +19,6 @@ describe Capybara::Session do
end
it_should_behave_like "session"
it_should_behave_like "session with javascript driver"
end
end

View file

@ -185,7 +185,7 @@ shared_examples_for "session" do
end.should raise_error(Capybara::ElementNotFound)
end
end
it "should serialize and send GET forms" do
@session.visit('/form')
@session.click_button('med')
@ -272,7 +272,7 @@ shared_examples_for "session" do
@session.click_button('awesome')
extract_results(@session)['gender'].should == 'both'
end
context "with a locator that doesn't exist" do
it "should raise an error" do
running { @session.choose('does not exist') }.should raise_error(Capybara::ElementNotFound)
@ -296,7 +296,7 @@ shared_examples_for "session" do
@session.click_button('awesome')
extract_results(@session)['pets'].should include('dog', 'cat', 'hamster')
end
context "with a locator that doesn't exist" do
it "should raise an error" do
running { @session.check('does not exist') }.should raise_error(Capybara::ElementNotFound)
@ -322,7 +322,7 @@ shared_examples_for "session" do
extract_results(@session)['pets'].should include('dog')
extract_results(@session)['pets'].should_not include('hamster')
end
context "with a locator that doesn't exist" do
it "should raise an error" do
running { @session.uncheck('does not exist') }.should raise_error(Capybara::ElementNotFound)
@ -346,7 +346,7 @@ shared_examples_for "session" do
@session.click_button('awesome')
extract_results(@session)['locale'].should == 'fi'
end
context "with a locator that doesn't exist" do
it "should raise an error" do
running { @session.select('foo', :from => 'does not exist') }.should raise_error(Capybara::ElementNotFound)
@ -400,7 +400,7 @@ shared_examples_for "session" do
@session.should_not have_xpath("//p//a[@id='doesnotexist']")
@session.should_not have_xpath("//p[contains(.,'thisstringisnotonpage')]")
end
it "should respect scopes" do
@session.within "//p[@id='first']" do
@session.should have_xpath("//a[@id='foo']")
@ -426,20 +426,20 @@ shared_examples_for "session" do
@session.should_not have_xpath("//p//a[@id='doesnotexist']", :count => 1)
end
end
context "with text" do
it "should discard all matches where the given string is not contained" do
@session.should have_xpath("//p//a", :text => "Redirect", :count => 1)
@session.should_not have_xpath("//p", :text => "Doesnotexist")
end
it "should discard all matches where the given regexp is not matched" do
@session.should have_xpath("//p//a", :text => /re[dab]i/i, :count => 1)
@session.should_not have_xpath("//p//a", :text => /Red$/)
end
end
end
describe '#has_css?' do
before do
@session.visit('/with_html')
@ -455,7 +455,7 @@ shared_examples_for "session" do
@session.should_not have_css("p a#doesnotexist")
@session.should_not have_css("p.nosuchclass")
end
it "should respect scopes" do
@session.within "//p[@id='first']" do
@session.should have_css("a#foo")
@ -479,13 +479,13 @@ shared_examples_for "session" do
@session.should_not have_css("p a.doesnotexist", :count => 1)
end
end
context "with text" do
it "should discard all matches where the given string is not contained" do
@session.should have_css("p a", :text => "Redirect", :count => 1)
@session.should_not have_css("p a", :text => "Doesnotexist")
end
it "should discard all matches where the given regexp is not matched" do
@session.should have_css("p a", :text => /re[dab]i/i, :count => 1)
@session.should_not have_css("p a", :text => /Red$/)
@ -533,14 +533,14 @@ shared_examples_for "session" do
@session.click_button('Upload')
end
end
context "with a locator that doesn't exist" do
it "should raise an error" do
running { @session.attach_file('does not exist', 'foo.txt') }.should raise_error(Capybara::ElementNotFound)
end
end
end
describe '#find_field' do
before do
@session.visit('/form')
@ -551,22 +551,22 @@ shared_examples_for "session" do
@session.find_field('form_description').text.should == 'Descriptive text goes here'
@session.find_field('Region')[:name].should == 'form[region]'
end
it "should raise an error if the field doesn't exist" do
@session.find_field('Does not exist').should be_nil
end
it "should find only given kind of field" do
@session.find_field('form_description', :text_field, :text_area).text.should == 'Descriptive text goes here'
@session.find_field('form_description', :password_field).should be_nil
end
it "should be aliased as 'field_labeled' for webrat compatibility" do
@session.field_labeled('Dog').value.should == 'dog'
@session.field_labeled('Does not exist').should be_nil
end
end
describe '#find_link' do
before do
@session.visit('/with_html')
@ -576,12 +576,12 @@ shared_examples_for "session" do
@session.find_link('foo').text.should == "ullamco"
@session.find_link('labore')[:href].should == "/with_simple_html"
end
it "should return nil if the field doesn't exist" do
@session.find_link('Does not exist').should be_nil
end
end
describe '#find_button' do
before do
@session.visit('/form')
@ -591,7 +591,7 @@ shared_examples_for "session" do
@session.find_button('med')[:id].should == "mediocre"
@session.find_button('crap321').value.should == "crappy"
end
it "should return nil if the field doesn't exist" do
@session.find_button('Does not exist').should be_nil
end
@ -607,11 +607,11 @@ shared_examples_for "session" do
@session.all('//h1').first.text.should == 'This is a test'
@session.all("//input[@id='test_field']").first[:value].should == 'monkey'
end
it "should return an empty array when nothing was found" do
@session.all('//div').should be_empty
end
context "within a scope" do
before do
@session.visit('/with_scope')
@ -620,7 +620,7 @@ shared_examples_for "session" do
it "should find any element using the given locator" do
@session.within(:xpath, "//div[@id='for_bar']") do
@session.all('//li').should have(2).elements
end
end
end
end
end
@ -634,11 +634,11 @@ shared_examples_for "session" do
@session.find('//h1').text.should == 'This is a test'
@session.find("//input[@id='test_field']")[:value].should == 'monkey'
end
it "should return nil when nothing was found" do
@session.find('//div').should be_nil
end
context "within a scope" do
before do
@session.visit('/with_scope')
@ -647,7 +647,7 @@ shared_examples_for "session" do
it "should find the first element using the given locator" do
@session.within(:xpath, "//div[@id='for_bar']") do
@session.find('//li').text.should =~ /With Simple HTML/
end
end
end
end
end
@ -656,7 +656,7 @@ shared_examples_for "session" do
before do
@session.visit('/with_scope')
end
context "with CSS selector" do
it "should click links in the given scope" do
@session.within(:css, "ul li[contains('With Simple HTML')]") do
@ -665,7 +665,7 @@ shared_examples_for "session" do
@session.body.should include('<h1>Bar</h1>')
end
end
context "with XPath selector" do
it "should click links in the given scope" do
@session.within(:xpath, "//li[contains(.,'With Simple HTML')]") do
@ -674,7 +674,7 @@ shared_examples_for "session" do
@session.body.should include('<h1>Bar</h1>')
end
end
context "with the default selector" do
it "should use XPath" do
@session.within("//li[contains(., 'With Simple HTML')]") do
@ -683,12 +683,12 @@ shared_examples_for "session" do
@session.body.should include('<h1>Bar</h1>')
end
end
context "with the default selector set to CSS" do
after do
Capybara.default_selector = :xpath
end
it "should use CSS" do
Capybara.default_selector = :css
@session.within("ul li[contains('With Simple HTML')]") do
@ -697,7 +697,7 @@ shared_examples_for "session" do
@session.body.should include('<h1>Bar</h1>')
end
end
context "with click_link" do
it "should click links in the given scope" do
@session.within("//li[contains(.,'With Simple HTML')]") do
@ -725,7 +725,7 @@ shared_examples_for "session" do
@session.body.should include('Hello world')
end
end
it "should raise an error if the scope is not found on the page" do
running {
@session.within("//div[@id='doesnotexist']") do
@ -749,12 +749,12 @@ shared_examples_for "session" do
end
end
end
describe '#within_fieldset' do
before do
@session.visit('/fieldsets')
end
it "should restrict scope to a fieldset given by id" do
@session.within_fieldset("villain_fieldset") do
@session.fill_in("Name", :with => 'Goldfinger')
@ -762,7 +762,7 @@ shared_examples_for "session" do
end
extract_results(@session)['villain_name'].should == 'Goldfinger'
end
it "should restrict scope to a fieldset given by legend" do
@session.within_fieldset("Villain") do
@session.fill_in("Name", :with => 'Goldfinger')
@ -771,12 +771,12 @@ shared_examples_for "session" do
extract_results(@session)['villain_name'].should == 'Goldfinger'
end
end
describe '#within_table' do
before do
@session.visit('/tables')
end
it "should restrict scope to a fieldset given by id" do
@session.within_table("girl_table") do
@session.fill_in("Name", :with => 'Christmas')
@ -784,7 +784,7 @@ shared_examples_for "session" do
end
extract_results(@session)['girl_name'].should == 'Christmas'
end
it "should restrict scope to a fieldset given by legend" do
@session.within_table("Villain") do
@session.fill_in("Name", :with => 'Quantum')
@ -793,6 +793,28 @@ shared_examples_for "session" do
extract_results(@session)['villain_name'].should == 'Quantum'
end
end
end
shared_examples_for "session with javascript driver" do
describe "#evaluate_script" do
before{ @session.visit('/with_js') }
it "should return the evaluated script" do
@session.evaluate_script("1+3").should == 4
end
end
end
shared_examples_for "session without javascript driver" do
describe "#evaluate_script" do
before{ @session.visit('/with_js') }
it "should raise an error" do
running {
@session.evaluate_script("1+5")
}.should raise_error(Capybara::NotSupportedByDriverError)
end
end
end
describe Capybara::Session do
@ -804,3 +826,4 @@ describe Capybara::Session do
end
end
end