Don't raise error when field not found

This commit is contained in:
Jonas Nicklas 2009-12-09 16:52:30 +01:00
parent 2d4f8b0bee
commit 43f65f0738
2 changed files with 52 additions and 19 deletions

View File

@ -49,27 +49,39 @@ module Capybara
end end
def fill_in(locator, options={}) def fill_in(locator, options={})
find_field(locator, :text_field, :text_area, :password_field).set(options[:with]) field = find_field(locator, :text_field, :text_area, :password_field)
raise Capybara::ElementNotFound, "cannot fill in, no text field, text area or password field with id or label '#{locator}' found" unless field
field.set(options[:with])
end end
def choose(locator) def choose(locator)
find_field(locator, :radio).set(true) field = find_field(locator, :radio)
raise Capybara::ElementNotFound, "cannot choose field, no radio button with id or label '#{locator}' found" unless field
field.set(true)
end end
def check(locator) def check(locator)
find_field(locator, :checkbox).set(true) field = find_field(locator, :checkbox)
raise Capybara::ElementNotFound, "cannot check field, no checkbox with id or label '#{locator}' found" unless field
field.set(true)
end end
def uncheck(locator) def uncheck(locator)
find_field(locator, :checkbox).set(false) field = find_field(locator, :checkbox)
raise Capybara::ElementNotFound, "cannot uncheck field, no checkbox with id or label '#{locator}' found" unless field
field.set(false)
end end
def select(value, options={}) def select(value, options={})
find_field(options[:from], :select).select(value) field = find_field(options[:from], :select)
raise Capybara::ElementNotFound, "cannot select option, no select box with id or label '#{options[:from]}' found" unless field
field.select(value)
end end
def attach_file(locator, path) def attach_file(locator, path)
find_field(locator, :file_field).set(path) field = find_field(locator, :file_field)
raise Capybara::ElementNotFound, "cannot attach file, no file field with id or label '#{locator}' found" unless field
field.set(path)
end end
def body def body
@ -129,9 +141,7 @@ module Capybara
def find_field(locator, *kinds) def find_field(locator, *kinds)
kinds = FIELDS_PATHS.keys if kinds.empty? kinds = FIELDS_PATHS.keys if kinds.empty?
field = find_field_by_id(locator, *kinds) || find_field_by_label(locator, *kinds) find_field_by_id(locator, *kinds) || find_field_by_label(locator, *kinds)
raise Capybara::ElementNotFound, "no field of kind #{kinds.inspect} with id or label '#{locator}' found" unless field
field
end end
alias_method :field_labeled, :find_field alias_method :field_labeled, :find_field

View File

@ -272,6 +272,12 @@ shared_examples_for "session" do
@session.click_button('awesome') @session.click_button('awesome')
extract_results(@session)['gender'].should == 'both' extract_results(@session)['gender'].should == 'both'
end 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)
end
end
end end
describe "#check" do describe "#check" do
@ -290,6 +296,12 @@ shared_examples_for "session" do
@session.click_button('awesome') @session.click_button('awesome')
extract_results(@session)['pets'].should include('dog', 'cat', 'hamster') extract_results(@session)['pets'].should include('dog', 'cat', 'hamster')
end 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)
end
end
end end
describe "#uncheck" do describe "#uncheck" do
@ -310,6 +322,12 @@ shared_examples_for "session" do
extract_results(@session)['pets'].should include('dog') extract_results(@session)['pets'].should include('dog')
extract_results(@session)['pets'].should_not include('hamster') extract_results(@session)['pets'].should_not include('hamster')
end 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)
end
end
end end
describe "#select" do describe "#select" do
@ -328,6 +346,12 @@ shared_examples_for "session" do
@session.click_button('awesome') @session.click_button('awesome')
extract_results(@session)['locale'].should == 'fi' extract_results(@session)['locale'].should == 'fi'
end 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)
end
end
end end
describe '#has_content?' do describe '#has_content?' do
@ -509,7 +533,12 @@ shared_examples_for "session" do
@session.click_button('Upload') @session.click_button('Upload')
end end
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 end
describe '#find_field' do describe '#find_field' do
@ -524,23 +553,17 @@ shared_examples_for "session" do
end end
it "should raise an error if the field doesn't exist" do it "should raise an error if the field doesn't exist" do
running { @session.find_field('Does not exist').should be_nil
@session.find_field('Does not exist')
}.should raise_error(Capybara::ElementNotFound)
end end
it "should find only given kind of field" do 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', :text_field, :text_area).text.should == 'Descriptive text goes here'
running { @session.find_field('form_description', :password_field).should be_nil
@session.find_field('form_description', :password_field)
}.should raise_error(Capybara::ElementNotFound)
end end
it "should be aliased as 'field_labeled' for webrat compatibility" do it "should be aliased as 'field_labeled' for webrat compatibility" do
@session.field_labeled('Dog').value.should == 'dog' @session.field_labeled('Dog').value.should == 'dog'
running { @session.field_labeled('Does not exist').should be_nil
@session.field_labeled('Does not exist')
}.should raise_error(Capybara::ElementNotFound)
end end
end end