Validation of Browser API Calls | Bondar Academy
Course: Mastering testRigor
Module: Working with APIs
Instructor: Artem Bondar
Lesson Summary
In this lesson, we explore how to validate API calls made by a browser during application operations. This process helps ensure that the application behaves as expected. Key Concepts API Call Interception: By intercepting API calls, we can confirm that the correct requests are being made when user actions occur. Example Scenario: A likes counter for articles that increases or decreases likes based on user clicks. Request Types: The application makes a POST request to update likes and a DELETE request to decrease likes. Validation Process To validate API requests, follow these steps: Log in to the application. Trigger the API call by clicking the likes button. Intercept the API call and validate its properties. Validation Criteria Check that the API call is a POST request with a 200 response status. On the second click, validate that a DELETE request is made with the same endpoint and a 200 response status. Automation Example In the test framework, you can automate the validation using the following code structure: check that API call was made to POST with URL check that API call was made to DELETE with URL Additionally, you can validate request properties such as method , headers , and body . For request body validation, options include text , JSON , or JSON path . By following these steps, you can effectively validate the API calls in your web application. See you in the next lesson!
Video Transcript
In this lesson, we're gonna talk about how to validate the API calls that were performed by the browser. So you can intercept those API calls that browser is doing during the particular operations on your application. And by validating your calls, you can complete that your application doing what it's supposed to do. So let's get into it. So here on our application, we have a likes counter for each of the article. And if I click on this likes counter, we can see that the number increases by one. And if I click it one more time, it decreases by one. But what actually happened in the background? So if I make right click and inspect network and make click one more time and then click one more time, we can see that browser is performing API request to update this value in the database. So when we click on this for the first time, it makes the post request to this URL. So this is the post request. The payload is the empty object and response is information about updated likes for the articles. But if we look at the second API call, this call is a delete type of the request to exactly the same URL. So this is what our application is doing. And let's say that we want to validate that the sequence of API calls that application is performing is correct. And the way how we can do it is to intercept this API call and validate that API call was triggered by the browser when we clicked on this button. And when we click on this one more time, a different type of API call was triggered. Because for example, if not correct type of API call will be created, then something can be messed up with the articles or maybe the values will not be sent to the database at all. So sometimes in your application, it can be useful to validate the API requests of the browser. And let's automate this scenario to validate this API request. So going back to the test framework and I'm creating a new test. So the first step, we need to log in to our application. The next step, we need to click on the likes button in order to trigger the API call. So let's see what we can choose for the locator. So this is the button and it has an icon and this icon has a class IonHeart. So we can just use a partial value of the class to click on the first heart on the page. So let's do this. Click first heart. And after that, we need to intercept browser API call and make a validation. So how can you do this? So going back to documentation and here are several examples of what you can do with this type of validation. So this is the full example of check that API call made a POST request with the URL, with headers. You can validate the request body. You can validate the headers, validate the response status and response status headers, which are coming back. In our example, all we need to validate that this API makes a POST request to a particular endpoint with a response 200. And then we need to validate it. It will be a delete type of the request after we click it on the second time. So just initially, let me take this example as a short version of validation that we want to do. And I paste it right here in the code. So check that browser called API. This we need to replace with our endpoint and response was 200. So going back to application and let's find out the URL. So this is the URL that we expect to be called. And I replace it with this one. And let's quickly run to make sure that it is working as expected. All right, test passed and we can see that here is our assertion that validated that call was performed. If we go back to test application, I'll just make a refresh. We can see that currently 122 likes and it is selected. So I will click one more time and let's see what happens. So going back to the test and what we need to do is basically click on this article one more time. And after that, make a validation one more time. But this time, I want to exactly validate what type of the request will be performed. So with the first type of request, I expect it will be performed on the second time. So with the first type of request, I expect it will be post request. The second type of the request, it will be a delete request. And let me modify my prompt a little bit. So check that API call was made to post with URL and this is the URL. And we replace the same thing in our second assertion. Check that API call was made to delete with URL and URL is the same. And I believe the status code will be so one, two. Yeah, the status code will be 200 for this endpoint. And let's try to run this test one more time. All right, and test pass successfully as well. We can see that here, we made the request to this URL 200 and validation request with URL specified method post. And if we open here more detail, extra info, response is specified. Here we go. Method delete. So test trigger was successfully able to intercept the request that was made by the browser and we validated that. In first example, it was a post request. And in second example, it was a delete request. All right, so let's quickly summarize what we did in this lesson. If you need to make a validation of API calls that your browser performs during a particular operation on the web page, you can do it in test trigger as well. For that, you need to check that API call was made and provide different arguments into your prompt. So you can validate things such as request method, request headers, request body, response status code, and response headers for this API request. And if you want to validate particular values in the request body, there are three types that you can use. Text body will be returned as a default. Also, you can use JSON body or JSON path body parameter if you want to validate the request of this API. All right, that's it, guys, and see you in the next lesson.