Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a cucumber step to test if a list of elements is sorted?

I need to write a cucumber scenario to test that a list of projects are sorted (by name). I have something like:

Scenario: Sort projects by name
   Given there is a project called "Project B"
   And there is a project called "Project A"
   And there is a project called "Project C"
   Given I am on the projects page
   When I follow "Sort by name"
   Then I should see in this order ["Project A", "Project B", "Project C"]

I have added a step, that looks like:

Given /^I should see in this order (\[.*\])$/ do |array|

end

What's the best way to test if the projects that are listed on the page appear with the right order? I tried to get all the project names through jQuery:

$(function() {
    var arrjs = new Array();
    $("div.project-main-info").find("a:first").each(function(){
        arrjs.push($(this).text());
    })
  });

and put them inside an array to do a comparison with the array passed as a parameter to this step, but I don't know how to integrate that jQuery code inside this step!

Thanks!

EDIT

As suggested by McStretch, I tried to get the anchors using XPath by doing:

all('a').each do |a|
    if(/\/projects\/\d*/).match("#{a[:href]}")
        arr_page << "...." # Need to retrieve the value out of <a href="..">VALUE</a> but don't know how..any idea?
    end
  end

Is this the right way to proceed? I just tested, and unfortunately arr_page doesn't get filled with anything (I replaced the "..." part with a[:href] just to test)! Actually I tried to check the value of a[:href] (by raising it) and it's blank! How would I better check my anchors (given there href all match the regex mentionned above)?

like image 504
Amokrane Chentir Avatar asked May 04 '11 16:05

Amokrane Chentir


People also ask

How do you write a example of a Cucumber?

In Cucumber, an example is called a scenario. Scenarios are defined in . feature files, which are stored in the src/test/resources/hellocucumber directory (or a subdirectory). One concrete example would be that Sunday isn't Friday.

How do you define step definitions in cucumbers?

Add a Step Definition file 1) Create a new Class file in the 'stepDefinition' package and name it as 'Test_Steps', by right click on the Package and select New > Class. Do not check the option for 'public static void main' and click on Finish button. Take a look at the message in the console window.

Which style is used by Cucumber to run the tests?

It runs automated acceptance tests written in a behavior-driven development (BDD) style. Cucumber was originally written in the Ruby programming language. and was originally used exclusively for Ruby testing as a complement to the RSpec BDD framework.


1 Answers

Firstly, it would be better to write your final step as:

Then I should see the projects in this order:
  | Project A |
  | Project B |
  | Project C |

Now you can easily access the list as an array, e.g.

expected_order = table.raw

You then need to collect together the projects in the page into an array, as @McStretch suggests:

actual_order = page.all('a.project').collect(&:text)

(This assumes that each of your project links has a "project" CSS class to make testing easier).

You can then use RSpec to compare the two arrays.

expected_order.should == actual_order

This will show a failure if the order is incorrect.

like image 115
Andy Waite Avatar answered Sep 28 '22 02:09

Andy Waite