Showing posts with label Test Automation Framework. Show all posts
Showing posts with label Test Automation Framework. Show all posts

First TestNG script




This is a simple Testng script which print message before and after tests and run two test methods using Testng annotations.

After installation of TestNG in Eclipse or IntelliJ Idea, we need to follow below steps to write TestNG scripts.


Below are the steps of writing first TestNG script.

a) Create a  java class as test class, we can name as MyFirstTest.java

b) Add TestNG dependencies in POM.xml

c) Write TestNG test code in MyFirstTest.java class as mentioned below.

d) We can directly execute code in eclipse or intellij Idea or create a separate TestNG.xml for Test cases execution, as shown below.

e) Right Click on TestNG.xml file and run the file.














Step 1: Project Structure

You can create this in EclipseIntelliJ, or any Java IDE.

MyTestNGProject/

├── testng.xml

└── src/

    └── MyFirstTest.java



Step 2: Add TestNG to Project


If using Maven, add this to pom.xml:

<dependencies>
  <dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.9.0</version> <!-- Latest version -->
    <scope>test</scope>
  </dependency>
</dependencies>


If not using Maven:



Step 3: Java Code – MyFirstTest.java

import org.testng.annotations.*;

public class MyFirstTest {

    @BeforeClass
    public void setup() {
        System.out.println("Setting up before running any test...");
    }

    @Test
    public void testLogin() {
        System.out.println("Running test: Login functionality");
    }

    @Test
    public void testLogout() {
        System.out.println("Running test: Logout functionality");
    }

    @AfterClass
    public void tearDown() {
        System.out.println("Cleaning up after all tests...");
    }
}



Explanation of Code

@BeforeClassRuns once before any @Test method in this class. Used to initialize resources.
@Test: Marks a method as a test case. TestNG will automatically detect and run it.
@AfterClass: Runs once after all @Test methods have run. Used to clean up resources.



Step 4: Create Testng.xml File

This file tells TestNG which classes to run.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="MyFirstTestSuite">
  <test name="SimpleTest">
    <classes>
      <class name="MyFirstTest"/>
    </classes>
  </test>
</suite>



Step 5: Run the Test

How to Run in Eclipse/IntelliJ:

  • Right-click testng.xml
  • Choose Run As → TestNG Suite


Output

Test script executed successfully and logs the statements on console.

Setting up before running any test...
Running test: Login functionality
Running test: Logout functionality
Cleaning up after all tests...


Suggested Posts:

1. Read Excel Data by Data provider
2. TestNG Dependent Tests
3. TestNG Asserts
4. Parameterization in TestNG
5. Priority in TestNG

How to validate Response by Matchers API in Rest Assured

  



How to Use Hamcrest Matchers in GET API via Rest Assured

Hamcrest is a framework for writing matcher objects, which allow 'match' rules to be defined declaratively. In Rest Assured, we use Hamcrest matchers to write expressive assertions for validating the API response.


We can use the Hamcrest Matchers API in a GET API test by employing the assertThat assertion within your testing framework (like JUnit or TestNG), where the actual value is an element extracted from the API's response body or headers, and the matcher defines the specific expectation.

The entire process follows three theoretical steps: Execute, Extract, and Assert.

You use the Hamcrest Matchers API in a GET API test by employing the assertThat assertion within your testing framework (like JUnit or TestNG), where the actual value is an element extracted from the API's response body or headers, and the matcher defines the specific expectation.

The entire process follows three theoretical steps: Execute, Extract, and Assert.


1. Execute the GET Request

First, your testing tool performs the GET request to the target API endpoint. The result of this execution is the API Response, which contains:
  • Status Line: The HTTP Status Code (example: 200 OK).
  • Headers: Metadata about the response (e.g., Content-Type, Date).
  • Body: The payload, usually in JSON or XML format.

2. Extract the Actual Value

This step involves parsing the response and pulling out the specific piece of data you want to validate.
  • Extraction from the Body: If the response body is JSON (which is common), you use a JSONPath or XPath expression (depending on the testing library, like REST Assured) to navigate the structure and isolate the required value.
Example: Extracting a product name from a nested JSON object.
  • Extraction from Headers: You extract a specific header value directly.
Example: Extracting the value of the Content-Type header.
  • Extraction of Status Code: You get the numeric HTTP status code.
This extracted data becomes the actual value (actual_value) for your Hamcrest assertion.


3. Assert with Hamcrest Matchers

The final step is the assertion, where the extracted value is verified against the expected condition using the Hamcrest syntax: assertThat(actual_value,matcher).











Maven Dependencies:

<dependencies>
    <!-- Rest Assured -->
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>5.3.0</version>
        <scope>test</scope>
    </dependency>

    <!-- Hamcrest -->
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest</artifactId>
        <version>2.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>


Commonly Used Hamcrest Matchers

