Introduction
In this blog, I have discussed the difference in performing API Automation Testing using Java and javascript programming languages.
For Java, I have used rest assured library for API Testing.
And for Javascript, I have used supertest library for API Testing.
Style of writing
Rest Assured
// Specify the base URL to the RESTful web service
RestAssured.baseURI = "https://demoqa.com/BookStore/v1/Books";
// Get the RequestSpecification of the request to be sent to the server.
RequestSpecification httpRequest = RestAssured.given();
// specify the method type (GET) and the parameters if any.
//In this case the request does not take any parameters
Response response = httpRequest.request(Method.GET, "");
// Print the status and message body of the response received from the server
System.out.println("Status received => " + response.getStatusLine());
System.out.println("Response=>" + response.prettyPrint());
SuperTest
Once the request body and headers are set we need to call the end function which performs the request and invokes fn(err, res). in which we can perform the assertions and in case if there is an error returned from the API request, it will throw an error. We need to call the done function once our test is complete. This is necessary as it informs the calling function(err,res)that the task is completed.
it('should successfully pass the test for get api without query param', (done) => {
Request(basuri)
.get('/api/users/2')
.end(function (err, res) {
expect(res.statusCode).to.be.equal(200);
expect(res.body.data.id).to.be.equal(2);
expect(res.body.data.first_name).to.be.equal('Janet');
done();
});
});
Sending query param
Rest Assured
User queryparam method
QueryParam can be passed as:
String endpoint = "http://example.com/building";
var response = given()
.queryParam("globalDates", "{\"startMs\":1473672973818,\"endMs\":1481448973817,\"period\":90}")
.queryParam("startFrom", "0").queryParam("limitTo", "6").queryParam("loadTvData", true)
.queryParam("startFrom", "0").queryParam("userId", "5834fb36981baacb6a876427")
.when().get(endpoint).then();
SuperTest
const queryParams = {
param1: 'value1',
param2: 'value2'
};
request(app)
.get('/endpoint')
.query(queryParams)
.expect(200)
.end((err, res) => {
if (err) throw err;
// Handle the response
console.log(res.body);
});
Sending path param
Rest Assured
Use pathparam method
Response response = RestAssured.given()
.pathParam("userId", userId)
.get(endpoint);
SuperTest

Muzaffar Ur RahmanPosted Dec 5, 2023, 3:35 PM
Nice Article.!