Rspec + Capybara: check for a tag with it css class

I am using Capybara with Rspec and have the following code on my page:

<div class="pagination pagination-centered"> <ul> // ... <li class="last"> <a href="/en/articles?page=2">Last ยป</a> </li> </ul> </div> 

I want to check the query that there is a div with the class "pagination pagination". I tried this:

 it "should have pagination" do page.should have_selector('div#pagination pagination-centered') end 

And this:

 it "should have pagination" do page.should have_selector('div', :class => 'pagination pagination-centered') end 

And nobody is working. How to solve a problem?

+4
source share
4 answers

OK, I found a solution. I should use the have_css method:

 it "should have pagination" do page.should have_css('div.pagination.pagination-centered') end 
+9
source
 expect(page).to have_selector :css, 'nav li.active' 
+4
source

This is what finally worked for me (also using Capybara with Rspec) after trying (and with testing errors) in the earlier examples on this page:

  it { should have_selector('div', 'pagination.pagination-centered') } 
+1
source

Regarding OP:

Thank you for sharing your thoughts, problems and solutions, ExiRe - this helped me find a solution to my own problem. Although I use Rspec on my own and not with Capybara, one of my tests caused a false negative:

it {must have selector ('div.pagination')}

However, the html check in my browser tells me otherwise:

<div class = "pagination">

I changed my test to (somewhat) like the previous of your first two experiments:

it {must have a selector ('div' ,: class => 'pagination')}

And now he is passing.

0
source

Source: https://habr.com/ru/post/1436958/


All Articles