I am click the download button to download my file in my project dir and I want to read that file content this is pdf
starting of filename is same but end pf the name is dynamic it changes every time when I click download
what I am doing:
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 for this
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
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 boolean result.
@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 boolean isFileDownloaded(String fileText, String fileExtension, int timeOut) {
String folderName = "location of download folde";
File[] listOfFiles;
int waitTillSeconds = timeOut;
long passedTimeInSeconds = 0;
boolean fileDownloaded = false;
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;
break;
}
}
if (fileDownloaded) {
break;
}
}
return fileDownloaded;
}
Call the isFileDownloaded
method:
if (!isFileDownloaded(fileName, ".pdf", 30)) {
System.out.println("File is not downloaded. Expected file is " fileName ".pdf");
}else{
System.out.println("File is downloaded.);
}