I'm trying to write some tests in capybara that will test a list of dates are sorted correctly. For example I have a list of dates Wed 27 Jun 12, Mon 13 Aug 12, Thu 31 May 12 and when I click on the button it will rearrange the dates starting from the earliest ie: Thu 31 May 12, Wed 27 Jun 12, 13 Aug 12.
Is there any way in capybara that you can write such tests.
Normally I would use methods like page.find etc but those methods will just find the dates and not tell you if they have been sorted in the correct order.
You need to use the :nth-child
css selector.
Lets say your list of dates are in a <ul>
with the id #dates
, you could test order with:
page.should have_selector("ul#dates li:nth-child(1)", content: @date1.content)
page.should have_selector("ul#dates li:nth-child(2)", content: @date2.content)
The first value passed into have_selector()
is your selector, the second (in this example) is the content you expect. You're not limited to passing just content for example if each date linked to the Date#show
action you could add url: date_path(@date)
.
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