简体   繁体   English

关闭selenium中的web驱动前如何验证文件下载是否完成?

[英]How to verify whether file downloading is completed before closing the web driver in selenium?

How to verify whether file downloading is completed before closing my Selenium web driver in JAVA.在JAVA中关闭我的Selenium web驱动之前如何验证文件下载是否完成。

I have written a Selenium code to download 2 files to my desired folder location.我编写了一个 Selenium 代码来将 2 个文件下载到我想要的文件夹位置。 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.但是,我在单击 2 个链接后立即关闭了浏览器,这将下载的文件作为临时文件或扩展名无效。 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.在关闭 web 驱动程序之前单击两个链接中的每一个后,我使用了 Thread.sleep 方法,现在它工作正常。

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.我需要知道在使用显式方法或任何其他方式关闭 web 驱动程序之前是否有最佳方法来检查下载是否完成,而不是使用 Thread.sleep() 方法设置预定义时间。

Here is part of the source code (JAVA, Selenium and Testng) which is relevant to this question.这是与此问题相关的部分源代码(JAVA、Selenium 和 Testng)。

  // 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();

     }

There is a piece of code I was using to download files.有一段代码我用来下载文件。 Just change fileName and increase timeout in waitSec(driver, int).只需更改文件名并增加 waitSec(driver, int) 中的超时。

public WebDriverWait waitSec(WebDriver driver, int sec) {
      return new WebDriverWait(driver, sec);
}
String fileName = "foo";
String filePathFull = "C:/users/user/downloads/" + fileName  + ".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.始终存在 a.part 文件,并且在下载完成之前,原始文件(在我的示例中为 csv)大小为零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM