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?
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/
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.
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