Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test order(sequence) of content using capybara

Tags:

ruby

rspec

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.

like image 660
rhunal Avatar asked Oct 16 '25 03:10

rhunal


1 Answers

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

like image 81
rhunal Avatar answered Oct 17 '25 18:10

rhunal