From 43f65f073816c96957fddc5656933f3af4931937 Mon Sep 17 00:00:00 2001 From: Jonas Nicklas Date: Wed, 9 Dec 2009 16:52:30 +0100 Subject: [PATCH] Don't raise error when field not found --- lib/capybara/session.rb | 28 ++++++++++++++++++--------- spec/session_spec.rb | 43 +++++++++++++++++++++++++++++++---------- 2 files changed, 52 insertions(+), 19 deletions(-) diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index 33abbead..0d768cde 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -49,27 +49,39 @@ module Capybara end 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 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 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 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 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 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 def body @@ -129,9 +141,7 @@ module Capybara def find_field(locator, *kinds) kinds = FIELDS_PATHS.keys if kinds.empty? - field = 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 + find_field_by_id(locator, *kinds) || find_field_by_label(locator, *kinds) end alias_method :field_labeled, :find_field diff --git a/spec/session_spec.rb b/spec/session_spec.rb index 7c9e73d3..73ff908a 100644 --- a/spec/session_spec.rb +++ b/spec/session_spec.rb @@ -272,6 +272,12 @@ 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) + end + end end describe "#check" do @@ -290,6 +296,12 @@ 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) + end + end end describe "#uncheck" do @@ -310,6 +322,12 @@ 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) + end + end end describe "#select" do @@ -328,6 +346,12 @@ 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) + end + end end describe '#has_content?' do @@ -509,7 +533,12 @@ 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 @@ -524,23 +553,17 @@ shared_examples_for "session" do end it "should raise an error if the field doesn't exist" do - running { - @session.find_field('Does not exist') - }.should raise_error(Capybara::ElementNotFound) + @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' - running { - @session.find_field('form_description', :password_field) - }.should raise_error(Capybara::ElementNotFound) + @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' - running { - @session.field_labeled('Does not exist') - }.should raise_error(Capybara::ElementNotFound) + @session.field_labeled('Does not exist').should be_nil end end