检测在Selenium Java中下载的文件 [英] detecting a file downloaded in selenium java

查看:51
本文介绍了检测在Selenium Java中下载的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在selenium java中编写了一个自动化测试,该测试可以检测页面是否正在重定向(自动化检测是否打开了新页面,页面重定向到了其他页面,打开了新标签以及是否打开了警报窗口)

I wrote an automation test in selenium java that detects if the page is redirecting (the automation detects if a new page is opened, the page redirects to other page, a new tab is opened and if an alert window is opened)

现在是问题所在.我找不到任何方法可以检测到的重定向之一是自动下载的文件(您输入一个网站,该网站会自动下载文件而无需用户触发)

Now to the problem. one of the redirects i can't find any way to detect is an automatic downloaded file (you enter a website and the website automatically downloads a file without any trigger from the user)

p.s.

我知道每种浏览器的下载过程可能有所不同,我需要它主要在chrome上工作

I know the download process may differ in each browser, I need it to work mainly on chrome

谢谢

推荐答案

我有同样的问题,这是我在Internet上某个地方发现的内容(也许是在stackoverflow上,我不记得了).我只是添加了一行删除文件,以便在测试开始时通过调用此方法来确保再次尝试下载文件时该文件不再存在.

I had the same question and here is what I found somewhere on the Internet (maybe on stackoverflow, I cannot remember). I just added a line to delete the file so that by calling this method at the beginning of my test, I'm making sure the file does not exist anymore when trying to download it again.

  public boolean isFileDownloaded(String downloadPath, String fileName) {
  File dir = new File(downloadPath);
  File[] dirContents = dir.listFiles();

  for (int i = 0; i < dirContents.length; i++) {
      if (dirContents[i].getName().equals(fileName)) {
          // File has been found, it can now be deleted:
          dirContents[i].delete();
          return true;
      }
          }
      return false;
  }

您仅需拨打以下电话:isFileDownloaded("C:\ Path \ To \ Your \ Folder","yourPdfFile.abc");

You just have to call with this single line: isFileDownloaded("C:\Path\To\Your\Folder", "yourPdfFile.abc");

希望这会有所帮助!

这篇关于检测在Selenium Java中下载的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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