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

Nat*_*ong 5 ruby-on-rails webrat cucumber capybara

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

Then I should see "Example business name" in the "Business name" input
Run Code Online (Sandbox Code Playgroud)

我想将"商家名称"输入定义为"标签有文字的输入"商业名称."

这是我到目前为止所做的一切:

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

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

within("input:nth-child(#{pos.to_i}")
Run Code Online (Sandbox Code Playgroud)

page.should have_content('foo')
Run Code Online (Sandbox Code Playgroud)

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

Nat*_*ong 11

弄清楚了

您可以使用标签文本查找输入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
Run Code Online (Sandbox Code Playgroud)