Cucumber &引用;那我就看";水豚步骤的范围过于具体

Cucumber &引用;那我就看";水豚步骤的范围过于具体,cucumber,capybara,Cucumber,Capybara,我的页面有一个标记,它的CSS类是“Attention”。以下步骤失败: Then I should see "Name" within "th" 当这一条通过时: Then I should see "Name" within "th.attendance" 我真的必须使用这种程度的特异性吗?我觉得,对于Cucumber来说,这需要对测试的底层代码有太多的了解,而且应该更通用一些 换句话说,我怎么说“在任何“th”标记中查找“[value]”?我这么做是不是错了?试试下面的方法。它使用xp

我的页面有一个
标记,它的CSS类是“Attention”。以下步骤失败:

Then I should see "Name" within "th"
当这一条通过时:

Then I should see "Name" within "th.attendance"
我真的必须使用这种程度的特异性吗?我觉得,对于Cucumber来说,这需要对测试的底层代码有太多的了解,而且应该更通用一些


换句话说,我怎么说“在任何“th”标记中查找“[value]”?我这么做是不是错了?

试试下面的方法。它使用xpath在给定选择器中查找给定文本。
“//#{selector}”
意味着它应该找到与选择器匹配的标记。
“[contains(text(),'#{text}']””
表示标记文本应包含给定文本

Then /^I should see "([^"]*)" within "([^"]*)"$/ do |text, selector|
  find(:xpath, "//#{selector}[contains(text(),'#{text}')]").should_not(be_nil, "Could not find the text '#{text}' within the selector '#{selector}'")
end

尝试以下操作。它使用xpath在给定的选择器中查找给定的文本。
“//#{selector}”
意味着它应该找到与选择器匹配的标记。
“[contains(text(),'#{text}']””
表示标记文本应包含给定文本

Then /^I should see "([^"]*)" within "([^"]*)"$/ do |text, selector|
  find(:xpath, "//#{selector}[contains(text(),'#{text}')]").should_not(be_nil, "Could not find the text '#{text}' within the selector '#{selector}'")
end