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

Time:09-29

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

filename

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

Code:

    try {
            JSclick(download);
            Thread.sleep(4000);
            String pdfContent = readPdfContent();
            Assert.assertTrue(pdfContent.contains("Test Kumar"));
            Assert.assertTrue(pdfContent.contains("XXXXX"));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    public static  String readPdfContent() throws IOException {
        File File=new File(Filepath);
        fileName=File.getAbsolutePath().substring(File.getAbsolutePath().lastIndexOf("\\") 1);
        System.out.println(fileName);
        
        
        File file = new File(System.getProperty("user.dir") "\\src\\test\\resources\\Download\\" 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;
}

static String Filepath=System.getProperty("user.dir") "\\src\\test\\resources\\Download\\";

Can you please help on this?

CodePudding user response:

  • 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;
        long passedTimeInSeconds = 0;
        boolean fileDownloaded = false;
        String filePath = null; 

        long waitTillTime = Instant.now().getEpochSecond()   waitTillSeconds;
        while (passedTimeInSeconds < 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;
    }

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

CodePudding user response:

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

  • Related