On page layer, define download and compare method:
public List<String> downloadReport(int index){
WebElement locator = driver.findElement(By.cssSelector("a[title='Download']"));
String url = locator.getAttribute("href");
log.info("Download URL: " + url);
//Download CSV
//DownloadUtils - Self defination on common class layer to do download file
String file = DownloadUtils.download(driver, url);
log.info("Download: " + file);
log.info(file.substring(0,22));
//upzip csv
List<String> files = FileUtils.unZip(file, file.substring(0,22));
for(String s: files){
log.info(s);
}
return files;
}
public void compareCsvContent(String actual_file, String except_file) throws Exception{
//File Uilts - self define common class to read/write files
List<String> actuallyFile = FileUtils.readCsv(new File(actual_file));
List<String> exceptFile = FileUtils.readCsv(new File(except_file));
log.info("Compare File: " +actual_file);
log.info("actuallyFile.size(): " + actuallyFile.size());
log.info("exceptFile.size(): " + exceptFile.size());
//compare file except the first row.
for(int i=1; i<exceptFile.size(); i++){
log.info("I = " + i);
Assert.assertEquals(actuallyFile.get(i), exceptFile.get(i));
}
log.info("Assert CSV content is same with templete");
}
public String getFirstRowForDownloadFile(String file) throws Exception{
List<String> actuallyFile = FileUtils.readCsv(new File(file));
String content = actuallyFile.get(0);
log.info("File: " + file);
log.info("First row: " + content);
return content;
}
On Test case layer to use it:
@Test(retryAnalyzer = TestngRetry.class)
public void testDownloadFile() throws Exception {
log.info("Test case(27.2): - Download");
DataGridForExtractData data = new DataGridForExtractData();
List<String> files2= data.downloadReport(1);
log.info("File: " +files2.get(0));
Assert.assertTrue(files2.get(0).contains(except_file));
data.compareCsvContent(files2.get(0), except_file);
//Assert first row
String firstRow= data.getFirstRowForDownloadFile(files2.get(0));
Assert.assertTrue(except_file.contains(firstRow)
}