我在用Chrome。单击按钮时,它正在下载“下载”文件夹中的文件(没有任何下载窗口弹出,否则我也可以尝试使用AutoIT工具)。现在,我需要验证文件是否下载成功。稍后,我需要验证该文件的内容。文件的内容应该与GUI上的内容相匹配。
发布于 2020-12-02 08:52:07
如果存在program.txt文件,下面的代码行返回true或false:
File f = new File("F:\\program.txt");
f.exists();您可以使用这个内部自定义的预期condition:##来等待文件下载和显示。
使用:
进口java.io.File;
在任何pageobject类中定义方法
public ExpectedCondition<Boolean> filepresent() {
return new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
File f = new File("F:\\program.txt");
return f.exists();
}
@Override
public String toString() {
return String.format("file to be present within the time specified");
}
};
}我们放弃了一个自定义的预期条件方法,现在将它用作:
在测试代码中,请稍候如下:
wait.until(pageobject.filepresent());输出:
失败:

通过了

发布于 2020-12-02 08:56:50
public static boolean isFileDownloaded(String downloadPath, String fileName) {
File dir = new File(downloadPath);
File[] dir_contents = dir.listFiles();
if (dir_contents != null) {
for (File dir_content : dir_contents) {
if (dir_content.getName().equals(fileName))
return true;
}
}
return false;
}您应该在此方法中提供要检查的fileName (下载与否),以及下载应该找到可以使用的下载路径的路径:
public static String getDownloadsPath() {
String downloadPath = System.getProperty("user.home");
File file = new File(downloadPath + "/Downloads/");
return file.getAbsolutePath();
}发布于 2022-10-19 10:22:17
public boolean isFileDownloaded(String filename) throws IOException
{
String downloadPath = System.getProperty("user.home");
File file = new File(downloadPath + "/Downloads/"+ filename);
boolean flag = (file.exists()) ? true : false ;
return flag;
}https://stackoverflow.com/questions/65104816
复制相似问题