Monday 12 January 2015

Zip Utility file (Generic Function) in Selenium Webdriver.

To ZIP the Junit HTML reports in Selenium Webdriver.

The Junit HTML reports are created in a folder. To zip it and send an email follow the steps below.

1. Create a utility class for zipping all files. I have added the zip utility in the generic utilities file.
  */
   
    /**
     *
     * Method is for adding file to ZIP. These methods can zip the contents inside a source folder as a single zip
     * file, into a destination folder.
     */
   
    public static boolean addFileToZip(String path, String srcFile, ZipOutputStream zip) throws Exception {
        try {
            File folder = new File(srcFile);
            if (folder.isDirectory()) {
                addFolderToZip(path, srcFile, zip);
            } else {
                byte[] buf = new byte[BUFFER];
                int len;
                FileInputStream in = new FileInputStream(srcFile);
                zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
                while ((len = in.read(buf)) > 0) {
                    zip.write(buf, 0, len);
                    System.out.println("Zip");
                }
            }
            return true;
        } catch (Exception e) {
            //log.debug("Exception in zipping files" + e);
            return false;
        }
    }
   
   
    public static boolean addFolderToZip(String path, String srcFolder, ZipOutputStream zip) throws Exception {
        boolean areFilesZipped = false;
        File folder = new File(srcFolder);
        for (String fileName : folder.list()) {
            if (path.equals("")) {
                areFilesZipped = addFileToZip(folder.getName(), "D:\\TestReport" + "/" , zip);
            } else {
                areFilesZipped = addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip);
            }
        }
        return areFilesZipped;
    }
   
   
    /*
     *
     * These methods can zip the contents inside a souce folder as a single zip
     * file, into a destination folder.
     */
    public static boolean archiveFiles(String srcFolder, String destZipFile, String zipFilename) throws Exception {
        ZipOutputStream zip = null;
        FileOutputStream fileWriter = null;
        String destinationFile = destZipFile + "\\" + zipFilename + ".zip";
        fileWriter = new FileOutputStream(destinationFile);
        zip = new ZipOutputStream(fileWriter);
        boolean areFilesZipped = addFolderToZip("", srcFolder, zip);
        zip.flush();
        zip.close();
        return areFilesZipped;
    }

2. Now just reuse the archiveFiles method to zip the folder as shown below. Provide the source and destination folder and the destination zipfile name.


     try {
            TestUtil.archiveFiles("D:\\TestReport", "D:\\AutoTestReport", "TestAutomationReport");
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

In the destination folder, a zip file will be created with name  “TestAutomationReport.zip”.

 


No comments:

Post a Comment