Testing Pagination Features with Cucumber - ruby-on-rails

I am learning and liking cucumber, and now have a feature I'm not sure of the best way to proceed on via BDD: pagination. I have scenarios (in my mind) where there are zero pages, one page, several pages, etc. and where I want to make sure certain records are on certain pages, make sure the "next" button is not a link when on the last page, etc.
I will be using will_paginate, so essentially I want to figure out how to BDD its features for a specific list of items, for example, books.
I'm sure I can muddle through it but I feel this should be common and would like to see what others have done. Can anyone recommend an article, or point me to some example code, or even take a shot at an example themselves?

You could probably get away with using scenario outlines to keep the repetition down in your feature file, but be aware that it will expand to a very large number of actually-run scenarios, so it'll be slower than you'd expect. Something like this should probably work, assuming 5 books per page. I'll leave the step definitions as an exercise, but they should be pretty straightforward.
I should also mention that I haven't actually run this, so take any syntax errors with a grain of salt.
Feature: Book Browsing Pagination
Scenario: No results
Given I have 0 books
When I view all books
Then I should see "No results" on the page
Scenario: Some results
Given I have 3 books
When I view all books
Then I should see "Book 1"
And I should see "Book 2"
And I should see "Book 3"
Scenario: Page links
Given I have <count> books
When I view all books from page <page>
Then I should see a link to page <target page>
Examples:
| count | page | target page |
| 8 | 1 | 2 |
| 8 | 2 | 1 |
| 13 | 1 | 2 |
| 13 | 1 | 3 |
| 13 | 2 | 1 |
| 13 | 2 | 3 |
| 13 | 3 | 1 |
| 13 | 3 | 2 |
Scenario: Page links for current page
Given I have <count> books
When I view all books from page <page>
Then I should see a disabled pagination link to page <page>
Examples:
| count | page |
| 8 | 1 |
| 8 | 2 |
| 13 | 1 |
| 13 | 2 |
| 13 | 3 |
Scenario: Next Page links
Given I have <count> books
When I view all books from page <page>
Then I should see a link "Next Page"
When I click "Next Page"
Then I should be on page <next page> # assert against query params maybe?
# alternately, to keep page requests down, one could use something like:
# Then I should see a link "Next Page" going to "?p=<next page>"
Examples:
| count | page | next page |
| 8 | 1 | 2 |
| 13 | 1 | 2 |
| 13 | 2 | 3 |
Scenario: Next Page links on last page
Given I have <count> books
When I view all books from page <page>
Then I should see a disabled pagination link "Next Page"
Examples:
| count | page |
| 8 | 2 |
| 13 | 3 |
Similar scenarios could be used for checking the link state of Previous/First/Last as for Next, and you could add some followup clicking to the Page Links scenario similar to what the Next Page scenario is doing if you wanted. You might also want to add extra examples to check that your pagination is at exactly 5, since the examples I've picked would also pass if pagination was 6 per page. Depends on what exactly your goals are for checking the pagination functionality.
If you eventually decide on something other than will_paginate, then the only things you'd need to look at changing might be a few of the steps (like the disabled pagination ones).
And as you mention asking for links, this might make a good blog post too ;)

Related

How to achieve conditional formatting of names between pages?

I have a Google Sheet with one page for team leaders to list their desired team members (first names, last names, emails) by row--each TL fills in one row--, and a second page where team members are listed who have actually registered with my program.
Page 1
+------------------------+------------+---------------+------------+--------------------+
| Team Leader First Name | First Name | Email Address | First Name | Email Address |
+------------------------+------------+---------------+------------+--------------------+
| Danielle | Elizabeth | XXX#tamu.edu | Matthew | XXX#tamu.edu |
| Stoian | William | XXX#tamu.edu | Victoria | XXX#email.tamu.edu |
| Christa | Olivia | XXX#tamu.edu | | |
+------------------------+------------+---------------+------------+--------------------+
Page 2
+--------------------+-------------------------+
| Scholar First Name | Scholar Preferred Email |
+--------------------+-------------------------+
| elizabeth | xxx#gmail.com |
| william | xxx#tamu.edu |
+--------------------+-------------------------+
I want to be able to see at a glance which of the names listed by the TL on pg 1 have not registered and thus don't appear on pg 2.
In the example above, I want Olivia, Matthew, and Victoria's names to appear red because she does not show up on pg2 (which means they still need to register). Everyone else should appear normally.
I tried at first to importrange from pg1 to get a clean list of the team members, then conditional formatting to match against pg2, the idea I had being it shows up red if a name is not found.
Import range from page 2 to page 1 the scholar first name to F12:F14
Conditional Formatting: Apply to range B2:B999(first name list in page 1)
=NOT(OR(ISNUMBER(MATCH(TRIM(B2),$F$12:$F$13,0)),ISBLANK(B2)))
Conditional Formatting2: Apply to range D2:D999(Second First name list)
=NOT(OR(ISNUMBER(MATCH(TRIM(D2),$F$12:$F$13,0)),ISBLANK(D2)))
Note: Instead of importing, You could also reference the second sheet using INDIRECT.

