Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing a list is sorted in the correct order capybara

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.

like image 429
Ray Avatar asked May 23 '12 16:05

Ray


1 Answers

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).

like image 93
vladiim Avatar answered Oct 31 '22 19:10

vladiim