2

I have a task to click on "Generate PDF", then automate the PDF to see if the data is present as expected. However, I am not able to select last generated PDF.... it takes only the first pdf file. Every time when i click on "Generate PDF" file gets generated --------> example: ("file:///home/tifoso/Downloads/Search by Criteria.pdf"),("file:///home/tifoso/Downloads/Search by Criteria(1).pdf".....etc)

My code so far:

public void readPDF_ProvidersList() throws IOException {
        System.out.println("_______Testing PDF file_______");
        URL url = new URL("file:///home/tifoso/Downloads/Provider List.pdf");
        InputStream is = url.openStream();
        BufferedInputStream fileParse = new BufferedInputStream(is);
        PDDocument document = null;
        document = PDDocument.load(fileParse);
        String pdfContent = new PDFTextStripper().getText(document);
        System.out.print(pdfContent);

        Assert.assertTrue(pdfContent.contains("License/Provider"));
        Assert.assertTrue(pdfContent.contains("Address"));
        Assert.assertTrue(pdfContent.contains("Phone"));
        Assert.assertTrue(pdfContent.contains("Email"));
        Assert.assertTrue(pdfContent.contains("Gender"));
        Assert.assertTrue(pdfContent.contains("Ethnicity"));
        Assert.assertTrue(pdfContent.contains("Highly"));
        Assert.assertTrue(pdfContent.contains("Recommended"));
        Assert.assertTrue(pdfContent.contains("Metro"));
    } 

How do i specify in my code that i want system to use the latest generated PDF? It is dynamic and will change every time i will click "Generate PDF"

tifoso
  • 58
  • 6
  • There is a way to know files, maybe name, time download or similar. Try to find answer https://stackoverflow.com/questions/34548041/selenium-give-file-name-when-downloading/56570364#56570364 – Gaj Julije Nov 24 '20 at 22:22
  • https://stackoverflow.com/questions/1179970/how-to-find-the-most-recent-file-in-a-directory-using-net-and-without-looping This can help you – SeleniumTech Nov 25 '20 at 05:04
  • Thank you i will look into it, i added a line to delete file however, it does not delete.... can you tell me what could be the reason? – tifoso Nov 25 '20 at 18:33
  • I would like to share the code that I wrote. Maybe it will help someone. public void deleteFile(String value) { try { String fileName = (value); File file = new File(fileName); if (file.exists()) { System.out.println("File is present"); file.delete(); System.out.println(file.getName() + " File was deleted"); } else { System.out.println("Delete operation is failed"); } } catch (Exception e) { e.printStackTrace(); } } – tifoso Dec 18 '20 at 21:27

0 Answers0