问题描述
我正在使用 Ruby on Rails 3、Cucumber 和 Capybara
I'm working with Ruby on Rails 3, Cucumber, and Capybara
我已经搜索了很长时间,但我无法弄清楚如何在 css 标记中找到特定的页面元素.就我而言,我需要确保在表格中找到名称,而不是在欢迎 [名称]"中.
I've been searching for quite some time, and I can't figure out how to find a specific page element within a css tag. In my case, I need to make sure that a name is found inside of a table, and not in the "Welcome [Name]".
我尝试了类似的方法:
within('table') do
page.body.index("[Name]")
end
我有一张带有 id='table'
的表格.
And I have a table with id='table'
.
但我想知道如何对任何 css 元素执行此操作,例如:
But I'd like to know how to do this for any css element, such as:
within('h2') do
page.body.should have_content ('stuff')
end
我认为我的问题与 page.body 有关,但我不确定如何将其包含到特定的 css 标记中.
I think my problem has to do with page.body, but I'm not sure how to contain it to a particular css tag.
提前致谢!
推荐答案
Capybara的within
匹配器只匹配第一个结果,所以如果你有多个h2
标签,它'只会看第一个.
Capybara's within
matcher only matches the first result, so if you have multiple h2
tags, it'll only look in the first one.
请尝试 have_css
和 :text
选项.
Instead, try have_css
with the :text
option.
page.should have_css("#table", :text => "[Name]")
page.should have_css('h2', :text => 'stuff')
这篇关于Capybara,在 CSS 元素中查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!