Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec + Capybara: check existence of tag with it's css class

I use 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 in the request that a div with class "pagination pagination-centered" exists. 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 neither work. How should I solve the problem?

like image 283
ExiRe Avatar asked Sep 29 '12 14:09

ExiRe


3 Answers

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

it "should have pagination" do
  page.should have_css('div.pagination.pagination-centered')
end
like image 65
ExiRe Avatar answered Oct 22 '22 08:10

ExiRe


expect(page).to have_selector :css, 'nav li.active'
like image 31
Apoorv Mittal Avatar answered Oct 22 '22 08:10

Apoorv Mittal


Regarding the OP:

Thank you for sharing your thoughts, troubles and solutions, ExiRe - it helped me to find the solution to my own problem. Though I am using Rspec by itself, and not with Capybara, one of my tests was producing a false negative:

it { should have_selector('div.pagination') }

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

< div class="pagination" >

I changed my test to (somewhat) resemble the latter of your initial two experiments:

it { should have_selector('div', :class => 'pagination') }

And now it passes.

like image 1
Fluffy Avatar answered Oct 22 '22 08:10

Fluffy