Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capybara, checking HTML element by ID and Class

Tags:

capybara

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!

like image 400
mickael Avatar asked Dec 11 '12 00:12

mickael


1 Answers

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

like image 190
Justin Ko Avatar answered Nov 13 '22 21:11

Justin Ko