In Cucumber, I'm trying to create a step like this:
Then I should see "Example business name" in the "Business name" input
I'd like the "Business name" input to be defined as "the input whose label has text "Business name."
Here's what I've got on the step so far:
Then /^I should see "([^"]*)" in the "([^"]*)" input$/ do |content, labeltext|
# Not sure what to put here
end
In jQuery, I'd look for the label with that text, look at its "for" attribute, and find the input with that id. But the only selectors I've seen so far in Cucumber are these:
within("input:n开发者_StackOverflowth-child(#{pos.to_i}")
and
page.should have_content('foo')
Can anybody suggest a solution using the Webrat / Capybara selector syntax?
Figured it out
You can find an input by its label text using find_field(labeltext)
.
# Example:
# 'I should see "Howdy" in the "Greeting" input' ("Greeting" is the label text)
Then /^I should see "([^"]*)" in the "([^"]*)" input$/ do |content, labeltext|
find_field("#{labeltext}").value.should == content
end
This works too. The difference here is that it finds the field based on a partial match of the label text. Our fields have a ":" at the end of them but it's not always right by it, so i didn't want to match on the whole label value...
When /^i enter "([^"]*)" in the "([^"]*)" field$/i do |value, fieldname|
@thefield = all("label").detect { |l| l.has_content?(fieldname) }
if @thefield.nil? then
raise Exception.new("Couldn't find field #{fieldname}")
end
fill_in @thefield[:for], :with=>value
end
I still want to extend this to be case insensitive though. This is my first day with rspec,cucumber and i really never use ruby so forgive the 'less than rubyish/rspec' code. but it does seem to work.
Update
The following will find the field based on the partial label via a CASE INSENSITIVE match. This works like a charm for my needs.
When= /^i enter "([^"]*)" in the "([^"]*)" field$/i do |value, fieldname|
@thefield = all("label").detect { |l| (l.text =~ /#{fieldname}/i).nil? == false }
if @thefield.nil? then
raise Exception.new("Couldn't find field '#{fieldname}'")
end
fill_in @thefield[:for], :with=>value
end
精彩评论