Test Data Setup Using API | Bondar Academy
Course: Mastering testRigor
Module: Working with APIs
Instructor: Artem Bondar
Lesson Summary
In this lesson, we explore how to create test data using API calls to streamline the process of testing article deletion in an application. Overview Instead of manually creating an article through the UI, we can use API calls to automate this process, making it significantly faster. The steps include: Creating an article via API. Testing the deletion of the article through the UI. Process Steps Login Request: Use the users endpoint to obtain an access token by sending a POST request with user credentials. Create Article: Use the access token to send a POST request to the API create articles endpoint, including the article data in the request body. Validate Creation: Refresh the application UI to confirm the article has been created. Delete Article: Navigate to the article in the UI and delete it, confirming the deletion. Implementation Details In TestRigor, the following key elements are used: JSON Path: To extract values from the API response, such as the access token and article title. Authorization Header: Include the access token in the header for subsequent API requests. Reusable Rules: Refactor API calls into reusable rules for cleaner test scripts. By using API calls, we significantly reduce the time taken to create and delete test data compared to manual UI interactions. This method enhances the efficiency of the testing process.
Video Transcript
In this lesson, we're gonna talk about how to create test data using API calls. So we will go through the scenario when we need to, let's say, delete the article. But to delete the article in our application, first, we need to create this article. One way of creating the article is just going through the UI. So we can go to New Article button, create a new article, and after that, test the process of deletion of the article. But this process is kind of slow, and the faster way of doing this is using API. So using API call, we can create a new article, and then from the UI, we can test the process of deletion of this article. And this is what we're gonna do in this lesson, so let's get into it. So in the previous lesson, I showed you how you can use Postman to trigger different API calls and see the result of those API calls in the application. So we went through the scenarios like get tags, for example, what else? This is the user endpoint to get the token, and this is the endpoint to create a new article. So to create a new article, I need just our API URL, I need body that we wanna send, and also I need authorization header over here. And we copied this value from the browser. But in order to simulate this completely through API, we need to get this value differently from API as well. And for that, we can use this endpoint, users endpoint, where we provide our username and password, and API returns the access token for that as a response. And then we can use this token as a header for our request to create the article. So let's first recreate the scenario that we want to test in the Postman N application, and then this scenario later we will put in the test rigger. So first of all, we need to make a login request to post endpoint to get the token. So I make a post request with our user credentials, and we get a fresh value for the token. Then I copy this value, go to this API endpoint, API create articles, and I need to update this value with a new value which we got from the previous request. So this one, I send a request, 201 status code, and this article with this title have to be created in application. So going back to the application, and I just click refresh, and here we go, article is created. Now I can click on the article, delete the article, article does not exist. I'm going back to the Postman one more time, click Send Post Request, article is created, refresh, and this article exists again. So you can see using API creation of the article is a significantly faster process if we just want to delete this article, this is what we want to test. So now what we need to do is to implement these two steps of getting the token for the API request, and then request to create the article using the authorization header that we got on the previous step. So we need to implement two API requests, and after that, article will be created, and then we can go through the UI process and test the deletion of the article. So let's do this, going back to TestRigor, and I will create a new test. Test, deletion of the article. And in order to start writing the first API request, I will go to documentation one more time just to take the example how the request have to be made. So I'm going here to API testing section, and this is the template that we need to use to successfully perform API request. And this is one of the example of the requests like this. So let me just copy this example, and I will modify it later on for our needs. So we make call API, we make a post request, and we need to provide the URL to get the token. So going back to Postman, and first we need this user's login endpoint. And I replace it with this one. Okay, this is good. This is good. With handlers content type and application.json, we don't need this. So I will just go straight to the with body. And instead of the providing string as our request for the body, I want to provide the entire object. So for me, it's gonna be easier to work with. In order to do this, I need to use this kind of syntax. So I'm gonna use body text starting from the next line and ending with. So let me copy the whole thing. Going back and I'm replacing with body and replacing this part. Body text starting from next line and ending with end. So here now we can paste the object that we want to send. Going back to Postman one more time, and this is the main reason why I told you to remember to recreate your API request because easier than from Postman to Test Trigger will be easy to transfer all those parameters to create your call. So now I just copy the request body, putting it over here. All right, this is a request and then get data name. So what is this? Then after we make a request, we want to get a response and we want to get particular values of the response. And this syntax represents a JSON path. So JSON path start with dollar sign and then using dot notation, you can get different values. So here in the Test Trigger documentation, you can read more about JSON path right here. What is different syntax is used for the JSON path? The most simplest one is you call the dollar sign to get the root of the JSON path. And you just go through the object into the details. So API testing and this is the response that we have. This is the object. And this object has a user node. Then inside of the user, we have another object. And inside of the user, we have a token key. And this is token key and this is a value. So we need just a value. To get the value of it, we need to put $.user.token. And we will get the value of this token. So let's do this. So I replace $.user.token. And save it as, and I want to save it as access token. And then check that HTTP status code is 200. In our example, it is also 200. Okay, let's make a validation. This is the first step. Let's run this first. And just to make sure that this API request will be performed successfully. So I click Add and Run. All right, it passed. So now let's check the details. I click More Details, Show Extra Info. And here we go. So as you can see in the logs, that store value access token was set to, and this is the long value of our token. So the API request was successful and we were able to extract the token from the response and saved it to the variable access token. We can go to the next step. So quick edit. So the first step was get access token. And the second step, we need to perform create test article. And we basically will repeat all the same steps, but just with a new parameters. So we need to make post request one more time. It will be API call articles. I replaced the URL, articles. In this example, we also need to pass authorization header. So how to do that? We need to use the syntax like this, with headers and provide the header value. Going back to the script. So with headers and body text starting from the line and ending with. So now instead of this content type application.json, we need to replace the authorization. Colon, and then instead of application.json, we need to provide the variable access token that we used on the previous step. In order to use variable as part of the string, we need to specify this as part of the prompt as well. With, we'll remove this, with headers string with parameters. And instead of application.json, we have to use a value token, then space, and then the value of the actual token. Exactly how it is in the postman. So going back and I type token space, and then I use dollar sign and curly braces and use the value of the access token. Done. The next step, we need to provide body that we're gonna send. Going back to postman one more time. This is the body, copying it from here and pasting it here. And after that, we can extract, for example, the title of the article that we just created. So if we're creating this article, we later want to validate this article. So we can save the title of this article from the response and later validate it. So let's do this as well. I'm looking into the response for the create article request. And we can get it using JSON path as well. So we need to call article and then title. And that way, we're gonna get the title of the article that was just created. So I type article title. And save it as test article title. And then check that HTTP is 200. So in our example, it's 201. So let's change it as well and check that HTTP is 201. I believe this is it. I hope you did not get lost with me on all those modifications. And let me quickly run this as well, just to see if the test article is created inside of the application. Update and retest. All right, request was successful, but we don't see if this article is created here. Let's go to application, refresh, and here we go. We can see the article. So it was created successfully. Let me delete this article right now, because remember, we cannot create two articles with the same title. Application will not allow to do that. So we need to control the flow when we create the article and when we delete this article. So I delete this article, making sure the test will not fail when we run it the next time. So I click Delete Article. And what is left? So pretty much just to update the steps to delete the article from the UI. So all we need to do is to log into application after that. Then we need to make a validation. Check that that article that we have created exists on the page. Page contains stored value. Test article title. Roughly below global feed. Roughly below global feed. The next step, we need to click Stored Value Title. After that, after we click on the article, let me go back. After we click on the article, there will be a Delete Article button. So where is this? Click first button, delete article. And the last step is the validation that test article does not contain stored value. Test article roughly below global feed. So page does not contain the article that we just deleted. And that's pretty much it. And let's run this test and then review it one more time. Update and retest. All right, test pass successfully. Let's quickly check this out. So we are logging in into application. Then we already see that article is created on the page. Then we validated that this article is available on the page. We click on this, we delete the article. And now we make one more validation that article is deleted. So test worked very quick. And as you can see, creating the data through the API is significantly faster process than if we would create the same data using the UI. Clicking on the new article, filling this up, publish the article, and then going back and deleting this article again. So let's quickly review what we just did inside of the script, so quick edit. So first, we created the token for our script. So we use the login URL endpoint. We use the syntax body text starting and next line in order to pass the object. Then using JSON path, we extracted the token from the response and then saved it as access token. And then the next step, we created a second post request to API create articles. We provided header string with parameter, passing the authorization token, access token variable that we created on the previous step. And then we passed body for the request. And as a result, we also using JSON path extracted title from the response, save this title as a test article title variable. We also made the assertion that the response status was 201. And then later, we just use that variable with the title to validate that first it is created, then we click on this and then validate it that it was successfully deleted. And let's make one little refactoring over here. So you can see we use username and password inside of the script. This is not good. So we need to replace this with variables as well. How can you replace variables inside of the JSON object body that you're requesting to the API? And you use exactly the same syntax as we use here string with parameters. So where is that string with parameters and body string with parameter text, starting and ending with, and here we can just replace this with our predefined variables of username and password. Username and password. So now these variables are predefined variables that we define at the very beginning configuration of our test suite where we provided credentials that we use for this application. And now the credentials are not exposed and they are safely secured on the test suite level. So let's run this one more time to see if it is working as expected. All right, it was working. And probably the last step that worth to make is to make some refactoring and remove these API calls from the test itself into the reusable rules. I'll copy this and going back to reusable rule and just create a new reusable rule. And going back to reusable rule and just creating a new reusable rule, create test article and paste this code over here, test article creation. And click save, find and replace. Going back to the tests, view steps, and now our test looks much nicer. So let me quick edit, write this. So we have a precondition step, create test article, then log into application and then we perform the actual operation of deleting of the test article. So now our script is significantly optimized. We improve the speed execution and controlling the creation of the test data using API calls. All right, that's it guys. I hope it was interesting and not very overwhelming. This is how you can use APIs in TestRigor to create a test data for your test runs. See you in the next lesson.