Selenium Interview Questions and answers
Java Coding Interview Questions and Answers
GIT Interview Questions and Answers
Automate GET request & Validate Status Code
In this example, we will send a GET request to the API and check the status code of the response. We expect the status code to be 200, which indicates that the request was successful.
@Test //public is access modifier and void as nothing is returned for getusers() public void getusers() { // resp will contain the response from the api get method for /api/users?page=2 Response resp = given() .when().get(“serverURL/api/users?page=2”); //save the actual status code in integer variable, variable name is actualstatuscode int actualstatuscode = resp.getStatusCode(); //print the status code in the console System.out.println(resp.getStatusCode()); //print the response body in console System.out.println(resp.getBody().asString()); //validating actual status code that we got from API response with the expected status code 200 assertEquals(200,actualstatuscode); } |
Now in the above code, instead of assertEquals(200,actualstatuscode); we can also make use of then() like below:
.then() .statusCode(200); |
Import below in your package before using the code and add rest assured/testng jar:
import static io.restassured.RestAssured.given; import static org.testng.Assert.assertEquals; import org.testng.annotations.Test; import io.restassured.response.Response; |
To create our test, we use the given(), when(), and assertion methods in this code. The given() method constructs the request, the when() method sends it, and the then() method examines the response. The StatusCode() method is used to determine the response’s status code.
Code | Explanation |
Given() | ‘Given’ keyword, lets you set a background, here, you pass the request headers, query and path param, body, cookies. This is optional if these items are not needed in the request |
When() | ‘when’ keyword marks the premise of your scenario. For example, ‘when’ you get/post/put something, do something else. |
Then() | Your assert and matcher conditions go here |
What is the difference between 401 vs 403 ? Very Imp Interview Question
Validate the response body of a GET request
In this example, we will send a GET request to the API and check the response body. Instead of using then() we will use assertEquals for validation of response body, but then90 can also be used for response body validation.
@Test(description=“Verify status code for GET method-users/2 as 200”) public static void verifyStatusCodeGET() { Response resp=given().when().get(“https://reqres.in/api/users”); System.out.println(resp.path(“total”).toString()); assertEquals(resp.getStatusCode(),200); assertEquals(resp.path(“total”).toString(),“12”); } |
To check the response body, we use the body() method in this code. The JSON path and the expected value are passed on to the body() method. In this example, we inspect the JSON path for the first and second blog posts to ensure that the expected values are present.
Automate a POST request with Rest Assured
In this example, we’ll send a POST request to the API and verify that the response contains the data we’re looking for.
Assuming we have an API endpoint that allows us to create new users. JSON request bodies with the fields name, email, and password are accepted by the endpoint. The endpoint returns a JSON response containing the newly created user’s id, name, email, and password.
Through the creation of the following test, we can leverage Rest Assured to automate the process of creating a new user. As we have previously used assertEquals() to verify the response body, we can demonstrate the process of validating response bodies using then() in this example.
@Test public void testCreateUser() { String requestBody = “{\”name\”:\”John Doe\”,\”email\”:\”johndoe@example.com\”,\”password\”:\”password123\”}”; given() .contentType(“application/json”) .body(requestBody) .when() .post(“https://example.com/api/users”) .then() .statusCode(201) .body(“name”, equalTo(“John Doe”)) .body(“email”, equalTo(“johndoe@example.com”)); } |
In this code, we first create a string representation of a JSON request body. We then use the given() method to set the request’s content type to application/json and the body() method to set the request body.
The POST request is then sent to the specified endpoint using the when() method. To specify the HTTP method and endpoint URL, we use the post() method.
Finally, we validate the response using the then() method. The statusCode() method is used to ensure that the response status code is 201. (indicating that the request was successful and a new user was created). We also use the body() method to verify that the response body contains the expected user name and email fields.
Learn how to create a POJO class to send as a Request Body ==> VERY IMP
Similarly, you can also explore to automate the rest of the API methods like PUT & PATCH
Automate DELETE method with Rest Assured
This is an example of a Rest Assured test that validates the status code for a DELETE request. The test sends a DELETE request to the specified API endpoint and verifies that the response status code is 204 (indicating that the user was successfully deleted).
Here’s a rephrased version of the code with comments explaining each step:
@Test(description=“Verify 204 status code for deleting a user”, groups= {“RegressionSuite”,“B_User”}) public void testDeleteUser() { // Send DELETE request to delete a user Response response = given() .delete(“https://reqres.in/api/users/2”); // Verify that the response status code is 204 assertEquals(response.getStatusCode(), 204); // Print message indicating the test passed System.out.println(“Test passed: User deleted successfully with status code 204”); } |
In this code, we use the given() method to set up the DELETE request and specify the API endpoint to delete a user. We then use the delete() method to specify the HTTP method and endpoint URL.
We send the request using given().delete() and store the response in the response variable. We then use the assertEquals() method to verify that the response status code is 204.
Basic Linux Commands for Automation QA
****************************************
TOP 15 BDD - CUCUMBER Interview Q&A
************************************************
✍️AUTHOR: LinkedIn Profile
************************************************
Learn (API-Microservice)Testing+ Selenium UI Automation-SDET with Self Paced Videos prepared by FAANG employees and LIVE Doubt Session
*************************************************
SeleniumWebdriver Automation Testing Interview Questions:
https://automationreinvented.blogspot.com/search/label/SeleniumWebdriver
API Testing Interview Question Set:
https://automationreinvented.blogspot.com/2022/03/top-80-api-testing-interview-questions.html
DevOps Interview Q&A:
https://automationreinvented.blogspot.com/2021/11/top-11-devops-interview-questions-and.html
Kubernetes Interview Question Set
https://automationreinvented.blogspot.com/search/label/Kubernetes
Docker Interview Question Set
https://automationreinvented.blogspot.com/Docker
Linux Interview question Set
https://automationreinvented.blogspot.com/search/label/Linux
Automation Testing/SDET Framework Design
https://automationreinvented.blogspot.com/search/label/FrameworkDesign
Java Related Interview Question Set
https://automationreinvented.blogspot.com/search/label/Java
GIT Interview Question Set:
https://automationreinvented.blogspot.com/2021/09/top-40-git-interview-questions-and.html
Coding Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Coding%20Questions
Mobile Testing Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Mobile%20Testing
Python Interview Question Set for QAE - SDET - SDE:
https://automationreinvented.blogspot.com/search/label/Python