From 294832a0e0fe1390d22452dffa750bca19ccdca9 Mon Sep 17 00:00:00 2001 From: Rob Holland Date: Wed, 3 Feb 2010 13:42:28 +0000 Subject: [PATCH] Make sure we favour exact option matches over partial ones. --- lib/capybara/driver/rack_test_driver.rb | 5 +++-- spec/dsl/select_spec.rb | 6 ++++++ spec/views/form.erb | 9 +++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/capybara/driver/rack_test_driver.rb b/lib/capybara/driver/rack_test_driver.rb index 8a157cd0..7c5b488d 100644 --- a/lib/capybara/driver/rack_test_driver.rb +++ b/lib/capybara/driver/rack_test_driver.rb @@ -32,8 +32,9 @@ class Capybara::Driver::RackTest < Capybara::Driver::Base end def select(option) - node.xpath(".//option").each { |node| node.remove_attribute("selected") } - if option_node = node.xpath(".//option[contains(.,'#{option}')]").first + node.xpath(".//option[@selected]").each { |node| node.remove_attribute("selected") } + if option_node = node.xpath(".//option[text()='#{option}']").first || + node.xpath(".//option[contains(.,'#{option}')]").first option_node["selected"] = 'selected' else options = node.xpath(".//option").map { |o| "'#{o.text}'" }.join(', ') diff --git a/spec/dsl/select_spec.rb b/spec/dsl/select_spec.rb index 86c8e15a..94878aee 100644 --- a/spec/dsl/select_spec.rb +++ b/spec/dsl/select_spec.rb @@ -16,6 +16,12 @@ shared_examples_for "select" do extract_results(@session)['locale'].should == 'fi' end + it "should favour exact matches to option labels" do + @session.select("Mr", :from => 'Title') + @session.click_button('awesome') + extract_results(@session)['title'].should == 'Mr' + 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) diff --git a/spec/views/form.erb b/spec/views/form.erb index c26ad1a3..8dfca239 100644 --- a/spec/views/form.erb +++ b/spec/views/form.erb @@ -2,6 +2,15 @@
+

+ + +

+