From 297dd7349be1efe8e22cbe323cab50d096a0abef Mon Sep 17 00:00:00 2001 From: Jonas Nicklas Date: Sat, 30 Jan 2010 01:02:26 +0100 Subject: [PATCH] Nicer warnings when option not found for select --- lib/capybara.rb | 1 + lib/capybara/driver/celerity_driver.rb | 3 +++ lib/capybara/driver/rack_test_driver.rb | 7 ++++++- lib/capybara/driver/selenium_driver.rb | 3 +++ spec/dsl/select_spec.rb | 6 ++++++ 5 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/capybara.rb b/lib/capybara.rb index 5b767354..7cea9155 100644 --- a/lib/capybara.rb +++ b/lib/capybara.rb @@ -7,6 +7,7 @@ module Capybara class CapybaraError < StandardError; end class DriverNotFoundError < CapybaraError; end class ElementNotFound < CapybaraError; end + class OptionNotFound < ElementNotFound; end class NotSupportedByDriverError < CapybaraError; end class TimeoutError < CapybaraError; end class InfiniteRedirectError < TimeoutError; end diff --git a/lib/capybara/driver/celerity_driver.rb b/lib/capybara/driver/celerity_driver.rb index 3c26ff1c..d5b2e980 100644 --- a/lib/capybara/driver/celerity_driver.rb +++ b/lib/capybara/driver/celerity_driver.rb @@ -19,6 +19,9 @@ class Capybara::Driver::Celerity < Capybara::Driver::Base def select(option) node.select(option) + rescue + options = all(:xpath, "//option").map { |o| "'#{o.text}'" }.join(', ') + raise Capybara::OptionNotFound, "No such option '#{option}' in this select box. Available options: #{options}" end def click diff --git a/lib/capybara/driver/rack_test_driver.rb b/lib/capybara/driver/rack_test_driver.rb index abb6b0a3..01ecdd67 100644 --- a/lib/capybara/driver/rack_test_driver.rb +++ b/lib/capybara/driver/rack_test_driver.rb @@ -33,7 +33,12 @@ class Capybara::Driver::RackTest < Capybara::Driver::Base def select(option) node.xpath(".//option").each { |node| node.remove_attribute("selected") } - node.xpath(".//option[contains(.,'#{option}')]").first["selected"] = 'selected' + if option_node = node.xpath(".//option[contains(.,'#{option}')]").first + option_node["selected"] = 'selected' + else + options = node.xpath(".//option").map { |o| "'#{o.text}'" }.join(', ') + raise Capybara::OptionNotFound, "No such option '#{option}' in this select box. Available options: #{options}" + end end def click diff --git a/lib/capybara/driver/selenium_driver.rb b/lib/capybara/driver/selenium_driver.rb index 16cfd936..d78cc513 100644 --- a/lib/capybara/driver/selenium_driver.rb +++ b/lib/capybara/driver/selenium_driver.rb @@ -29,6 +29,9 @@ class Capybara::Driver::Selenium < Capybara::Driver::Base def select(option) node.find_element(:xpath, ".//option[contains(.,'#{option}')]").select + rescue + options = node.find_elements(:xpath, "//option").map { |o| "'#{o.text}'" }.join(', ') + raise Capybara::OptionNotFound, "No such option '#{option}' in this select box. Available options: #{options}" end def click diff --git a/spec/dsl/select_spec.rb b/spec/dsl/select_spec.rb index 25b657a3..86c8e15a 100644 --- a/spec/dsl/select_spec.rb +++ b/spec/dsl/select_spec.rb @@ -21,5 +21,11 @@ shared_examples_for "select" do running { @session.select('foo', :from => 'does not exist') }.should raise_error(Capybara::ElementNotFound) end end + + context "with an option that doesn't exist" do + it "should raise an error" do + running { @session.select('Does not Exist', :from => 'form_locale') }.should raise_error(Capybara::OptionNotFound) + end + end end end