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?
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
expect(page).to have_selector :css, 'nav li.active'
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With