Allow referencing form elements by name in addition to id and label.

This commit is contained in:
Dan Dofter 2009-12-27 02:11:22 -06:00
parent b3bd6ed44b
commit 3072fdbe8e
3 changed files with 27 additions and 7 deletions

View File

@ -60,32 +60,32 @@ module Capybara
end
def fill_in(locator, options={})
msg = "cannot fill in, no text field, text area or password field with id or label '#{locator}' found"
msg = "cannot fill in, no text field, text area or password field with id, name, or label '#{locator}' found"
locate(XPath.fillable_field(locator), msg).set(options[:with])
end
def choose(locator)
msg = "cannot choose field, no radio button with id or label '#{locator}' found"
msg = "cannot choose field, no radio button with id, name, or label '#{locator}' found"
locate(XPath.radio_button(locator), msg).set(true)
end
def check(locator)
msg = "cannot check field, no checkbox with id or label '#{locator}' found"
msg = "cannot check field, no checkbox with id, name, or label '#{locator}' found"
locate(XPath.checkbox(locator), msg).set(true)
end
def uncheck(locator)
msg = "cannot uncheck field, no checkbox with id or label '#{locator}' found"
msg = "cannot uncheck field, no checkbox with id, name, or label '#{locator}' found"
locate(XPath.checkbox(locator), msg).set(false)
end
def select(value, options={})
msg = "cannot select option, no select box with id or label '#{options[:from]}' found"
msg = "cannot select option, no select box with id, name, or label '#{options[:from]}' found"
locate(XPath.select(options[:from]), msg).select(value)
end
def attach_file(locator, path)
msg = "cannot attach file, no file field with id or label '#{locator}' found"
msg = "cannot attach file, no file field with id, name, or label '#{locator}' found"
locate(XPath.file_field(locator), msg).set(path)
end

View File

@ -3,6 +3,7 @@ module Capybara
# Xpath.text_field('foo').link('blah').to_s
# this will generate an XPath that matches either a text field or a link
class XPath
class << self
def from_css(css)
Nokogiri::CSS.xpath_for(css).first
@ -109,6 +110,7 @@ module Capybara
def add_field(locator, field)
xpath = append("#{field}[@id=#{s(locator)}]")
xpath = xpath.append("#{field}[@name=#{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]")

View File

@ -17,12 +17,18 @@ module FillInSpec
extract_results(@session)['first_name'].should == 'Harry'
end
it "should fill in a text field by name" do
@session.fill_in('form[first_name]', :with => 'Harry')
@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')
@session.click_button('awesome')
@ -41,6 +47,12 @@ module FillInSpec
extract_results(@session)['description'].should == 'Texty text'
end
it "should fill in a textarea by name" do
@session.fill_in('form[description]', :with => 'Texty text')
@session.click_button('awesome')
extract_results(@session)['description'].should == 'Texty text'
end
it "should fill in a password field by id" do
@session.fill_in('form_password', :with => 'supasikrit')
@session.click_button('awesome')
@ -53,6 +65,12 @@ module FillInSpec
extract_results(@session)['password'].should == 'supasikrit'
end
it "should fill in a password field by name" do
@session.fill_in('form[password]', :with => 'supasikrit')
@session.click_button('awesome')
extract_results(@session)['password'].should == 'supasikrit'
end
context "with a locator that doesn't exist" do
it "should raise an error" do
running do