Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cucumber test order of elements in table

I want to run a test that tests the order of the elements I want the order to be ascending by date.

Here is my cucumber feature scenario and the step for the last sentence.

Scenario: Order of the events should be Ascending
    Given I am signed into an account called "Gorilla Tech" as a customer
    When I follow "Register"
    And I follow "Events"
    And I follow "Register new event"
    Then I should see the header "Use the form below to register a new event"
    And I fill in "Title" with "Apefest 2010"
    And I fill in "Event at" with "2010-01-07"
    And I fill in "Add a note" with "This was truly awesome"
    Then I press "Create"
    Then I follow "Register new event"
    And I fill in "Title" with "Monkeyfest 2010"
    And I fill in "Event at" with "2010-01-08"
    And I fill in "Add a note" with "Yeah"
    Then "Apefest 2010" should appear before "Monkeyfest 2010"



   Then /"(.*)" should appear before "(.*)"/ do |first_example, second_example|
     response.body.should =~ /#{first_example}.*#{second_example}/
   end

I really have two problems here. The first one is how do i specify my test correctly? Can i specify my test above different and more correct?

What i want to do is to register 2 different events under 2 different dates. The event will later appear as a list on the webpage. Then i want to check if the events are in order by date . I want the event with the newest date to appear on the top.

here is the code with the collection i want to test. In some way i want to check if the collection in the div below has the an ascending order by date.

<div id="feeds">
    <table>
      <caption><%= t('events.all') %></caption>
      <thead>
        <tr>
          <th><%= Event.t(:title) %></th>
          <th><%= Event.t(:event_at) %></th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <%= render :partial => 'event', :collection => @events %>
      </tbody>
    </table>
  </div>

  <%= will_paginate(@events) %>
like image 743
Soroush Hakami Avatar asked Nov 09 '10 10:11

Soroush Hakami


2 Answers

The way you wrote your step definition is fine you just need to add the multiline (m) switch to your pattern. :)

response.body.should =~ /#{first_item}.*#{second_item}/m
like image 175
Andy Ferra Avatar answered Oct 04 '22 06:10

Andy Ferra


I would perhaps propose some consolidation to your steps by using a table.

Scenario: Order of the events should be Ascending
...
And I fill in "Title" with "Apefest 2010"
And I fill in "Event at" with "2010-01-07"
And I fill in "Add a note" with "This was truly awesome"
Then I press create

I would suggest:

Scenario: Order of the events should be Ascending
...
When I register a new event:
| Title | Event at | Add a note |
| Apefest 2010 | 2010-01-07 | This was truly awesome |

Your current solution has the trade off of not being very precise, looking in the entire response body you may find text from one example in another location. I would suggest the use of XPATH up to your pain threshold or utilizing, I am assuming your using Watir (or variant), the classes.

You could collect all the event names from the table by iterating over the rows (and specifying the column that contains the event name; here I assume the first one)...

event_names = $browser.div(:id,"feeds").tables.first.rows.collect {|row| row[0].text}

Then assert that the events exist and they are in the desired order within the array.

event_names.index(first_example).should_not be_nil
event_names.index(second_example).should_not be_nil
event_names.index(first_example).should < event_names.index(second_example)
like image 43
burtlo Avatar answered Oct 04 '22 06:10

burtlo