Ensure that exact label matches are favoured over partial matches.

This commit is contained in:
Rob Holland 2009-12-04 11:01:50 +00:00
parent 3f9950e09d
commit 8f6fe3e553
3 changed files with 14 additions and 3 deletions

View File

@ -175,7 +175,7 @@ module Capybara
def find_field_by_label(locator, *kinds)
kinds.each do |kind|
label = find("//label[contains(.,'#{locator}')]").first
label = find("//label[text()='#{locator}']").first || find("//label[contains(.,'#{locator}')]").first
if label
element = find_field_by_id(label[:for], kind)
return element if element

View File

@ -205,6 +205,12 @@ shared_examples_for "session" do
extract_results(@session)['first_name'].should == 'Harry'
end
it "should favour exact label matches over partial matches" do
@session.fill_in('Name', :with => 'Harry Jones')
@session.click_button('awesome')
extract_results(@session)['name'].should == 'Harry Jones'
end
it "should fill in a textarea by id" do
@session.fill_in('form_description', :with => 'Texty text')
@session.click_button('awesome')

View File

@ -11,6 +11,11 @@
<input type="text" name="form[last_name]" value="Smith" id="form_last_name"/>
</p>
<p>
<label for="form_name">Name</label>
<input type="text" name="form[name]" value="John Smith" id="form_name"/>
</p>
<p>
<label for="form_password">Password</label>
<input type="password" name="form[password]" value="seeekrit" id="form_password"/>
@ -102,8 +107,8 @@
<form action="/upload" method="post" enctype="multipart/form-data">
<p>
<label for="form_name">Name</label>
<input type="file" name="form[name]" id="form_name"/>
<label for="form_file_name">File Name</label>
<input type="file" name="form[file_name]" id="form_file_name"/>
</p>
<p>