homerThinking
homerThinking

Reputation: 865

How to run a Cypress test several times in a row by modifying only one variable out of several?

I am creating my first cypress tests for the first time to test an endpoint. This is the login.js

Given(
  "Request to login in correct {string} with {string} and {string}, valid {string} and {string}",
  (country, mail, password, token, udid) => {
    cy.request({
      method: "POST",
      url: `${Cypress.env("BASE_URL")}/api/login`,
      failOnStatusCode: false,
      headers: {
        Authorization: `Basic ${Cypress.env(cvp)}`,
        "Content-Type": "application/json",
        "MRKT": country,
      },
      body: {
        email: Cypress.env(mail),
        password: Cypress.env(password),
        udid: Cypress.env(udid),
      },
    }).as("login");
  }
);


Then("Status Code is {int}", (statusCode) => {
  cy.get("@login").should((response) => {
    expect(response.status).to.eq(statusCode);
  });
});





and this is the login.feature

@login

Scenario Outline: login  - Response 200
    Given Request to login in correct '<country>' with '<mail>' and '<password>', valid '<token>' and '<udid>'
    Then Status Code is 200
    Examples:
    | country | mail | password | token | udid |
    | US | US_MAIL | PASSWORD | US_TOKEN | NO_UDID |
    | UK | UK_MAIL | PASSWORD | UK_TOKEN | NO_UDID |
    


this is running perfectly, giving the desired result but now I'm stuck in a case where I want to pass the same user with different udid to test a device limitation error. In this case I need to run 3 times the same data except the udid that must change in each of the executions to , after being 3 executions in a row , check that returns an error 429 (too many request).

Can I use the same js to test this case ?

An example of what I need to do is sometinhg like this:

@login

Scenario : login - Response 429 too many requests from

    Given Request to login in correct US with US_EMAIL and MAIL, and PASSWORD valid TOKEN and provided FIRST_UDID

    And Request to login in correct US with US_EMAIL and MAIL, and PASSWORD valid TOKEN and provided SECOND_UDID

    And Request to login in correct US with US_EMAIL and MAIL, and PASSWORD valid TOKEN and provided THIRD_UDID

    And Request to login in correct US with US_EMAIL and MAIL, and PASSWORD valid TOKEN and provided FOURTH_UDID

    Then Status Code is 429


or

Scenario : login - Response 429 too many requests 

    Given Request login in correct '<country>' with '<mail>' and '<password>', valid '<token>' and provided '<udid>'

        | country | mail | password | token | udid |

        | US | US_MAIL | PASSWORD | TOKEN | FIRST_UDID |

    And Request to login in correct '<country>' with '<mail>' and '<password>', valid '<token>' and provided '<udid>'

        | country | mail | password | token | udid |

        | US | US_MAIL | PASSWORD | TOKEN | SECOND_UDID |

    And Request to login in correct '<country>' with '<mail>' and '<password>', valid '<token>' and provided '<udid>'

        | country | mail | password | token | udid |

        | US | US_MAIL | PASSWORD | TOKEN | THIRD_UDID |

    And Request to login in correct '<country>' with '<mail>' and '<password>', valid '<token>' and provided '<udid>'

        | country | mail | password | token | udid |

        | US | US_MAIL | PASSWORD | TOKEN | FOURTH_UDID |

    Then Status Code is 429


How can I run the same test 3 times in a row and check the error right after?

Upvotes: 1

Views: 242

Answers (2)

homerThinking
homerThinking

Reputation: 865

I simply execute the same request as many times in a row as I need to, add the data directly and check the status after the number of requests needed. I don't know if there is a better way to execute this case but it works perfectly.

    Given Request to login in correct 'US' with 'US_MAIL' and 'PASSWORD', valid 'US_TOKEN' and 'FIRST_UDID'

    Given Request to login in correct 'US' with 'US_MAIL' and 'PASSWORD', valid 'US_TOKEN' and 'SECOND_UDID'

    Given Request to login in correct 'US' with 'US_MAIL' and 'PASSWORD', valid 'US_TOKEN' and 'THIRD_UDID'

    Given Request to login in correct 'US' with 'US_MAIL' and 'PASSWORD', valid 'US_TOKEN' and 'FOURTH_UDID'

    Then Status Code is 429


Upvotes: 0

PeaceAndQuiet
PeaceAndQuiet

Reputation: 1790

Each line in your Examples: should be considered a different independant test case. You should avoid linking cases using output from the previous Examples: line in the next Examples: line. You should write a new Scenario Outline: for your 429 test since it has a different flow of actions.

@login

Scenario Outline: login  - Response 200
    Given Request to login in correct '<country>' with '<mail>' and '<password>', valid '<token>' and '<udid>'
    Then Status Code is 200
    Examples:
    | country | mail | password | token | udid |
    | US | US_MAIL | PASSWORD | US_TOKEN | NO_UDID |
    | UK | UK_MAIL | PASSWORD | UK_TOKEN | NO_UDID |

Scenario Outline: login - Response 429 (Too many requests)
    Given Request to login in correct '<country>' with '<mail>' and '<password>', valid '<token>' and '<udid1>'
    And   Request to login in correct '<country>' with '<mail>' and '<password>', valid '<token>' and '<udid2>'
    And   Request to login in correct '<country>' with '<mail>' and '<password>', valid '<token>' and '<udid3>'
    Then Status Code is 429
    Examples:
    | country | mail    | password | token    | udid1   | udid2   | udid3   |
    | US      | US_MAIL | PASSWORD | US_TOKEN | NO_UDID | NO_UDID | NO_UDID |
    | UK      | UK_MAIL | PASSWORD | UK_TOKEN | NO_UDID | NO_UDID | NO_UDID |

Upvotes: 2

Related Questions