End-to-End API Testing | Bondar Academy
Course: Cypress UI Testing with JavaScript
Module: Working with APIs
Instructor: Artem Bondar
Lesson Summary
In this lesson, we explore an end-to-end flow for API testing without using a browser, focusing on creating, validating, and deleting an article using API calls. Key Steps in the API Flow Create a Token: Start with a POST request to generate an authentication token. Create an Article: Use the token to create a new article with a POST request, e.g., "test title Cypress API testing." Validate Article Creation: Perform a GET request to confirm the article's title matches the expected title. Delete the Article: Retrieve the article's slug ID and use it in a DELETE request to remove the article. Confirm Deletion: Execute another GET request to ensure the article no longer exists in the response. Important Considerations During the testing process, it is crucial to: Validate the status codes of responses (e.g., 200 for successful requests, 204 for successful deletions). Ensure the response body contains the expected data, particularly when validating article titles. Cypress Limitations While Cypress can be used for API testing, it has some limitations: The scripting syntax may be clunky and not user-friendly for extensive API scripting. Cypress requires a browser to run tests, which may not be optimal for large-scale API testing. For more effective API testing at scale, consider alternatives like Rest Assured , Karate , or Playwright , which are better optimized for API automation. In conclusion, while Cypress is capable of API testing, exploring dedicated frameworks may provide a more efficient and scalable solution.
Video Transcript
Hey guys, so let's continue and in this lesson we will run just pure API end-to-end flow without Running anything in the browser. How about that? So let's jump into it. So previously in the previous lesson We already using a couple of API calls to create Token and then create the article. How about if we continue this scenario? Validating the article creation and then deleting of this article So it will be a complete cycle of end-to-end data flow for our test application so for that I will copy these two steps as my initial steps because we need a token and we need the article and I created a new test as the Place holder. So let's check it out This is a POST request to create a token This is our token. Then we create a new article and let's call it differently. For example title test title Cypress API testing something like this and after this Article is created. We expect to see this title right here Now this validation to be honest as the response is not a proper validation for the POST request So if you are doing just pure API testing and pure API End-to-end flow after you make a POST request creating the data You absolutely need to make a GET request to make the validation of this Request so let's do this. So going back to the Postman and where is our GET request? This is this guy. So that's the request that we need to perform I create a new CyRequest CyRequest with a new object now the URL will be this one Then method I need to put comma then method will be method will be a GET and It doesn't have body. All we need is just Authorization header So this is authorization header and I copying pasting it over here after we made a GET request We need to process the response then response Like this first of all validation of the status code The status code as far as I remember should be at 200. Let's double check it in Postman yeah, the status code is 200 and then we can make a validation of the title so response then body property then we are looking for Articles remember it is an array of the articles and we are looking for the first article to validate the title so articles Like this zero and I'm grabbing the title of the article and I expect it to equal The title that we just created this one Okay, and if I want to delete this article afterwards I need to grab the slug ID for this article and I will create a new constant So const slug ID equals 2 and I am taking This property but instead of title I am looking for the slug property for the first article Like this Okay, and since I am planning to use the slug ID for the delete request I need to keep writing the next API call to delete the article within this then function. So Psy request and now I need to delete this article URL Column, let's check it out. What is our URL? This is the delete request from the postman so since we will use the slug ID directly inside of the URL I Will need to pass this variable into URL so I will use a backticks right here instead of a single quote to default the URL and Hit command V and inside of this hard-coded value. I replace it with the Constant I put the dollar sign put the curly brace and I take the slug ID and paste it right here so now it's be a variable from the response of the get request then method have to be a Delete we also need headers Because this is authorized requests authorization header then response And We need to make a validation of the status code Like this and status code should be probably 204 if I remember that correctly That correctly All right And after we done the deletion Then we need to make another get request to make sure that the first article in the response of the articles Does not have the article that we have deleted so I need to repeat this call one more time so I take in this code and I think I can put it right here and Then Response And after that again and again first thing guys we need to make a validation of the status code All right, and then assertion that expect that Them a response body article title So, you know what I can copy the entire assertion from here paste it right here and it should be not To not not equal there Test title Cypress API testing so sounds about it So this is our end-to-end flow using just APIs and let's see if it is Working and let's review it one more time So I'm going back to the Cypress and trying to run this test and it is working Yeah, so look we executed like a lot of API calls it's a pure sequence we got there login we Requested created a new article. We made the validation. This article was created they deleted the article then make another API request that article no longer exists in the list of the articles and Obviously, we don't see anything in the browser because we didn't run anything in the browser Just pure API sequence and also look how fast it is So just one second to execute the entire end-to-end flow and that's why API testing is So much, you know better and stable and faster than UI automation. That's why end-to-end automation using APIs and integration testing using like API layer it is You know worth investments because it provides the fast feedback loop for your application how stable it is how data Integrity working correctly and all that stuff But if you are thinking about using Cypress for just API automation and API testing there are kind of You know opinions I think that Cypress is not very suitable tool for just pure API testing for a couple of reasons so first of all the scripting syntax you see it is kind of I don't know clunky and not very Friendly to be honest in my opinion to make API scripting with the Cypress now and the second thing that in Cypress You need to run browser every time to run your test, which is also not a very optimal way So if you need to, you know write some, you know, 20-30 tests like this, it's not a big deal You can write them in the Cypress but if you think choosing Cypress as your API testing framework and you want to scale your API automation with Cypress I Would think about the alternatives. I would look into some more API testing specific testing framework For example rest assured or karate That are more optimized specifically for API testing it has more user friendly scripting experience Or you can look in the playwright as well It also more capable with API testing than Cypress and by the way I have courses about karate and playwright API automation as well If you are interested in so just my five cents It's up to you to decide Cypress is more than capable for API testing But I think that there are just a better way of doing this if you want to do it at scale All right. That's it guys, and I'll see you in the next lesson