Data-Driven Testing in Rest Assured Using Data Provider - Notes By ShariqSP

Data-Driven Testing in Rest Assured Using Data Provider

Data-driven testing is a methodology where test data is read from external sources like Excel files, databases, or CSV files to drive the tests. This approach is particularly useful for testing APIs with varying input data without modifying the test scripts. Below, we demonstrate how to implement data-driven testing in Rest Assured using a Data Provider in TestNG, leveraging an Excel sheet for user credentials.

Base URL

The base URL for our API testing is http://apitesting.shariqsp.com:8080/. This will be used to interact with two endpoints:

  • POST /api/admin/add - To add a new admin using data from the Excel sheet.
  • POST /api/admin/login - To log in as an admin, using Base64 encoded credentials.

Steps to Implement

  1. Prepare Test Data: Download the user data Excel sheet from this link.
  2. Read Data from Excel: Use the Apache POI library to read the usernames and passwords from the provided Excel sheet.
  3. Configure Data Provider: Use a TestNG Data Provider to supply the test data to your Rest Assured test methods.
  4. Write Rest Assured Tests: Implement tests for both adding an admin and logging in using the data fetched from the Excel sheet.

Code Example

                    
            // Import necessary libraries
            import io.restassured.RestAssured;
            import io.restassured.response.Response;
            import org.apache.poi.ss.usermodel.*;
            import org.apache.poi.xssf.usermodel.XSSFWorkbook;
            import org.testng.annotations.DataProvider;
            import org.testng.annotations.Test;
            
            import java.io.FileInputStream;
            import java.io.IOException;
            
            public class AdminApiTests {
            
                // Set Base URI
                static {
                    RestAssured.baseURI = "http://apitesting.shariqsp.com:8080/";
                }
            
                // Data Provider to fetch data from Excel
                @DataProvider(name = "AdminData")
                public Object[][] getAdminData() throws IOException {
                    String filePath = "path/to/realtime_user_credentials.xlsx"; // Update path as needed
                    FileInputStream file = new FileInputStream(filePath);
                    Workbook workbook = new XSSFWorkbook(file);
                    Sheet sheet = workbook.getSheet("user");
                    int rowCount = sheet.getLastRowNum();
            
                    Object[][] data = new Object[rowCount][2]; // 2 columns: Username and Password
                    for (int i = 1; i <= rowCount; i++) {
                        Row row = sheet.getRow(i);
                        data[i - 1][0] = row.getCell(0).getStringCellValue(); // Username
                        data[i - 1][1] = row.getCell(1).getStringCellValue(); // Password
                    }
            
                    workbook.close();
                    return data;
                }
            
               
    // Test for Adding Admin
    @Test(dataProvider = "AdminData")
    public void testAddAdmin(String username, String password) {
            given()
            .header("Content-Type", "application/json")
            .body("{\"username\": \"" + username + "\", \"password\": \"" + password + "\"}")
            .post("http://apitesting.shariqsp.com:8080/api/admin/add")
		    .then()
		    .assertThat()
		    .statusCode(201)
		    .log()
		    .all();
    }
            
    // Test for Admin Login
    @Test(dataProvider = "AdminData")
    public void testAdminLogin(String username, String password) {
        String encodedCredentials = java.util.Base64.getEncoder().encodeToString((username + ":" + password).getBytes());

        given()
            .header("Authorization", "Basic " + encodedCredentials)
            .post("http://apitesting.shariqsp.com:8080/api/admin/login")
		    .then()
		    .assertThat()
		    .statusCode(200)
		    .log()
		    .all(); // Validate response
    }

    //testfor display admin
    @Test
	 public void displayAdmins()
	 {
		 given()
		 .get("http://apitesting.shariqsp.com:8080/api/admin/all")
		 .then()
		 .log()
		 .all();
	 }
    }
                    
                

Download Test Data

You can download the Excel sheet containing test data from this link.