Search code examples
testingcypressgherkin

How to order user clean-up in Gherkin+Cucumber+Cypress


I am writing testing for user-manipulation focused webUI application (freeipa).

I want to test addition of multiple users and then their deletions. For that, I need to make sure that users from previous runs are cleaned, especially since in local development the back-end application keeps running without restart (in Github-CI it spawns for each PR run).

Relevant snippet:

 Background:
    Given I am logged in as "admin"
    Given I am on "active-users" page

  Scenario Outline: Add a new user
    When I click on "Add" button
    * I type in the field "User login" text "<userLogin>"
    * I type in the field "First name" text "<firstName>"
    * I type in the field "Last name" text "<lastName>"
    * I type in the field "New Password" text "<password>"
    * I type in the field "Verify password" text "<password>"
    * in the modal dialog I click on "Add" button
    Then I should see "<userLogin>" entry in the data table
    Examples:
      | userLogin | firstName | lastName | password             |
      | testuser1 | Arthur    | Dent     | ILoveKlingonPoetry42 |
      | testuser2 | Banana    | Bread    | FishAndChips097      |
      | testuser3 | Cypress   | Gateway  | TestingIsFun73       |


  Scenario: Delete a user
    Given I should see "testuser1" entry in the data table
    When I select entry "testuser1" in the data table
    And I click on "Delete" button
    Then I see "Remove active users" modal

    When in the modal dialog I check "Delete" radio selector
    And in the modal dialog I click on "Delete" button
    Then I should not see "testuser1" entry in the data table

What I tried:

  • Before hook
    • This is executed before Background (before admin is logged in), therefore it fails
  • putting "clean state" step in the Background block
    • this gets executed before every scenario, therefore Delete a user fails
  • putting "clean state" step as a first step in Add a new user scenario:
    • This beats the purpose of having multiple users visible.

How should I refactor/order my tests in order to start Add a new user with an empty table, but keep the added users for deletion? If this violates coupling principle, is it possible viable to re-use scenario in another scenario? Thank you!


Solution

  • Since you have three example users, can you only run run "clean state" when the the user count equals 3? Or only when testuser2 and testuser3 are present, since Scenario: Delete a user will take care of #1.

    Place the step last in Background.


    There is also conditional Before() with tags, but I don't think it directly slots into your test suite above (since Scenario Outline: Add a new user runs three times).

    Before({ tags: "@clean" }, function () {
      loginAdmin()
      cleanState()
    })
    

    The other thing to consider is Cypress/Mocha before() as opposed to cucumber Before() which is equivalent to beforeEach().

    In cypress/support/e2e.ts

    before(() => {
      loginAdmin()
      cleanState()
    })