ankush singh
ankush singh

Reputation: 39

How to wait till file is getting downloaded or available in required folder?

On clicking the download button a file will be downloaded in my project dir and I want to read that file content this is pdf

File name is dynamic. It starts with same name but in middle the content is dynamic and ends with .pdf extension.

Code:

   public static String isFileDownloaded(String fileText, String fileExtension, int timeOut) {
String folderName = System.getProperty("user.dir")+"\\src\\test\\resources\\Download\\";
File[] listOfFiles;
int waitTillSeconds = timeOut;
boolean fileDownloaded = false;
String filePath = null; 

long waitTillTime = Instant.now().getEpochSecond() + waitTillSeconds;
while (Instant.now().getEpochSecond() < waitTillTime) {
    listOfFiles = new File(folderName).listFiles();
    for (File file : listOfFiles) {
        String fileName = file.getName().toLowerCase();
        if (fileName.contains(fileText.toLowerCase()) && fileName.contains(fileExtension.toLowerCase())) {
            fileDownloaded = true;
            filePath = fileName.getAbsolutePath();
            break;
        }
    }
    if (fileDownloaded) {
        break;
    }
}
return filePath;

}

public static  String readPdfContent(String fileName) throws IOException {
    
    File file = new File(System.getProperty(fileName));
    PDDocument doc = PDDocument.load(file);
    int numberOfPages = getPageCount(doc);
    System.out.println("The total number of pages "+numberOfPages);
    String content = new PDFTextStripper().getText(doc);
    doc.close();    
return content;

}

try {
        JSclick(download);
        String filePath = isFileDownloaded("Personal Data", ".pdf", 30);
        if(filePath != null){
        String pdfContent = readPdfContent(filePath);
        Assert.assertTrue(pdfContent.contains("Test Kumar"));
        Assert.assertTrue(pdfContent.contains("XXXXX"));
        }else {
             System.out.println("Some issue with file downloading");
        }
       
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

file

it show me error-:The method getAbsolutePath() is undefined for the type String Can you please help on this?

Upvotes: 0

Views: 2787

Answers (2)

Nandan A
Nandan A

Reputation: 2922

  • Create a method to check whether a file is fully downloaded or not.
  • Call that method wherever it's required.

Method to check file is downloaded or not: This method is to search for specific file with partial or full text in download folder for given time and returns the absolute path of the file.

@param fileText - Partial or full file name
@param fileExtension - .pdf, .txt
@param timeOut - How many seconds you are expecting for file to be downloaded.
    public static String isFileDownloaded(String fileText, String fileExtension, int timeOut) {
        String folderName = "location of download folde";
        File[] listOfFiles;
        int waitTillSeconds = timeOut;
        boolean fileDownloaded = false;
        String filePath = null; 

        long waitTillTime = Instant.now().getEpochSecond() + waitTillSeconds;
        while (Instant.now().getEpochSecond() < waitTillTime) {
            listOfFiles = new File(folderName).listFiles();
            for (File file : listOfFiles) {
                String fileName = file.getName().toLowerCase();
                if (fileName.contains(fileText.toLowerCase()) && fileName.contains(fileExtension.toLowerCase())) {
                    fileDownloaded = true;
                    filePath = file.getAbsolutePath();
                    break;
                }
            }
            if (fileDownloaded) {
                break;
            }
        }
        return filePath;
    }

Call the isFileDownloaded method:

As you know the partial file name then you can pass the input like below to get the file path.

String filePath = isFileDownloaded("Personal Data", ".pdf", 30);
System.out.println("Complete path of file:"+ filePath);

Output:

C:\Users\Download\Personal data...pdf

Updated code for OP's logic:

Try block:

    try {
        JSclick(download);
        String filePath = isFileDownloaded("Personal Data", ".pdf", 30);
        if(filePath != null){
        String pdfContent = readPdfContent(filePath);
        Assert.assertTrue(pdfContent.contains("Test Kumar"));
        Assert.assertTrue(pdfContent.contains("XXXXX"));
        }
       }else{
         System.out.println("Some issue with file downloading");
       }

Read pdf content method:

public static  String readPdfContent(String fileName) throws IOException {
        
        File file = new File(System.getProperty(fileName);    
        PDDocument doc = PDDocument.load(file);
        int numberOfPages = getPageCount(doc);
        System.out.println("The total number of pages "+numberOfPages);
        String content = new PDFTextStripper().getText(doc);
        doc.close();
    return content;
}

Upvotes: 4

Kozubi
Kozubi

Reputation: 539

Well you can simply list all files in folder and take the newest

Like something:

File f = new File("download_folder");
String[] pathnames = f.list();

then you can use for-loop to find what you need

Upvotes: 0

Related Questions