如何使用Webdriver等待下载完成 [英] How to wait for download to finish using Webdriver

查看:724
本文介绍了如何使用Webdriver等待下载完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以等待WebDriver中的下载完成吗?

Is there any way to wait for a download to finish in WebDriver?

基本上,我想验证下载的文件是否正确存储在硬盘驱动器中,并且要验证,需要等到下载完成.如果有人较早意识到这种情况,请提供帮助.

Basically, I want to verify that downloaded file getting stored correctly inside hard drive and to verify that, need to wait till download finishes. Please help if anyone aware of such a scenario earlier.

推荐答案

然后,您可以使用之类的库来进行实际的文件处理以查看文件是否已存储正确(这可能意味着比较文件大小,Md5哈希值,甚至检查Tika实际也可以执行的文件内容).

Then you can use a library like this to do the actual file handling to see the file is stored correctly (that could mean comparing file size, Md5 hashes or even checking the content of the file which Tika can actually do as well).

public void fileChecker(){
        waitForFileDownload(20000,"filename_here");

        File file = downloadsDirectory.resolve(expectedFileName).toFile();

        AutoDetectParser parser = new AutoDetectParser();
        parser.setParsers(new HashMap<MediaType, Parser>());

        Metadata metadata = new Metadata();
        metadata.add(TikaMetadataKeys.RESOURCE_NAME_KEY, file.getName());

        try (InputStream stream = new FileInputStream(file)) {
            parser.parse(stream, (ContentHandler) new DefaultHandler(), metadata, new ParseContext());
        }

        String actualHash = metadata.get(HttpHeaders.CONTENT_MD5); 
        assertTrue("There was a hash mismatch for file xyz:",actualHash.equals("expectedHash"));
    }

这篇关于如何使用Webdriver等待下载完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