问题
I can see option on internet to check the file name and verify whether the file is downloaded and the another option is checking the file size to make sure it download completely but is there any other way we can verify the file download completely if that file is increasing daily. The file i am downloading is more than 500 MB and will go on increasing day by day so how can i code in selenium java so it will check the file and make sure it 100% downloaded.
My requirement of automation is to run the automation script and download the file everyday.
Can anyone guide me on it?
回答1:
There is a piece of code I was using to download files. Just change filePathFull
and increase timeout in waitSec(driver, int)
.
public WebDriverWait waitSec(WebDriver driver, int sec) {return new WebDriverWait(driver, sec);}
String filePathFull = stahovani + "exportFeedbacks_" + urlCast1.split("/")[9] + ".csv";
waitSec(driver, 30).until(new Function<WebDriver, Boolean>() {
public Boolean apply(WebDriver driver) {
if(Files.exists(Paths.get(filePathFull))) {
return true;
}
else {
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
}
}
return false;
}
});
File exportFile = new File(filePathFull);
if (Files.size(Paths.get(filePathFull)) == 0) {
try {
waitSec(driver, 120).until(new Function<WebDriver, Boolean>() {
public Boolean apply(WebDriver driver) {
try {
if(Files.size(Paths.get(filePathFull)) > 0) {
return true;
}
else {
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
}
}
}
catch (IOException e) {
}
return false;
}
});
}
catch (TimeoutException e) {
}
}
There is always a .part file and until download is complete orginial file (csv in my example) has zero size.
来源:https://stackoverflow.com/questions/58717512/how-to-verify-file-download-completely-in-selenium-java-if-that-file-will-go-on