How to Fetch Credentials from Excel File to Test Login Functionality?

In software testing, the login functionality of an application is an important part to ensure the security and usability of the system. One common practice in automated testing is to store the login credentials directly in the test code. However, this approach can lead to several challenges and potential risks.

In this blog post, we will discuss the importance of fetching credentials from an Excel file instead of embedding them into the code.

Drawbacks of Hardcoding Credentials in Code

When testing the login functionality of software applications, it is common for testers to directly include the login credentials in the test code. While this may seem convenient, but it has following drawbacks:

  1. Difficulty in Maintaining the Code:
    Hardcoding credentials in the test code can make it challenging to maintain. If there are multiple test cases or different sets of credentials to test, any changes to the login information would require modifying the code for each test case individually. This process can be time-consuming, prone to errors, and cumbersome, especially when the credentials need frequent updates. If the code is shared among team members or stored in a version control system, coordinating these changes becomes even more complicated.
  2. Security Risks:
    Hardcoding credentials in the code poses security risks. When the login credentials are directly included in the code, they become visible to anyone who has access to the codebase. This includes not only authorized team members but potentially unauthorized individuals as well. If the code is publicly accessible or stored in a public repository, the credentials become easily accessible to anyone who can view the code. This exposure of sensitive information puts the application and its users at risk of data breaches, unauthorized access, and malicious activities.
  3. Lack of Flexibility:
    Including credentials directly in the code restricts flexibility in the testing process. If the login information needs to be changed or updated, it requires modifying and recompiling the code, which can be time-consuming and prone to mistakes. This lack of flexibility becomes problematic when dealing with scenarios that involve testing different sets of credentials or when frequent updates to the credentials are required. It affects productivity and makes the testing process less efficient overall.
  4. Limited Scalability:
    Hardcoding credentials limits the scalability of the testing process. As the application grows and evolves, the number of test cases and associated credentials may increase significantly. Including all these credentials directly in the code makes it difficult to manage and scale the testing efforts. It becomes increasingly challenging to keep track of which credentials are used in which test cases and to make changes when necessary.

Benefits of Fetching Credentials from Excel

When testing the login functionality of an application, using an Excel file to fetch credentials offers several important advantages compared to hardcoding them in the code. Following are some of the Benefits:

  1. More Flexibility:
    By storing login credentials in an Excel file instead of the code, testers gain more flexibility. This is especially useful when there are different sets of credentials to test or when credentials need frequent updates. Instead of changing the code every time, testers can easily update the Excel file. This saves time and makes the testing process more efficient.

    For example, imagine an application with different types of users, like admins, regular users, and guests. With an Excel file, you can keep all these sets of credentials separate in different rows or sheets. This allows for thorough testing of various login scenarios without needing to modify the code.
  2. Improved Security:
    Security is crucial when dealing with login credentials. Hardcoding them in the code can expose them to potential risks. If the code is shared or stored in a version control system, unauthorized people may access the credentials, putting the application and its users at risk.

    Using an external Excel file adds an extra layer of security. The file can be kept in a secure location with limited access, ensuring only authorized individuals can view and modify the credentials. This separation between the code and the credentials minimizes the chances of unauthorized access and helps protect sensitive information.

How to Fetch Credential from Excel File

For reading credentials from an Excel file, you can use the Apache POI library. This library provides extensive support for manipulating Microsoft Office documents, including Excel spreadsheets.
For using Apache POI make sure you have the Apache POI library added to your project. You can download it from the Apache POI website (poi.apache.org) or include it as a dependency in your build tool (e.g., Maven or Gradle).

Once you have set up the environment and have the necessary libraries in place, follow these steps to read credentials from an Excel file:

1: Import Required Classes:

At the top of your Java file, import the necessary classes from the Apache POI library:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.IOException;

2: Create FileInputStream:

Create a FileInputStream object by providing the path to the Excel file you want to read.

FileInputStream file = new FileInputStream("path/to/excel/file.xlsx");

3: Create Workbook and Sheet Objects:

Create a Workbook object using the XSSFWorkbook class, passing the FileInputStream as a parameter and get the desired sheet from the workbook using the getSheet() method.

Workbook workbook = new XSSFWorkbook(file);
Sheet sheet = workbook.getSheet("Sheet1");

4. Access the Row and Cell Values:

To read the credentials, you need to identify the specific row and cells that contain the username and password.

Row row = sheet.getRow(1);
Cell usernameCell = row.getCell(0);
Cell passwordCell = row.getCell(1);

5. Extract the Values:

Once you have the cell objects, you can extract the username and password values using the getStringCellValue() methods.

String username = usernameCell.getStringCellValue();
String password = passwordCell.getStringCellValue();

Practical example of Fetching credentials from Excel File and perform Login Functionality

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.FileInputStream;
import java.io.IOException;

public class Excelcredentiallogin {
    public static void main(String[] args) {
        // Initialize ChromeDriver
        WebDriver driver = new ChromeDriver();

        // Open the login page of the website
        driver.get("https://www.example.com/login");

        try {
            FileInputStream file = new FileInputStream("path/to/excel/file.xlsx");
            Workbook workbook = new XSSFWorkbook(file);
            Sheet sheet = workbook.getSheet("Sheet1");

            // Iterate through each row to fetch credentials
            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);
                Cell usernameCell = row.getCell(0);
                Cell passwordCell = row.getCell(1);
                Cell roleCell = row.getCell(2);

                String username = usernameCell.getStringCellValue();
                String password = passwordCell.getStringCellValue();
                String role = roleCell.getStringCellValue();

                // Fetch the login elements
                WebElement usernameInput = driver.findElement(By.id("username"));
                WebElement passwordInput = driver.findElement(By.id("password"));
                WebElement loginButton = driver.findElement(By.id("login-button"));

                // Enter the username and password
                usernameInput.sendKeys(username);
                passwordInput.sendKeys(password);

                // Click on the login button
                loginButton.click();

                // Perform successful login assertion based on the role
                WebElement welcomeMessage = driver.findElement(By.id("welcome-message"));
                if (welcomeMessage.getText().contains("Successfully logged in")) {
                    System.out.println("Successful login with " + role + " credentials");
                } else {
                    System.out.println("Login failed with " + role + " credentials");
                }

                // Logout from the current role
                WebElement logoutButton = driver.findElement(By.id("logout-button"));
                logoutButton.click();

                // Clear the input fields for the next set of credentials
                usernameInput.clear();
                passwordInput.clear();
            }

            workbook.close();
            file.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Close the browser
        driver.quit();
    }
}