MatcherDescription
equalTo(value)Check equality
hasItem(value)Check if a list contains item
hasItems(val1, val2)Check if list contains multiple items
not(value)Asserts the opposite
containsString(str)Check substring match
startsWith(str)Check if string starts with



API to be tested:

https://reqres.in/api/users?page=2



API Response of above API











Java Code:

import io.restassured.RestAssured;
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;

public class GetApiHamcrestExample {
    public static void main(String[] args) {

        // Set base URI
        RestAssured.baseURI = "https://reqres.in";

        // GET Request with Hamcrest matchers for assertions
        given()
            .log().all()
        .when()
            .get("/api/users?page=2")
        .then()
            .log().all()
            .statusCode(200)
            .body("page", equalTo(2))  // validate page number
            .body("data.id", hasItems(7, 8, 9))  // check if certain user IDs are present
            .body("data.first_name", hasItem("Michael"))  // check first_name list contains value
            .body("support.url", containsString("reqres.in"))  // substring match
            .body("data.email[0]", startsWith("michael"));  // starts with
    }
}



Code Explanation:

(a) Set the base URI
(b) Send the API request
(c) Get the API response nd log on console
(d) Validate API response by Matchers API methods like equalTo(), hasItems(), hasItem(), etc.



Output:

Got 200 server code response as shown below in the response. The validated data from Matcher's API is also mentioned below.

Request:
GET https://reqres.in/api/users?page=2

Response:
200 OK
...
Assertions:
page == 2
data.id contains 7, 8, 9
data.first_name contains "Michael"
support.url contains "reqres.in"
data.email[0] startsWith "michael"


Suggested Posts:

1. Test Basic Authentication in RestAssured
2. Test Preemptive Authentication in RestAssured
3. Test Form Authentication in RestAssured
4. Validate Keys in API in RestAssured
5. Validate XML Schema in RestAssured

How to validate API Response from Database in Rest Assured

 



Validating an API Response against the Database in Rest Assured means checking whether the data returned by the API is consistent with the actual data stored in the backend database. This ensures that the API is not only returning the correct structure but also the correct content.

To validate an API response against data in a MySQL database using Rest Assured, follow these steps:








Step-by-Step Guide


Step 1: Setup Project with Required Dependencies


<dependencies>
    <!-- Rest Assured for API Testing -->
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>5.3.0</version>
        <scope>test</scope>
    </dependency>

    <!-- MySQL Connector for DB Connection -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.33</version>
    </dependency>

    <!-- JSON handling -->
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20230227</version>
    </dependency>

    <!-- TestNG (optional for test framework) -->
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.4.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>


Step 2: MySQL Database Setup

Suppose you have a table users with structure:

CREATE TABLE users (
    id INT PRIMARY KEY,
    email VARCHAR(255),
    first_name VARCHAR(100),
    last_name VARCHAR(100),
    avatar VARCHAR(255)
);



Insert some matching test data from https://reqres.in/api/users?page=2


















Step 3: Java Code

import io.restassured.RestAssured;
import io.restassured.response.Response;

import java.sql.*;
import java.util.List;
import java.util.Map;

import static io.restassured.RestAssured.given;

public class APIDatabaseValidation {

    // JDBC details
    static final String DB_URL = "jdbc:mysql://localhost:3306/testdb";
    static final String USER = "root";
    static final String PASS = "password";

    public static void main(String[] args) throws Exception {
        // Step 1: Get API response
        RestAssured.baseURI = "https://reqres.in";
        Response response = given()
                .when()
                .get("/api/users?page=2")
                .then()
                .statusCode(200)
                .extract().response();

        // Step 2: Extract JSON data from API
        List<Map<String, Object>> apiUsers = response.jsonPath().getList("data");

        // Step 3: Connect to MySQL and validate
        Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
        Statement stmt = conn.createStatement();

        for (Map<String, Object> apiUser : apiUsers) {
            int id = (Integer) apiUser.get("id");
            String email = (String) apiUser.get("email");
            String firstName = (String) apiUser.get("first_name");
            String lastName = (String) apiUser.get("last_name");
            String avatar = (String) apiUser.get("avatar");

            // Query DB for this user
            String query = "SELECT * FROM users WHERE id = " + id;
            ResultSet rs = stmt.executeQuery(query);

            if (rs.next()) {
                assert email.equals(rs.getString("email")) : "Email mismatch for ID " + id;
                assert firstName.equals(rs.getString("first_name")) : "First name mismatch for ID " + id;
                assert lastName.equals(rs.getString("last_name")) : "Last name mismatch for ID " + id;
                assert avatar.equals(rs.getString("avatar")) : "Avatar mismatch for ID " + id;
                System.out.println("User ID " + id + " matched successfully.");
            } else {
                System.err.println("User ID " + id + " not found in database.");
            }
            rs.close();
        }

        // Close DB connection
        stmt.close();
        conn.close();
    }
}


