Download CSV and compare content

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)
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值