如何使用Webdriver等待下载完成

编程入门 行业动态 更新时间:2024-10-07 04:24:27
本文介绍了如何使用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.

推荐答案

如请等待硒Webdriver JAVA中完成下载

private void waitForFileDownload(int totalTimeoutInMillis, String expectedFileName) throws IOException { FluentWait<WebDriver> wait = new FluentWait(this.funcDriver.driver) .withTimeout(totalTimeoutInMillis, TimeUnit.MILLISECONDS) .pollingEvery(200, TimeUnit.MILLISECONDS); File fileToCheck = getDownloadsDirectory() .resolve(expectedFileName) .toFile(); wait.until((WebDriver wd) -> fileToCheck.exists()); } public synchronized Path getDownloadsDirectory(){ if(downloadsDirectory == null){ try { downloadsDirectory = Files.createTempDirectory("selleniumdownloads_"); } catch (IOException ex) { throw new RuntimeException("Failed to create temporary downloads directory"); } } return downloadsDirectory; }

然后,您可以使用此之类的库来进行实际的文件处理以查看文件是否已存储正确(这可能意味着比较文件大小,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等待下载完成

本文发布于:2023-11-13 02:47:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583215.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   下载完成   Webdriver

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!