How to verify entries in Exported CSV file

In this article we will look into verify Export functionality. Export functionality is exporting a document/downloading a document which has number of records / data which need to be verified. Generally exported document will be in the form of csv file. We will now read csv file with the help of Java FileReader and LineNumberReader

Export button will be placed to export the records/data to our local system from the application. If you observe the below image, you will see different options to export the data which is available in table.

Export csv file webdriver

We need to follow the below steps to verify the export functionality.

Step 1:- First get the number of entries that the table has.
Step 2:- Click on Export button (CSV in the case of below example).
Step 3:- Download the document to a specific folder or default downloads folder.
Step 4:- Read the csv file and get number of entries in Csv (Need to exclude the header when comparing entries)
Step 5:- Compare the entries in the table and number of entries in the csv file.

Now we will see the below example:-

package com.pack;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class ExportExample {
WebDriver driver;
	private static String downloadPath = "D:\\seleniumdownloads";
	private String URL="appURL"; 
	public void testSetup() throws Exception{
		driver = new FirefoxDriver(firefoxProfile());	
	public static FirefoxProfile firefoxProfile() throws Exception {
		FirefoxProfile firefoxProfile = new FirefoxProfile();
		return firefoxProfile;
	public void testExportAllRecords() throws InterruptedException {
		int noOfEntries = getNumberOfEntries();
		System.out.println("Total number of entries are :- "+noOfEntries);

		WebElement elementCSV = driver.findElement(By.xpath(".//*[@id='ToolTables_example_1']/span[contains(text(),'CSV')]"));;

		File file = getLatestFilefromDir(downloadPath);
		String csvFileName = file.getName();
		System.out.println("CSV File Downloaded is :- "+csvFileName);

		System.out.println("Verifying number of entries with number of entries in csv");
		Assert.assertEquals(noOfEntries, getRecordsCountInCSV(downloadPath,csvFileName));

Below method is used to get the number of entries in the table as shown in the above image.

	public int getNumberOfEntries() {
		String entriesTxt = driver.findElement("example_info")).getText().trim();
		String[] aEntriesText = entriesTxt.split(" ");
		String totalEntriesText = aEntriesText[aEntriesText.length-2];
		return Integer.parseInt(totalEntriesText);

Below is the method to read the CSV file and get the number of entries present in the exported csv file. It takes the file name as the parameter

	public int getRecordsCountInCSV(String downloadPath, String csvFileName) {
		int lineNumberCount = 0;
		try {
			if (!csvFileName.isEmpty() || csvFileName != null) {
				String filePath =	downloadPath + System.getProperty("file.separator") + csvFileName;
				File file = new File(filePath);
				if (file.exists()) {
					System.out.println("File found :" +csvFileName);
					FileReader fr = new FileReader(file);
					LineNumberReader linenumberreader = new LineNumberReader(fr);
					while (linenumberreader.readLine() != null) {
					//To remove the header
					System.out.println("Total number of lines found in csv : " + (lineNumberCount));
				} else {
					System.out.println("File does not exists");
		catch (IOException e) {
		return lineNumberCount;

Below method is used to get the latest file from the directory. It takes the folder path as the parameter and returns the file which is recently added to the folder.

	private File getLatestFilefromDir(String dirPath){
	    File dir = new File(dirPath);
	    File[] files = dir.listFiles();
	    if (files == null || files.length == 0) {
	        return null;
	    File lastModifiedFile = files[0];
	    for (int i = 1; i < files.length; i++) {
	       if (lastModifiedFile.lastModified() < files[i].lastModified()) {
	           lastModifiedFile = files[i];
	    return lastModifiedFile;

Hope the article helped you. Please fell free to comment if need more information.

Selenium Tutorials: 


Above is an awesome example to read csv files. but I think we can use selenium Datasource API for CSV Reading purpose nowadays.

Add new comment