Capybara,在一个css元素内发现 [英] Capybara, finding within a css element

查看:174
本文介绍了Capybara,在一个css元素内发现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ruby on Rails 3,Cucumber和Capybara



我一直在寻找相当长的时间,我不知道如何以找到css标记中的特定页面元素。在我的情况下,我需要确保在表中找到一个名称,而不是在欢迎[名称]。



我试过像:

  within('table')do 
page.body.index([Name])
end

我有一个包含 id ='table' code>。



但我想知道如何对任何css元素执行此操作,例如:

  in('h2')do 
page.body.should have_content('stuff')
end

我认为我的问题与page.body有关,但我不知道如何包含它到一个特定的css标签。 p>

提前感谢!

解决方案

Capybara的 匹配器只匹配第一个结果,因此如果您有多个 h2 标签,它只会查看第一个结果。

$请改用:text 选项

尝试 have_css

  page.should have_css(#table,:text => [Name])
page.should have_css('h2',:text =>'stuff')


I'm working with Ruby on Rails 3, Cucumber, and Capybara

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]".

I tried something like:

within('table') do
  page.body.index("[Name]")
end

And I have a table with id='table'.

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

I think my problem has to do with page.body, but I'm not sure how to contain it to a particular css tag.

Thanks in advance!

解决方案

Capybara's within matcher only matches the first result, so if you have multiple h2 tags, it'll only look in the first one.

Instead, try have_css with the :text option.

page.should have_css("#table", :text => "[Name]")
page.should have_css('h2', :text => 'stuff')

这篇关于Capybara,在一个css元素内发现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