Old Guy New Trick

An old guys journey to learn how to code.

Order is Important...

Author: John on July 28, 2015

Order is important, sometimes.  Recently I was assigned a task passed on by QA.  In the application that I am working on, users can download files from the "Download" section after they login.  Depending on their role, admin or client, the downloadable file links will have slightly different names. 

In an effort to keep things unified, QA felt that the order of those links is important - they need to match.  Prior to this revelation, our tests only confirmed that the links existed, were clickable, and the contents of the files was accurate.  Below is a sample of the new steps I created to get things going: 

When  I log in as user
And   I follow "Downloads"
Then  I confirm the following downloadable files:
  | order | description       |
  | 1     | My Bids           |
  | 2     | My Results        |
  | 3     | My Product Status |
  | 4     | My Bidder Status  |
And  I follow "Logout"

When  I log in as admin
And   I follow "Downloads"
Then  I confirm the following downloadable files:
  | order | description        |
  | 1     | All Bids           |
  | 2     | All Results        |
  | 3     | All Product Status |
  | 4     | All Bidder Status  |
And  I follow "Logout"

I struggled for a bit to figure out, how can I write my Cucumber steps so that I verify the data, in the exact order as  specified in the feature file.  My original steps above didn't have an order column.  But as I thought things through, I added the order attribute as seen above.  Now what?  How do I build my steps so that they can actually confirm the order
of my rows in the download link table?  After some trial and errors, and many searches I put together the following:

Then /^I confirm the following downloadable files:$/ do |table|
  table.hashes.each do |line|
    within('tbody') do
      row_num = "tr:nth-child(#{line[:order]})"
      within(row_num) do
        expect(page).to have_text line[:description]

Pretty neat, eh?  With this method, you do not need to ensure that your steps in the feature file are in the exact desired order.  Just be sure to have the correct value in the order column.

Learn Something New Every Day

Last Edited by: John on November 14, 2015