In this article, we will look to verify the CSV Export function. This export function is exporting a document or downloading a document with the number of records or data to verify. Usually the exported document will be in the form of a csv file. We will now read the csv file with the help of Java FileReader and LineNumberReader
Export button will be set to export the records / data to the local system from the application. If you look at the image below, you will see the different options for exporting available data in the table.
We need to follow the steps below to verify the CSV export function
Step 1 : – First take the number of items the table has.
Step 2 : – Click the Export button (CSV in the example case below).
Step 3 : – Download the document to a specific folder or the default download folder.
Step 4 : – Read csv file and get the number of items in Csv (Need to exclude titles when comparing items)
Step 5 : – Compare items in table and number of items in csv file.
Take a look at the example below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | package com.pack; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.LineNumberReader; 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"; @BeforeClass public void testSetup() throws Exception{ driver = new FirefoxDriver(firefoxProfile()); driver.manage().window().maximize(); } public static FirefoxProfile firefoxProfile() throws Exception { FirefoxProfile firefoxProfile = new FirefoxProfile(); firefoxProfile.setPreference("browser.download.folderList",2); firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false); firefoxProfile.setPreference("browser.download.dir",downloadPath); firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml"); return firefoxProfile; } @Test public void testExportAllRecords() throws InterruptedException { driver.get(URL); 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')]")); elementCSV.click(); 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)); } |
The method below is used to get the number of items in the table as shown in the image above.
1 2 3 4 5 6 7 8 | public int getNumberOfEntries() { String entriesTxt = driver.findElement(By.id("example_info")).getText().trim(); String[] aEntriesText = entriesTxt.split(" "); String totalEntriesText = aEntriesText[aEntriesText.length-2]; return Integer.parseInt(totalEntriesText); } |
Here’s the method to read the CSV file and get the number of items contained in the exported csv file. It takes the file name as a parameter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | public int getRecordsCountInCSV(String downloadPath, String csvFileName) { int lineNumberCount = 0; try { if (!csvFileName.isEmpty() || csvFileName != null) { String filePath = downloadPath + System.getProperty("file.separator") + csvFileName; System.out.println(filePath); 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) { lineNumberCount++; } //To remove the header lineNumberCount=lineNumberCount-1; System.out.println("Total number of lines found in csv : " + (lineNumberCount)); linenumberreader.close(); } else { System.out.println("File does not exists"); } } } catch (IOException e) { e.printStackTrace(); } return lineNumberCount; } |
The method below is used to get the latest file from the directory. It takes the directory path as a parameter and returns the recently added file to the directory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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 this article will help you!
Reference link: https://www.seleniumeasy.com/selenium-tutorials/verify-entries-in-exported-csv-file-using-webdriver-java