6

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:nth-child(#{pos.to_i}")

and

page.should have_content('foo')

Can anybody suggest a solution using the Webrat / Capybara selector syntax?

4

2 回答 2

12

弄清楚了

您可以使用 . 通过其标签文本查找输入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
于 2010-11-11T19:15:18.970 回答
0

这也有效。这里的区别在于它根据标签文本的部分匹配来查找字段。我们的字段末尾有一个“:”,但它并不总是正确的,所以我不想匹配整个标签值......

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

不过,我仍然想将其扩展为不区分大小写。这是我使用 rspec、cucumber 的第一天,我真的从不使用 ruby​​,所以请原谅“低于 ruby​​ish/rspec”的代码。但它似乎确实有效。

更新

以下将通过 CASE INSENSITIVE 匹配基于部分标签查找字段。这对我的需求来说就像一个魅力。

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
于 2010-12-23T01:08:57.980 回答