How to verify whether file downloading is completed before closing my Selenium web driver in JAVA.
I have written a Selenium code to download 2 files to my desired folder location. However I close the browser instantly after clicking on the 2 links, which given me the downloaded files as temporary files or with an invalid extension. I used Thread.sleep method after clicking on the each of the two links before closing the web driver and it is now working fine.
I need to know whether there is an optimum method to check whether download is completed or not before closing the web driver using explicit method or any other way rather setting a pre-defined time using the Thread.sleep() method.
Here is part of the source code (JAVA, Selenium and Testng) which is relevant to this question.
// Text file download
@Test(priority=2)
public void txtFileDownloadTest() {
fileDownloadPage.clickTxtFileLink();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// Java file download
@Test(priority=3)
public void javaFileDownloadTest() {
fileDownloadPage.clickJavaFileLink();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@AfterClass
public void setDown() {
closeUp();
}