Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to test the order of elements via RSpec/Capybara?

I'm using RSpec/Capybara as my test suite. I have some javascript that dynamically appends <li> to the end of a <ul>. I want to write a request spec to ensure that this is happening.

I tried using the has_css Capybara method and advanced CSS selectors to test for the ordering of the <li> elements, but Capybara doesn't support the + CSS selector.

Example:

page.should have_css('li:contains("ITEM #1")') pseuo_add_new_li page.should have_css('li:contains("ITEM #1")+li:contains("ITEM #2")') 

Does anyone know of another way to test for ordering?

like image 728
John Avatar asked Dec 07 '11 22:12

John


2 Answers

I resolved this issue by testing for a regex match against the body content of the page. A bit kludgy, but it works.

page.body.should =~ /ITEM1.*ITEM2.*ITEM3/ 
like image 129
John Avatar answered Sep 21 '22 04:09

John


I found a more canonical way of testing this behaviour with CSS. You could user :first-child, :last-child and :nth-child(n) selectors in whichever assert you like.

In your example I'd try these assertions:

page.should have_tag("ul:last-child", :text => "ITEM #1") pseuo_add_new_li page.should have_tag("ul:nth-last-child(2)", :text => "ITEM #1") page.should have_tag("ul:last-child", :text => "ITEM #2") 

I hope this helps someone. Read more about this.

like image 32
dgilperez Avatar answered Sep 22 '22 04:09

dgilperez