Behave - Common features between applications, avoiding duplication

I have many applications which I want to test, which have a largely overlapping set of features. Here is an oversimplified example of a scenario I might have:
Given <name> is playing a game,
When they shoot at a <color> target
Then they should <event>
Examples:
| name | color | event |
| Alice | red | hit |
| Alice | blue | miss |
| Bob | red | miss |
| Bob | blue | hit |
| Bob | green | hit |
It's a silly example, but suppose really I have a lot of players with different hit/miss conditions, and I want to run just the scenarios for a given name? Say, I only want to run the tests for Alice. There's still advantage to having all the hit/miss tests in a single Scenario Outline (since, after all, they're all closely related).
One approach would be to just duplicate the test for every name and tag them, so something like:
#Alice
Given Alice is playing a game
When she shoots at a <color> target
Then she should <event>
Examples:
| color | event |
| red | hit |
| blue | miss |
This way I can run behave --tags #Alice, But then I'm repeated the same scenario for every user, and that's a lot of duplication. Is there a good way to still compress all the examples into one scenario - but only selectively run some of them? What's the right approach here?
Version 1.2.5 introduced better ways to distinguish scenario outlines. It is now possible to uniquely distinguish them and thus select a unique scenario generated from an outline with --name= at the command line. For instance, suppose the following feature file:
Feature: test
Scenario Outline: test
Given <name> is playing a game,
When they shoot at a <color> target
Then they should <event>
Examples:
| name | color | event |
| Alice | red | hit |
| Alice | blue | miss |
| Bob | red | miss |
| Bob | blue | hit |
| Bob | green | hit |
Let's say I want to run only the test for Bob, red, miss. It is in the first table, 3rd row. So:
behave --name="#1.3"
will select this test. In version 1.2.5 and subsequent versions. A generated scenario gets a name which includes "#<table number>.<row number>" where <table number> is the number of the table (starting from 1) and <row number> is the number of the row.
This won't easily allow you to select all scenarios that pertain to a single user. However, you can achieve it in another way. You can split your examples in two:
Examples: Alice
| name | color | event |
| Alice | red | hit |
| Alice | blue | miss |
Examples: Bob
| name | color | event |
| Bob | red | miss |
| Bob | blue | hit |
| Bob | green | hit |
The table names will appear in the generated scenario names and you could ask behave to run all the tests associated with one table:
behave --name="Alice"
I do not know of a way to access the example name in steps and thus get rid of the first column.
The full set of details is in the release notes for 1.2.5.

Specflow Feature-level Templates

I'm trying to execute an entire SpecFlow Feature using three different UserID/Password combinations. I'm struggling to find a way to do this in the .feature file without having to introduce any loops in the MSTest.
On the Scenario level I'm doing this:
Scenario Template: Verify the addition functionality
Given the value <x>
And the value <y>
When I add the values together
Then the result should be <z>
Examples:
|x|y|z|
|1|2|3|
|2|2|4|
|2|3|5|
Is there a way to do a similar table at the feature level that will cause the entire feature to be executed for each row in the table?
Is there other functionality available to do the same thing?
I don't think the snippet you have is working is it? I've updated the below with the corrections I think you need (as Fresh also points out) and a couple of possible improvements.
With this snippet, you'll see that the scenario is run for each line in the table of examples. So, the first test will connect with 'Bob' and 'password', ask your tool to add 1 and 2 and check that the answer is 3.
I've also added an ID column - that is optional but I find it much easier to read the results with an ID number.
Scenario Outline: Verify the addition functionality
Given I am connecting with <username> and <password>
When I add <x> and <y> together
Then the result should be <total>
Examples:
| ID | username | password | x | y | total |
| 1 | Bob | password | 1 | 2 | 3 |
| 2 | Helen | Hello123 | 1 | 2 | 3 |
| 3 | Dave | pa£sword | 1 | 2 | 3 |
| 4 | Bob | password | 2 | 3 | 5 |
| 5 | Helen | Hello123 | 2 | 3 | 5 |
| 6 | Dave | pa£sword | 2 | 3 | 5 |
| 7 | Bob | password | 2 | 2 | 4 |
| 8 | Helen | Hello123 | 2 | 2 | 4 |
| 9 | Dave | pa£sword | 2 | 2 | 4 |
"Is there a way to do a similar table at the feature level that will
cause the entire feature to be executed for each row in the table?"
No, Specflow (and indeed the Gherkin language) doesn't have a concept of a "Feature Outline" i.e. a way of specifying a collection of features which should be run in their entirety.
You could possibly achiever what you are looking for by making use of Specflow tags to tag related scenarios. You could then use your test runner to trigger the testing of all the scenarios with that tag e.g.
#related
Scenario: A
Given ...etc...
#related
Scenario: B
Given ...etc.
SpecFlow+ Runner (aka SpecRun, http://www.specflow.org/plus/), provides infrastructure (called test targets) to be able to run the same test suite (or selected scenarios) with different settings. With this you can solve problems like the one you have mentioned. It can be also used to run the same web tests with different browsers, etc. Check this screencast for details: http://www.youtube.com/watch?v=RZYV4Dvhw3w

Cucumber and DELETE with Mongoid

So, I am currently trying to test a Rails app wired up with Mongoid with Cucumber. I have everything setup (or so I believe) so that the following test will run:
Feature: Create and manage about entries
In order to use the about data effectively
As an application consumer
I want to create and manage some about entries
Scenario: Create about entries
Given the following abouts exist
| title | body_copy |
| "About entry #1" | "hello body!" |
When I go to the list of about entries
Then I should see "About entry #1"
Scenario: Create and retreive specific about entry
Given the following abouts exist
| id | title | body_copy |
| 4e4d37756ea257f031000003 | "About entry #1" | "hello body!" |
When I go to about entry with id 4e4d37756ea257f031000003
Then I should see "About entry #1"
In my paths file, I have the following entries to support the above tests:
when /^the list of about entries$/i
'/abouts'
when /^about entry with id (.+)$/i
"/abouts/#{$1}"
These tests work great. However, I need to test the delete action. I did some research online but everything seems to be going through the UI to delete these items and the problem I have is that my Rails app only serves JSON files and JSON files and I need a better (more programatic) way of testing things without the UI being involved. As far as mocks goes, I am using the default mocks built into Pickle. I am open to using other mocking software if necessary such as factory-girl, but you'll have to give me some detailed feedback how I can wire that up. What I have currently for my delete test (that DOESN'T work) is:
Scenario: Delete about
Given the following abouts exist
| title | body_copy |
| title 1 | body_copy 1 |
| title 2 | body_copy 2 |
| title 3 | body_copy 3 |
| title 4 | body_copy 4 |
When I delete the 3rd about
Then I should see the following abouts:
| Title | body_copy |
| title 1 | body_copy 1 |
| title 2 | body_copy 2 |
| title 4 | body_copy 4 |
The problem is that the auto-generated test (seen above) uses the click_link "Destroy" method call but that will not work.
You'll need to change the implementation of the delete step (in your web_steps.rb if your using the Cucumber generated defaults) to send a DELETE HTTP request. I'd recommend the RestClient gem for this, but there are plenty other choices.
The Cucumber Book currently in beta from PragProg has a chapter about using Cucumber to test REST APIs like this.

Should all validation rules be tested within a Cucumber feature?

In Ruby on Rails, if all of the validation rules for a given model are being tested within that model's spec (or unit tests), is it still considered necessary to write a Cucumber scenario for each validation?
Would it suffice to instead write two scenarios: One for when valid data is entered, and one for when invalid data is entered?
This is a good question, and the answer is: It depends.
You can think of Cucumber as a way of communicating between the product owner, developers and testers.
If you feel that having the validations in Cucumber adds to the shared understanding of what the product does, then keep them there.
One approach is to combine the validations into a scenario outline:
Scenario Outline: User tries to register but skips a mandatory field
Given I am registering
And I leave the "<field>" blank
When I click "Submit"
Then I should see "<message>"
And I should not be registered
| field | message |
| Forename | Please enter your forename |
| Surname | Please enter your surname |
| Date of Birth | Please enter your date of birth |
This is a great question, and one that I have been dealing with recently.
It may be different for your organization, but in my organization we try to leave the field validation tests to the unit tests or some other framework that handles these situations better. Aside from that, I am of the school of thought that Cucumber is meant purely for automated acceptance testing (AKA a communications tool between you/your team and the PO) -- and that other methods should be used for anything outside that scope.
Why leave anything open to suggestion? Allowing for assumptions made by developers is inherently risky in terms of generating rework.
How each field a form behaves under different circumstances (create, display, edit, persona) is clearly acceptance criteria.
If you hide it away in a unit test then you are disconnecting it from the living documentation
You really shouldn't need unit tests if you are really bought into the BDD way.
Feature: Edit staff personal
Scenario Outline: Form validation
Given I am editing a staff personal details
And the form contains a "<Mandatory?>" field with a label "<Label>"
And text fields have a input length of between "<Min Length>" and "<Max Length>"
And select fields have these "<Options>"
When I submit the form by clicking the save button
Then an error displays if validation fails
But commits my changes if validation is successful and returns the form back to display mode
Examples:
| Label | Mandatory? | Type | Min length | Max length | Options
| Title | true | select | 0 | 0 | Mr, Mrs, Miss, Ms, Dr, Prof |
| Surname | true | text | 2 | 50 | null |
| Forename | true | text | 2 | 50 | null |
| Known as / Other Surname | false | text | 2 | 50 | null |
| Known as forename | false | text | 2 | 50 | null |
| Date of birth | true | date-picker | 0 | 0 | null |
| NI number | true | ni-number | 0 | 0 | null |

Resources