3

I have tried using below syntax:

page.body.index('Name') < page.body.index('Phone')

But problem is that if there are multiple strings with same content on same page, then unable to check the index of particular string.

For ex. Page is having content 'Name' and 'Phone' 3 times, then how specific content's order can be verified.

Please suggest if we can use CSS syntax for the same or any other better way.

rhunal
  • 395
  • 2
  • 15

1 Answers1

3

In feature spec, above issue of checking the sequence of elements on full page is solved as below:

Map all the elements on page, insert it into an array and match it with expected array of elements. In expected array, store elements in order as per the requirement.

Below is the solution for above:

expected_array = [ 'Name', 'Phone', 'Email' ]

page.all.map(&:text).should eq expected_array

Also, for particular elements like headers, it can be done using specific css class as below:

page.find('.h4').map(&:text).should eq expected_array

rhunal
  • 395
  • 2
  • 15
  • There is a potential pitfall here that could result in a flaky test in certain scenarios. Imagine you are testing the sort order in a table, and the data already appears on the page but out of order. If you try to check the order after sorting using for example `page.all('td').map(&:text)` after clicking the sort link, even with `all` having waiting behavior Capybara may return the current state of the table cells before the sort is applied and the page is reloaded. Better to use pseudo-classes like `:nth-of-type(n)` etc to explicitly check for the expected order of elements. – soupdog Mar 04 '20 at 19:01