Course Content
Introduction to QA Automation Testing
Introduction to QA Automation Testing
API Testing with Jest & Axios
In the previous course, we utilized Postman to perform API testing. However, that was from the perspective of Manual Testing.
In Automation Testing, to effectively perform API testing, we would require some kind of a JavaScript method which enables us to make HTTP requests through code.
Axios is a perfect choice in this scenario. It is a Node.JS library which allows us to programmatically make API requests.
The two methods which we will be looking at are:
The usage of these methods is demonstrated in the following video:
For-example, we can make a GET request to the api/posts
endpoint using the following code:
The HTTP Request methods return a response
object containing all the data of the HTTP response. We can use the await
key for the method to return that object and store it in a variable:
Since we are using await
, we need to enclose the code inside an async
function:
From the response, we can extract the Status Code of the response using the status
attribute of the response object. The response data can be extracted from the data
attribute of the response object.
It's imperative to enclose the code inside a try-catch block to make sure the program does not crash in case the HTTP request fails - since the get
and post
methods raise exceptions in case of request failure.
The post
method can be used for making a POST request. The usage is very similar to a GET request:
Thanks for your feedback!