fill in fields by label

This commit is contained in:
Jonas Nicklas 2009-11-10 00:06:51 +01:00
parent 378dc24f71
commit d25b10769b
2 changed files with 20 additions and 2 deletions

View File

@ -30,7 +30,8 @@ class Webcat::Session
end end
def fill_in(locator, options={}) def fill_in(locator, options={})
find_element("//input[@type='text'][@id='#{locator}']").value = options[:with] element = find_field(locator) { |id| "//input[@type='text'][@id='#{id}']" }
element.value = options[:with]
end end
def body def body
@ -39,12 +40,22 @@ class Webcat::Session
private private
def find_field(locator)
element = find_element(yield(locator), :loose => true)
element ||= begin
label = find_element("//label[text()='#{locator}']")
find_element(yield(label[:for]))
end
element
end
def find_element(*locators) def find_element(*locators)
options = if locators.last.is_a?(Hash) then locators.pop else {} end
locators.each do |locator| locators.each do |locator|
element = driver.find(locator).first element = driver.find(locator).first
return element if element return element if element
end end
raise Webcat::ElementNotFound, "element not found" raise Webcat::ElementNotFound, "element not found" unless options[:loose]
end end
end end

View File

@ -129,6 +129,13 @@ shared_examples_for "session" do
@session.click_button('awesome') @session.click_button('awesome')
YAML.load(@session.body)['first_name'].should == 'Harry' YAML.load(@session.body)['first_name'].should == 'Harry'
end end
it "should fill in a field by label" do
@session.visit('/form')
@session.fill_in('First Name', :with => 'Harry')
@session.click_button('awesome')
YAML.load(@session.body)['first_name'].should == 'Harry'
end
end end
end end