Added support for labels without 'for' Closes #18

This commit is contained in:
Jonas Nicklas 2009-12-15 23:31:57 +01:00
parent 34e064fa4c
commit ce914ed48e
3 changed files with 13 additions and 0 deletions

View File

@ -105,6 +105,7 @@ module Capybara
def add_field(locator, field)
xpath = append("#{field}[@id=#{s(locator)}]")
xpath = xpath.append("#{field}[@id=//label[contains(.,#{s(locator)})]/@for]")
xpath = xpath.append("//label[contains(.,#{s(locator)})]#{field}")
xpath.prepend("#{field}[@id=//label[text()=#{s(locator)}]/@for]")
end

View File

@ -239,6 +239,12 @@ shared_examples_for "session" do
@session.click_button('awesome')
extract_results(@session)['first_name'].should == 'Harry'
end
it "should fill in a text field by label without for" do
@session.fill_in('Street', :with => 'Avenue Q')
@session.click_button('awesome')
extract_results(@session)['street'].should == 'Avenue Q'
end
it "should favour exact label matches over partial matches" do
@session.fill_in('Name', :with => 'Harry Jones')

View File

@ -17,6 +17,12 @@
<label for="form_name">Name</label>
<input type="text" name="form[name]" value="John Smith" id="form_name"/>
</p>
<p>
<label>Street<br/>
<input type="text" name="form[street]" value="Sesame street 66"/>
</label>
</p>
<p>
<label for="form_phone">Phone</label>