Make sure we favour exact option matches over partial ones.

This commit is contained in:
Rob Holland 2010-02-03 13:42:28 +00:00
parent 95a63f8d30
commit 294832a0e0
3 changed files with 18 additions and 2 deletions

View File

@ -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(', ')

View File

@ -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)

View File

@ -2,6 +2,15 @@
<form action="/form" method="post">
<p>
<label for="form_title">Title</label>
<select name="form[title]" id="form_title">
<option>Mrs</option>
<option>Mr</option>
<option>Miss</option>
</select>
</p>
<p>
<label for="form_first_name">
First Name