Capybara, checking HTML element by ID and Class

半世苍凉 提交于 2019-12-20 17:33:36

问题


Two questions from a beginner.

Q1- Is it possible to assert the existence of an HTML node by ID and class? For example, to see if the following element exists:

<div class="drawer" id="first"....>

I've seen you can use something like:

 page.should have_css('div.drawer')
 page.should have_css('div#first')

but can we somehow query for the existence of both parameters, I've tried the following and didn't work:

page.should have_selector("div", :class => "drawer", :id => "first")

Q2- Is it possible to add 2 selectors to the 'within' capybara method, ie, I've seen you can limit the scope by doing:

within("//div[@id='first']") do

but can we filter that DIV by adding id='first' and class='drawer' somehow?

Many thanks!


回答1:


You can combine the selectors.

For your first question, the following checks for a div with id "first" and class "drawer":

page.should have_css('div#first.drawer')

For your second question, the within block can use the same css-selector as above:

within('div#first.drawer') do

Or if you really prefer xpath, you can do:

within("//div[@id='first' and @class='drawer']") do

A good reference for css-selectors: http://www.w3.org/TR/CSS2/selector.html



来源:https://stackoverflow.com/questions/13811763/capybara-checking-html-element-by-id-and-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!