Added within_fieldset method

This commit is contained in:
Jonas Nicklas 2009-11-24 21:32:25 +01:00
parent 51a5a2efa2
commit 5a89fafcdf
5 changed files with 76 additions and 30 deletions

View File

@ -44,8 +44,8 @@ module Capybara
SESSION_METHODS = [
:visit, :body, :click_link, :click_button, :fill_in, :choose, :has_xpath?, :has_css?,
:check, :uncheck, :attach_file, :select, :has_content?, :within, :save_and_open_page,
:find_field, :find_link, :find_button, :field_labeled
:check, :uncheck, :attach_file, :select, :has_content?, :within, :within_fieldset,
:save_and_open_page, :find_field, :find_link, :find_button, :field_labeled
]
SESSION_METHODS.each do |method|
class_eval <<-RUBY, __FILE__, __LINE__+1

View File

@ -100,6 +100,12 @@ class Capybara::Session
scopes.pop
end
def within_fieldset(locator)
within "//fieldset[@id='#{locator}' or contains(legend,'#{locator}')]" do
yield
end
end
def save_and_open_page
require 'capybara/save_and_open_page'
Capybara::SaveAndOpenPage.save_and_open_page(body)

View File

@ -594,6 +594,29 @@ 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')
@session.click_button("Create")
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')
@session.click_button("Create")
end
extract_results(@session)['villain_name'].should == 'Goldfinger'
end
end
end
describe Capybara::Session do

View File

@ -12,31 +12,7 @@ class TestApp < Sinatra::Base
get '/foo' do
'Another World'
end
get '/with_html' do
erb :with_html
end
get '/with_js' do
erb :with_js
end
get '/with_simple_html' do
erb :with_simple_html
end
get '/with_scope' do
erb :with_scope
end
get '/form' do
erb :form
end
post '/redirect' do
redirect '/redirect_again'
end
get '/redirect' do
redirect '/redirect_again'
end
@ -48,12 +24,24 @@ class TestApp < Sinatra::Base
get '/landed' do
"You landed"
end
post '/form' do
get '/form/get' do
'<pre id="results">' + params[:form].to_yaml + '</pre>'
end
get '/form/get' do
get '/favicon.ico' do
nil
end
get '/:view' do |view|
erb view.to_sym
end
post '/redirect' do
redirect '/redirect_again'
end
post '/form' do
'<pre id="results">' + params[:form].to_yaml + '</pre>'
end

29
spec/views/fieldsets.erb Normal file
View File

@ -0,0 +1,29 @@
<form action="/form" method="post">
<fieldset id="agent_fieldset">
<legend>Agent</legend>
<p>
<label for="form_agent_name">Name</label>
<input type="text" name="form[agent_name]" value="James" id="form_agent_name"/>
</p>
<p>
<input type="submit" value="Create"/>
</p>
</fieldset>
</form>
<form action="/form" method="post">
<fieldset id="villain_fieldset">
<legend>Villain</legend>
<p>
<label for="form_villain_name">Name</label>
<input type="text" name="form[villain_name]" value="Ernst" id="form_villain_name"/>
</p>
<p>
<input type="submit" value="Create"/>
</p>
</fieldset>
</form>