API Testing Automation Programs - Project One - Notes By ShariqSP
Detailed Explanation of the RegisterUser Program
This program is a simple test case written in Java using TestNG and RestAssured libraries to test the user registration functionality of an API. Below is an explanation of each line:
Code Analysis
Package Declaration
package example;
This declares the package `example` for organizing the code and helps avoid naming conflicts.
Import Statements
import org.json.JSONObject;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.*;
import org.json.JSONObject;: This imports the `JSONObject` class from the `org.json` library, used for constructing JSON objects.import org.testng.annotations.Test;: This imports the `@Test` annotation from TestNG, which marks a method as a test case.import static io.restassured.RestAssured.*;: This is a static import that allows using RestAssured methods directly without prefixing them with the class name.
Class Declaration
public class registerUser
The class registerUser encapsulates the test logic for user registration.
Test Method
@Test
public void RegisterUser()
The @Test annotation marks this method as a test case. The method name RegisterUser indicates the purpose of the test: to verify the user registration functionality.
Object Instantiation and JSON Construction
userData ud = new userData();
JSONObject bodyContent = new JSONObject();
bodyContent.put("id", ud.getId());
bodyContent.put("username", ud.getUsername());
bodyContent.put("email", ud.getEmail());
bodyContent.put("mobile", ud.getMobile());
bodyContent.put("password", ud.getPassword());
userData ud = new userData();: Creates an instance of the `userData` class, presumably containing user information like ID, username, email, etc.JSONObject bodyContent = new JSONObject();: Initializes a JSON object to represent the payload for the API request.bodyContent.put(...): Populates the JSON object with key-value pairs retrieved from the `userData` object.
API Request and Assertion
given()
.header("Content-Type","application/JSON")
.body(bodyContent.toString())
.post("http://apitesting.shariqsp.com:8080/api/users/register")
.then()
.assertThat().statusCode(200);
given(): Starts the RestAssured request-building process..header("Content-Type","application/JSON"): Specifies the header indicating the request body format as JSON..body(bodyContent.toString()): Attaches the JSON payload as a string to the request body..post(...): Sends a POST request to the specified API endpoint..then(): Marks the beginning of response verification..assertThat().statusCode(200): Asserts that the response status code is 200, indicating a successful operation.
Detailed Explanation of the Login Program
This program is a test case written in Java using TestNG and RestAssured libraries to test the user login functionality of an API. Below is a detailed explanation of each line of code:
Code Analysis
Package Declaration
package example;
This declares the package `example` to group related classes and avoid naming conflicts.
Import Statements
import org.json.JSONObject;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.*;
import org.json.JSONObject;: Imports the `JSONObject` class for creating and manipulating JSON objects.import org.testng.annotations.Test;: Imports the `@Test` annotation from TestNG to mark a method as a test case.import static io.restassured.RestAssured.*;: Allows direct usage of RestAssured methods without prefixing them with the class name.
Class Declaration
public class login
The class login contains the test logic for validating user login functionality.
Test Method
@Test
void testLogin()
The @Test annotation marks this method as a test case. The method name testLogin indicates the purpose of this test: to verify the login endpoint of the API.
Object Instantiation and JSON Construction
userData ud = new userData();
JSONObject bodyContent = new JSONObject();
bodyContent.put("username", ud.getUsername());
bodyContent.put("password", ud.getPassword());
userData ud = new userData();: Instantiates the `userData` class, which likely contains predefined user credentials like username and password.JSONObject bodyContent = new JSONObject();: Creates a JSON object to represent the request payload.bodyContent.put(...): Populates the JSON object with key-value pairs for the `username` and `password` fields, using values retrieved from the `userData` object.
API Request and Assertion
given()
.header("Content-Type","application/JSON")
.body(bodyContent.toString())
.post("http://apitesting.shariqsp.com:8080/api/users/login")
.then()
.assertThat()
.statusCode(200)
.log();
given(): Starts building the API request using RestAssured..header("Content-Type","application/JSON"): Sets the request header to indicate that the request body is in JSON format..body(bodyContent.toString()): Attaches the JSON payload (converted to a string) to the request body..post(...): Sends a POST request to the login endpoint of the API..then(): Marks the beginning of the response validation phase..assertThat(): Adds an assertion to validate the response..statusCode(200): Asserts that the response status code is 200, indicating a successful login operation..log();: Logs the response details to the console for debugging purposes.
Detailed Explanation of the GetUsers Program
This program is a test case written in Java using TestNG and RestAssured libraries to retrieve all user details from an API. Below is a detailed explanation of each line of code:
Code Analysis
Package Declaration
package example;
This declares the package `example`, organizing the class into a logical grouping to avoid naming conflicts.
Import Statements
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.testng.annotations.Test;: Imports the `@Test` annotation from TestNG to mark a method as a test case.import io.restassured.RestAssured;: Imports the RestAssured class, which provides the methods for making API calls.import io.restassured.response.Response;: Imports the `Response` class, used to handle and manipulate the response from the API.
Class Declaration
public class getUsers
The class getUsers encapsulates the logic for testing the retrieval of all users from the API.
Test Method
@Test
public void testGetAllUsers()
The @Test annotation marks this method as a test case. The method name testGetAllUsers indicates its purpose: to test the retrieval of all user details via an API endpoint.
API Call and Response Handling
Response resp = RestAssured.get("http://apitesting.shariqsp.com:8080/api/users/all");
System.out.println(resp.asPrettyString());
Response resp = RestAssured.get(...);: Sends a GET request to the specified API endpoint and stores the response in the `resp` object."http://apitesting.shariqsp.com:8080/api/users/all": The API endpoint that provides the details of all users.System.out.println(resp.asPrettyString());: Formats the response as a human-readable string usingasPrettyString()and prints it to the console.
Key Observations
This program does not include assertions to validate the response. Typically, for a more robust test, we would include assertions to check the response status code and verify the data returned by the API.
Detailed Explanation of the Update Program
This program is a test case written in Java using TestNG and RestAssured libraries to test the user update functionality of an API. Below is a detailed explanation of each line of code:
Code Analysis
Package Declaration
package example;
This declares the package `example`, organizing the class into a logical grouping and avoiding naming conflicts.
Import Statements
import org.json.JSONObject;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.*;
import org.json.JSONObject;: Imports the `JSONObject` class for constructing JSON payloads.import org.testng.annotations.Test;: Imports the `@Test` annotation from TestNG to mark a method as a test case.import static io.restassured.RestAssured.*;: Allows the use of RestAssured methods directly without prefixing them with the class name.
Class Declaration
public class update
The class update encapsulates the logic for testing the API functionality to update user details.
Test Method
@Test
void testUpdate()
The @Test annotation marks this method as a test case. The method name testUpdate indicates its purpose: to test updating a user's details via the API.
JSON Payload Construction
JSONObject bodyContent = new JSONObject();
bodyContent.put("id", 12);
bodyContent.put("username", "testinggss");
bodyContent.put("email", "testing@shariqsp.com");
bodyContent.put("mobile", "98765434565");
bodyContent.put("password", "98765");
JSONObject bodyContent = new JSONObject();: Initializes a JSON object to represent the payload for the API request.bodyContent.put(...): Adds key-value pairs to the JSON object representing the user details to be updated, including the ID, username, email, mobile number, and password.
API Request and Response Validation
given()
.header("Content-Type","Application/JSON")
.body(bodyContent.toString())
.put("http://apitesting.shariqsp.com:8080/api/users/update/12")
.then()
.assertThat()
.statusCode(200)
.log();
given(): Starts building the API request using RestAssured..header("Content-Type","Application/JSON"): Adds a header to specify that the request body is in JSON format..body(bodyContent.toString()): Attaches the JSON payload (converted to a string) to the request body..put(...): Sends a PUT request to the specified API endpoint to update the user with ID 12..then(): Marks the start of response validation..assertThat(): Adds an assertion to validate the response..statusCode(200): Asserts that the response status code is 200, indicating the update operation was successful..log();: Logs the response details to the console for debugging purposes.
Detailed Explanation of the UpdateUsername Program
This program is a test case written in Java using TestNG and RestAssured libraries to test the functionality of updating a user's username through an API. Below is a detailed explanation of each line of code:
Code Analysis
Package Declaration
package example;
This declares the package `example`, which organizes the code and avoids naming conflicts with other classes.
Import Statements
import org.json.JSONObject;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.*;
import org.json.JSONObject;: Imports the `JSONObject` class to construct the JSON payload for the API request.import org.testng.annotations.Test;: Imports the `@Test` annotation from TestNG, which is used to mark the method as a test case.import static io.restassured.RestAssured.*;: Allows direct usage of RestAssured methods without prefixing them with the class name.
Class Declaration
public class updateUsername
The class updateUsername contains the logic to test updating a user's username via the API.
Test Method
@Test
void testUpdateUserName()
The @Test annotation marks this method as a test case. The method name testUpdateUserName describes its purpose: to test the API functionality for updating a user's username.
JSON Payload Construction
JSONObject bodyContent = new JSONObject();
bodyContent.put("username", "updated");
JSONObject bodyContent = new JSONObject();: Initializes a JSON object to represent the API request payload.bodyContent.put("username", "updated");: Adds a key-value pair to the JSON object, specifying the new username to update.
API Request and Response Validation
given()
.header("Content-Type","Application/JSON")
.body(bodyContent.toString())
.patch("http://apitesting.shariqsp.com:8080/api/users/update/username/12")
.then()
.assertThat()
.statusCode(200)
.log();
given(): Begins the RestAssured API request-building process..header("Content-Type","Application/JSON"): Adds a header specifying that the request body is in JSON format..body(bodyContent.toString()): Attaches the JSON payload (converted to a string) to the request body..patch(...): Sends a PATCH request to the specified API endpoint to partially update the user's username for user ID 12..then(): Begins the response validation phase..assertThat(): Adds an assertion for response validation..statusCode(200): Asserts that the response status code is 200, indicating a successful update operation..log();: Logs the response details to the console for debugging and verification purposes.
Detailed Explanation of the UpdatePassword Program
This program is a test case written in Java using TestNG and RestAssured libraries to test the functionality of updating a user's password through an API. Below is a detailed explanation of each line of code:
Code Analysis
Package Declaration
package example;
This declares the package `example`, organizing the class into a logical grouping to avoid naming conflicts.
Import Statements
import org.json.JSONObject;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.*;
import org.json.JSONObject;: Imports the `JSONObject` class to construct the JSON payload for the API request.import org.testng.annotations.Test;: Imports the `@Test` annotation from TestNG, which marks the method as a test case.import static io.restassured.RestAssured.*;: Allows direct usage of RestAssured methods without prefixing them with the class name.
Class Declaration
public class updatePassword
The class updatePassword contains the logic to test the API functionality of updating a user's password.
Test Method
@Test
void testUpdatePasswords()
The @Test annotation marks this method as a test case. The method name testUpdatePasswords describes its purpose: to test the API for updating a user's password.
JSON Payload Construction
JSONObject bodyContent = new JSONObject();
bodyContent.put("password", "update");
JSONObject bodyContent = new JSONObject();: Initializes a JSON object to represent the API request payload.bodyContent.put("password", "update");: Adds a key-value pair to the JSON object, specifying the new password to update.
API Request and Response Validation
given()
.header("Content-Type","Application/JSON")
.patch("http://apitesting.shariqsp.com:8080/api/users/update/passwod/12")
.then()
.assertThat()
.log();
given(): Starts building the API request using RestAssured..header("Content-Type","Application/JSON"): Adds a header specifying that the request body is in JSON format..patch(...): Sends a PATCH request to the specified API endpoint to partially update the user's password for user ID 12. (Note: There is a typo in the URL; it should bepasswordinstead ofpasswod.).then(): Marks the start of the response validation phase..assertThat(): Placeholder for assertions to validate the response (e.g., status code, response body). However, no specific assertion is provided here..log();: Logs the response details to the console for debugging and verification purposes.
Key Observations
While the test is functional, it lacks explicit assertions to verify the success of the operation (e.g., checking the status code or response body). Adding a .statusCode(200) assertion would make the test more robust. Additionally, the typo in the URL should be corrected.
Detailed Explanation of the DeleteUser Program
This program is a test case written in Java using TestNG and RestAssured libraries to test the functionality of deleting a user through an API. Below is a detailed explanation of each line of code:
Code Analysis
Package Declaration
package example;
This declares the package `example`, organizing the class and avoiding conflicts with classes in other packages.
Import Statements
import org.testng.annotations.Test;
import static io.restassured.RestAssured.*;
import org.testng.annotations.Test;: Imports the@Testannotation from TestNG, which marks the method as a test case.import static io.restassured.RestAssured.*;: Allows the direct use of RestAssured methods without prefixing them with the class name.
Class Declaration
public class deleteUser
The class deleteUser contains the logic to test the API functionality for deleting a user.
Test Method
@Test
void deleteUser()
The @Test annotation marks this method as a test case. The method name deleteUser describes its purpose: to test the deletion of a user through an API.
API Request and Response Validation
given()
.delete("http://apitesting.shariqsp.com:8080/api/users/delete/12")
.then()
.assertThat()
.statusCode(200)
.log();
given(): Begins the API request-building process using RestAssured..delete(...): Sends a DELETE request to the specified API endpoint to delete the user with ID 12..then(): Marks the start of the response validation phase..assertThat(): Indicates that response validation assertions will follow..statusCode(200): Validates that the response status code is 200, indicating that the deletion operation was successful..log();: Logs the response details to the console for debugging and verification purposes.
Key Observations
The test is well-structured and includes an essential assertion to verify that the API returns the correct status code (200) for a successful deletion. Logging the response ensures that any errors or issues can be easily identified during test execution.