Test Data Cleanup Using API | Bondar Academy
Course: Mastering testRigor
Module: Working with APIs
Instructor: Artem Bondar
Lesson Summary
In this lesson, we explore how to use APIs to manage and clean up test data after executing tests. Proper cleanup is essential to prevent the accumulation of outdated data in the test environment, which can lead to confusion and inefficiencies. Key Steps in Using APIs for Test Data Cleanup Setup Test Data: Create necessary test data before running tests. Execute Tests: Run tests to validate functionality, such as creating an article. Cleanup: Use APIs to delete test data after execution. For example, when creating an article, if the test is run multiple times, it results in multiple test articles. To avoid this, we can use an API call to delete the article post-execution instead of manually deleting it. Implementation Steps Get Access Token: Authenticate to perform secure API calls. Retrieve Article List: Make a GET request to fetch the list of articles and find the slug ID for the created article. Delete Article: Use the DELETE API call with the retrieved slug ID to remove the article. Using tools like Postman , you can easily perform these API calls. For instance, to delete an article, you would: DELETE /api/articles/{slugID} In summary, leveraging APIs for cleanup is the most efficient method to maintain a clean test environment. This approach allows for automated management of test data, ensuring that your testing process remains streamlined and effective.
Video Transcript
In this lesson we're gonna talk about how to use APIs to clean up your test data after the test execution. So pretty often for our test environment, we need either to set up the test data to run the test, or to clean up the test data after the test execution. If you don't clean up the test data, pretty often your test environment slowly piling up with just old data and it just creating a big mess. So to avoid this mess, you need to clean up after yourself. And the best way of doing this is using API. So let me show you how to do this using Test Trigger. So here is our test application, right? And let's say that we want to create the process of creation of the article. So when I create a new article, new article, test, test, push the article, and article is created. When I navigate back to the homepage, we can see the article is created over here. But if we will run this test again and again, imagine how many test articles like that will be created in our test environment. This is actually pretty bad. So after the test execution, the best way is to delete the article. And again, you can do it using UI, but the best way is use API. So instead of clicking the delete article, we can trigger this API call directly and delete the article after the main test execution. So let's create the test. First of all, how we create the article and make a validation that this article is created. And after the test, we will make a cleanup using API. So going back to Test Trigger and I create a new test. So let's create a new test, test creation of the article. So first of all, we need to log into application. Then you need to click on what is new article, click new article. I need to fill up this field, article title. Enter, let's say, test title article into article title. Then enter short test description into what's this article about? What's this article about? Next step is write your article. And let's say we want to type, enter test body text into write your article. After that, we need to click on publish article. So what's next? So let me type something, publish the article. Then we need to validate that on this page is article is created. So we can validate the title, for example, above edit article button, because this button is unique over here. So check that page contains, and this is the test article title. Roughly above, roughly above edit article. By the way, did you notice how quickly actually it is to write the tests in the test rigor using just relative locators on the page, right? So just navigate across the text buttons, use relative location. Scripting is very, very quick. So moving on above the article, then the next step, we want to go to the homepage. So I want to click on conduit to go back to the homepage. Click conduit, conduit, and then I want to make one more assertion that this article is created on the homepage, and this will be a complete test. So roughly below, and roughly below global feed, global feed. Yeah, I guess this is it. This is the test that article can be created. We validate that it is created on the edit article page, then click conduit, and it's displayed on the article, on the global feed on the homepage. Let's run and see if everything is correct. All right, test pass, and it worked perfectly fine from the very first attempt. Perfect. So now let me go back and just manually delete this article just for now, until we implemented the actual logic using API. And now after these steps, we want to perform API request to delete this article. So what we need for that? So let's go to Postman and look one more time what we need for the deletion of the article. So here's the Postman. And previously, we have discovered that in order to delete the article, we need to perform a delete API request to this endpoint API articles providing this slug ID for the article. So where can we get this slug ID? Slug ID we can get if we call articles endpoint. So where is it? Get articles, send request, and here we go. So for the articles, we have array of the articles right here, and this is the slug ID for the article. So what we need to do is basically three steps. Step number one, we need to get access token because without access token, we cannot make secure call to delete the article. The user have to be authorized. Step one. Step two, we need to make a get call to get the current list of the articles and find the slug ID based on the title of the article in this big object of the articles. Most likely, it's going to be first article, but we want to search by title in case someone else running. So test in parallel in our test article will be not the first one. It will be the third one or second one. So we want to make sure that we delete exactly the article that we are looking for, and we are picking up the correct slug ID. And when we get this slug ID, then we can create this call to delete the article knowing the slug ID and article will be deleted. So let's perform those three steps. The first step is pretty easy because we already know, so validate and save reusable rules. We already know how to create token. It was used in the one of our previous tests to create the article. And what we can do, I can probably extract this logic to a new reusable rule. So get access token will be independent rule just for the convenience. So new rule, get access token. I put this code over here and this will give us access token. And click save. Sweet. Now inside of our test, quick edit. I type get and okay, it's not visible. Why? Because we didn't provide the label. Get access token. All right, save. All right, now it's saved successfully. Going back, get access token. Okay, it's working. The next step, we need to make a get call to get the list of the articles. So going back to documentation and I just take this example over here. Or no, let me take this one. This will be easier. So now the next step, get list of all articles. I'm pasting it here. So this will be a get call. We need to replace a URL. So this is our URL to get all articles. Replacing it right here. Now with headers. Remember, we need to provide header with the token. So I'm going here to create the article just to take the example that we used before. And scrolling it down. And this one with header string with parameters, authorization token and token. I'll take in this. And here with headers and before body and pasting it here. So with header string with parameter, authorization token and token. And yeah, that's it. And we don't have a body since it's a get call. And we can make an assertion that it is a status 200. I think it's 200, right? Yeah, it's 200. Okay. And now we need to extract the response as well using JSON path and save it to variable. So how we did before and get and save as. So I'm copying this, going back. So after the response and get and save it. And then check status 200. So here, get the. Here we need to replace with the correct JSON path. So how to find the JSON path for this object? For example, so we have a huge object right here, right? And we want to find the slug by the title using JSON path. The easiest way would be and like the hacky way is just using chat GPT. Because when you're dealing with complex JSON path, not just simple dot notation, you can just use chat GPT and hey, give me the right JSON path. So this is what I'm gonna do. So I'm going to chat GPT and doing something like this. Here is my JSON object. Boom, I want you to give me the value of slug for the article that has title. And this is just our training object. So let's say I want to get slug ID for this title. I just want a syntax, this one. So I want you to give me the value of slug for the article that has title, Discover Bonder Academy and so on. And by the way, we need to tell him that we want a JSON path. So I want to give me JSON path. I want to give me JSON path for the value of slug for the article that has title Discover Bonder Academy. Okay, let's try it on. Let's see what it's gonna do. And here we go. So it provided the value. So this is the value that we need. That's gonna return this slug ID for the title of the article. So I'm just copying this, going back and replacing it right here. But instead of the title that we used over here, I'm gonna use the title that we use inside of a test. This is test title article. And I remove this. All right, so this is our JSON path that will return us a correct slug ID and save this value as test article slug ID. And check that HTTP code is 200. All right, and yeah, and last step, delete the article by slug ID. All we need to do is to perform this API call providing this slug ID. So let's do this. So let's do this, call get, let me do a copying here. So an API, delete the URL, will be this one. Okay, but instead of this slug ID, we need to provide our own. So we will use string with parameters. And then instead of this value, we will replace it with variable. And the variable name is test article slug ID, okay? So call API string with parameters URL with header string with authorization token. That is correct. And the request does not have any response. So we don't need to get anything. And then check the status code is, and what is the status code? I think it's 204 as far as I remember. And status code is 204. I think that's pretty much it. So we created three steps. First one is get access token that we created previous lesson. Then we make a get call to get the list of the articles and get the slug ID for the article with our test title using JSON path. And then we use this slug ID in the API request, delete API request over here to delete this article. All right, I think we're all set. Let's try to run it. All right, the test pass successfully. But if you look into the screenshot, you still see this article displayed on the page, right? But the actually article is deleted. We just see this as a last screenshot. But screenshot does not reflect something that was happened in the background in the form of the API. Because if we go to our application and just hit a refresh, it does not exist. Because the article was deleted successfully. And also if we go here to the extra info and we will see the log method delete URL and we can see that the request was successful over here. And the response code was 204. So request was successful and the article was deleted successfully. Well, that's it. This is how you can control the test data and delete the old test data using API. This is the fastest, the most efficient approach to keep your test environment clean from old, not needed test data. And let's quickly summarize what we did in this lesson. So when you need to clean your test data, the best way is to use, of course, API. In this particular example, we extracted the needed slug ID using a JSON path from the list of the articles that are created. Save the slug ID into the variable and then use this slug ID as part of delete API call inside of our URL to delete the article. That way we can keep our environment clean. All right, that's it guys and see you in the next lesson.