Issue
I've a below scenario in selenium with Java that needs to be automated.
On a webpage , after giving input values there is an export to Excel button. After clicking on the export button I need to wait till the export is complete. My browser shouldn't be closed unless and until the export is completed. Note: After export completes , there is no message that will be displayed so not sure how to handle it.
Please help with the logic for above.
Solution
If you know the download location, and file path in advance you can use the below method,
public boolean isFileDownloaded(String downloadPath, String fileName) {
File dir = new File(downloadPath);
File[] dirContents = dir.listFiles();
for (int i = 0; i < dirContents.length; i++) {
if (dirContents[i].getName().equals(fileName)) {
// File has been found
return true;
}
}
return false;
}
the above method should return a boolean based on file presence in your folder.
so you can have a logic like if this method return true, which means file is there so now you can close the browser session. if it returns false, then do not close the session and wait for sometime and then check again.
Update :
boolean check = CommonMethods.isFileDownloaded(System.getProperty("user.dir") + "\\exportedFiles", "My file name here");
while(true){
Thread.sleep(1000);
check = CommonMethods.isFileDownloaded(System.getProperty("user.dir") + "\\exportedFiles", "My file name here");
if(check) {
System.out.println("File available");
break;
}
}
Answered By - cruisepandey
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.