Question

Is there a way to test sorting of a list with Cucumber and Capybara. The sorting is done client-side with javascript.

I was thinking something along the lines of:

Then I should see "first element" and then I should see "second element"

Unfortunately I have no idea how to approach building the steps.

Thanks for the help!

Was it helpful?

Solution

It's a good idea to separate out the stories that you're testing (which you want to get close to plain English) and the actual implementation of the testing (which is hidden in the step_definitions).

There are a few ways to tackle this, depending on what you want to test. In the first case, the cuke test is very readable, and it boils down to implementing the step definitions correctly:

Given that I am on page xyz
And I have a list
Then I should see the list in sorted order

In this case, you'll have to define what it means to have a list (can assign it to @list in a step def if you want), and then what it means to see the list in sorted order (here you can pass a regex that ensures you see item 1 before item 2, etc.)

Alternatively, if you like being more verbose in the cuke tests, you can do something like like:

Given that I am on page xyz
Then I should see /item1.*item2.*item3/

which assumes the list is already populated.

Depending on where the list is, you may have to use a within scope param.

Remember that cucumber is great for functional and integration testing, but probably isn't the right tool for unit testing the sort (looking at all edge cases). To test the sorting at a unit test level, I'd highly recommend using QUnit. Since QUnit tests are static pages, try this trick for running the tests as part of capybara:

Given I am on "/test/path/to/qunit/tests"
Then I should see "Whatever Title You have Assigned"
And I should see "0" within "//p[@id='qunit-testresult']/span/[@class='failed']"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top