Code explanation: 

(a) Define database details like db username, db password, db url, before main method
(b) In main method, define base URI, send API request and extract data from json response in a map object which is wrapped in a List object.
(c) Create a JDBC connection to the database
(d) Iterate map object by for loop and stored data in string objects
(e) Get data from database using ResultSet object and assert the data with string data that we got from list.
(f) Finally, close the database connection.



Points to Consider
  • Always ensure you are pointing to the same environment (API and DB should be connected to same Test/Stage DB).
  • Handle null values, trimming spaces, and data type mismatches while comparing.
  • Validate not just single records, but also multiple records if the API returns a list.
  • For performance testing, check if the response time of the API aligns with fetching from DB.

Suggested Posts:

1. Validate XML Schema in RestAssured
2. Validate JSON Schema in RestAssured
3. Extract Response by JSONPath in RestAssured
4. Validate Response by Matchers API in RestAssured
5. How to Test SOAP API in RestAssured

How to validate JSON Schema in Rest Assured

 



What is JSON Schema?

  • JSON Schema is a specification that defines the structure of a JSON document.
  • It is used to validate that a given JSON response (or request) follows a predefined format.

A JSON Schema defines:

  • Data types (string, number, object, array, boolean, null)
  • Required vs optional fields
  • Field constraints (min/max length, patterns, ranges, enum values, etc.)
  • Nested objects and arrays

Why validate JSON Schema?
  • To check that API responses always follow the contract agreed between client and server.
  • To avoid breaking changes when backend updates occur.
  • To make automated testing more robust and reliable.

How to validate JSON Schema in Rest Assured

Rest Assured provides built-in support for JSON Schema validation using the json-schema-validator library.

This includes:

1. Prepare a JSON Schema file
  • Create a .json file that defines the expected schema of the API response.
  • Example: define required fields, their data types, and structure.

2. Add JSON Schema Validator dependency
  • Rest Assured integrates with the json-schema-validator library, which enables schema-based validation.

3. Call the API using Rest Assured
  • Perform a request (GET/POST/etc.) to get the API response.

4. Validate Response against JSON Schema
  • Use Rest Assured’s built-in schema validator methods to check whether the actual JSON response matches the schema.
  • If the response matches → test passes.
  • If not → test fails with details about what part of the schema is violated.

In short:

To validate a JSON schema using Rest Assured in a GET API response, you can use the JsonSchemaValidator provided by Rest Assured. It helps ensure that the structure of the JSON returned from the API matches an expected schema.










Steps to Use JSON Schema Validation in Rest Assured


1. Add Dependencies

<dependencies>
    <!-- Rest Assured -->
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>5.4.0</version>
        <scope>test</scope>
    </dependency>

    <!-- JSON Schema Validator -->
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>json-schema-validator</artifactId>
        <version>5.4.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>


Just visit this page to create JSON schema or use existing below schema for API:
https://reqres.in/api/users?page=2



2. Create JSON Schema File

We can create schema from any json schema creator website. One of the best website is: http://json-schema.org/




















Create a json schema file named as users-schema.json for GET API: https://reqres.in/api/users?page=2 and placed in the src/test/resources directory.

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "page": { "type": "integer" },
    "per_page": { "type": "integer" },
    "total": { "type": "integer" },
    "total_pages": { "type": "integer" },
    "data": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "id": { "type": "integer" },
          "email": { "type": "string" },
          "first_name": { "type": "string" },
          "last_name": { "type": "string" },
          "avatar": { "type": "string" }
        },
        "required": ["id", "email", "first_name", "last_name", "avatar"]
      }
    },
    "support": {
      "type": "object",
      "properties": {
        "url": { "type": "string" },
        "text": { "type": "string" }
      },
      "required": ["url", "text"]
    }
  },
  "required": ["page", "per_page", "total", "total_pages", "data", "support"]
}




3. Java Code to Validate Schema

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;
import static io.restassured.RestAssured.*;

public class JsonSchemaValidationTest {

    public static void main(String[] args) {

        RestAssured.baseURI = "https://reqres.in";

        given()
            .contentType(ContentType.JSON)
        .when()
            .get("/api/users?page=2")
        .then()
            .assertThat()
            .statusCode(200)
            .body(matchesJsonSchemaInClasspath("users-schema.json"));

        System.out.println("Schema validated successfully.");
    }
}



Code explanation:

(a) Define the base URI
(b) Send request for GET API as mentioned in code and get response
(c) validate response from JSON schema validator by this method matchesJsonSchemaInClasspath("users-schema.json")


Suggested Posts:

1. Test Preemptive Authentication in RestAssured
2. Test Form Authentication in RestAssured
3. Test DELETE API in RestAssured
4. Validate Response by Matchers API in RestAssured
5. Validate API Response from Database in RestAssured