Afraz Ali
Afraz Ali

Reputation: 2762

What is the best practice for writing e2e tests that need to run in a sequence?

We are in the midst of writing e2e tests for our angular app using Cypress.io. The problem we are facing is that even though we know that tests should not depend on each other, but realistically speaking it seems impossible to achieve this in a real world app. Suppose a user needs to perform the following actions

  1. Signup
  2. Login
  3. Create a category
  4. Create a sub-category
  5. Crete a product in a specific sub category

Now if we have to write tests for these scenarios, we will either have to write one giant test that does all of this in one go or find a way to make them run in a sequence where perhaps each next test depends on the state left by the previous. I am curious how everyone tackles this situation because I feel it should appear very frequently in real enterprise apps.

Upvotes: 2

Views: 1726

Answers (4)

Kevin Old
Kevin Old

Reputation: 1027

Take a look a the Cypress Real World App created for demonstrating real-world usage of Cypress testing methods, patterns, practices, and workflows.

https://www.cypress.io/blog/2020/06/11/introducing-the-cypress-real-world-app/

Upvotes: 0

Srinu Kodi
Srinu Kodi

Reputation: 516

  1. Signup
  2. Login
  3. Create a category
  4. Create a sub-category
  5. Crete a product in a specific sub category

Basically when you write E2E tests, first thumb rule is your tests have to be independently run and it should not depend on previous tests. Here you are running independent tests, but at the same time you are making API call to complete your pre-req steps(in login case, signup is done through API)

So basically you can proceed as below.

  1. Write test for Signup functionality.

  2. Now for login test, you will not do SignUp process again thru UI. So all you have to do make API call to do SignUp process.
    Reason: Your UI for SignUp UI might have broken, and rest of the functionalities work fine. So you will not use UI route to do that. So make API call

  3. As mentioned above, you will make API calls to do 1st & 2nd test case to land on for 3rd case

  4. As mentioned above, you will make API calls to do 1st & 2nd & 3rd test case to land on for 4th case

  5. As mentioned above, you will make API calls to do 1st & 2nd & 3rd & 4th test case to land on for 5th case

Upvotes: 3

Muditha Perera
Muditha Perera

Reputation: 1248

Basically what I'll be doing is create 5 test files which include corresponding tests. But each test will have different pre-requisites. In your scenario,

  • Sign up - Have No dependencies
  • Login - Needs a signed up user
  • Create a category - needs a logged-in user
  • Create a sub-category - Needs a logged-in user and a created category .. etc.

What I would do is,

  • Follow the Page Object Model to identify, store and retrieve the page elements
  • Have a common place to store methods/functions

Then if we take test case for Create a category

As prerequisites (beforeeach)

  • Set the viewport
  • login to the page
  • Navigate to Create a category page/section
  • Veryfy the url

in test cases (it)

  • will add specific test scenarios, validations, etc as test cases specific for Create a category.

The reason to cluster your test scrips is it's easy to individually check the components if needed. If not the test file will be huge and it will make it really hard to update and most importantly when you do a modification on Create a sub-category and if you run the automation, cypress will go through all the test cases even they are not relevant.

As I was saying, Create a product in a specific subcategory

As pre-requests,

  • We need a user logged in
  • We need a created category
  • We need a created - sub- category

In all of these steps, to check the test scenarios of Create a product in a specific subcategory we don't have to go through the login validations, or create category validation or create subcategory validations. We just need a VALID login, VALID category, and a VALID sub-category. Most probably we can have them available before running the test.

so we will be,

Navigating to login with a valid login, select our category and the subcategory ( or you can create new ones ) and then do all the invalid and valid scenario testing to check the Create a product in a specific subcategory. Because it doesn't make sense you running all the login validations when you need to check the create product functionality.

Hope this helps, Cheers

Upvotes: 2

Afraz Ali
Afraz Ali

Reputation: 2762

Posting this for anyone else who stumbles upon this question in future and is considering Cypress.io for writing e2e tests.

This blog post on their website summarizes best practices of writing e2e tests.

An excellent talk by Brian Mann shows best practices of writing e2e tests using Cypress.io

Basically we can use commands in Cyprus to perform common actions that need to run before every test.

Answer by Mudithra is perfectly valid for my question to explain in general how we should tackle scenarios where a test is a prerequisite for another test.

Upvotes: 0

Related Questions