黄瓜:找到带有标签文字X的输入?

在Cucumber中,我正在尝试创建这样的步骤:

Then I should see "Example business name" in the "Business name" input 

我希望将“商家名称”输入定义为“标签有文字的输入”商业名称。“

这是我到目前为止所做的步骤:

 Then /^I should see "([^"]*)" in the "([^"]*)" input$/ do |content, labeltext| # Not sure what to put here end 

在jQuery中,我会查找带有该文本的标签,查看其“for”属性,并找到带有该id的输入。 但到目前为止我在Cucumber中看到的唯一选择器是这些:

 within("input:nth-child(#{pos.to_i}") 

 page.should have_content('foo') 

任何人都可以使用Webrat / Capybara选择器语法建议解决方案吗?

弄清楚了

您可以使用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 

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

 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,黄瓜的第一天,我真的从不使用ruby,所以请原谅’少于rubyish / 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