验证Excel文件是否下载到Selenium Web驱动程序Java中

cu6pst1q  于 2023-06-20  发布在  Java
关注(0)|答案(3)|浏览(119)

我可以通过点击下载按钮下载Excel文件,这是根据DOM,之后,我想验证下载的文件是同一个。项目中不允许使用AUTO IT。我已经尝试在本地验证下面的代码,但如果我将此代码推到仓库。则用户路径将得到改变并且代码将失败。

`String filepath = "C:User\\Dhananjay\\Downloads";

String fileName = "report.xlsx"

File targetFile = new File(fileName,filePath);

if(! targetFile.exists())'
{

system.out.println("File is verified")`
}else{
system.out.println("file not downloaded")
}'
cdmah0mi

cdmah0mi1#

String userProfile = System.getProperty("user.home");返回%USERPROFILE%变量。
可以使用String filepath = System.getProperty("user.home") + "\\Downloads";
即使在Linux上也可以工作。

nlejzf6q

nlejzf6q2#

I have found way to validate on local path and it's generic one

File folder = new File(System.getProperty("user.home") +\\Downloads);
File[] listOfFiles = folder.listFiles();

boolean found = false;

File f = null;
 

for (File listOfFile : listOfFiles) {

 if (listOfFile.isFile()) {
      String fileName = listOfFile.getName();
       System.out.println("File " + listOfFile.getName());
       if (fileName.matches("5MB.zip")) {
           f = new File(fileName);
           found = true;
            }
        }
    }
Assert.assertTrue("Downloaded document is not found",found );

f.deleteOnExit();
mspsb9vt

mspsb9vt3#

您可以根据自己的选择指定Chrome和Firefox浏览器的下载位置。位置可以是通用的操作系统-所以它将在远程和本地和任何操作系统上工作。请check this出来得到解决方案。在后一部分,已经解决了对下载文件的验证问题。

相关问题