Selenium 等待下载?

发布于 2024-09-27 03:42:41 字数 224 浏览 0 评论 0原文

我正在尝试测试一段需要很长时间才能响应的代码的快乐路径,然后开始将文件写入响应输出流,这会在浏览器中提示下载对话框。

问题是这个过程过去失败了,在这么长时间的工作后抛出异常。 selenium 有没有办法 wait-for-download 或等效方法?

我可以添加一个 Thread.sleep,但这会不准确并且不必要地减慢测试运行速度。

在这里我该怎么办?

I'm trying to test the happy-path for a piece of code which takes a long time to respond, and then begins writing a file to the response output stream, which prompts a download dialog in browsers.

The problem is that this process has failed in the past, throwing an exception after this long amount of work. Is there a way in selenium to wait-for-download or equivalent?

I could throw in a Thread.sleep, but that would be inaccurate and unnecessarily slow down the test run.

What should I do, here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

隔纱相望 2024-10-04 03:42:41

我也有同样的问题。我发明了一些东西来解决这个问题。临时文件由 Python 创建,扩展名为“.part”。因此,如果我们仍然有临时值,Python 可以等待 10 秒并再次检查文件是否已下载。

 while True:
        if os.path.isfile('ts.csv.part'):
            sleep(10)
        elif os.path.isfile('ts.csv'):
            break
        else:
            sleep(10)
 driver.close()

I had the same problem. I invented something to solve the problem. A tempt file is created by Python with '.part' extension. So, if still we have the temp, python can wait for 10 second and check again if the file is downloaded or not yet.

 while True:
        if os.path.isfile('ts.csv.part'):
            sleep(10)
        elif os.path.isfile('ts.csv'):
            break
        else:
            sleep(10)
 driver.close()
无法言说的痛 2024-10-04 03:42:41

所以这里有两个问题:

  1. 您需要让浏览器下载文件
  2. 您需要测量下载的文件何时完成

这两个问题都不能通过 Selenium 直接解决(但 2.0 可能会有所帮助),但这两个问题都是可以解决的。第一个问题可以通过GUI自动化工具包来解决,例如AutoIT。但也可以通过简单地在操作系统级别发送一个模拟回车键的自动按键来解决(适用于 Firefox,在某些版本的 Chrome 和 Safari 上有点困难)。如果您使用 Java,则可以使用 Robot 来执行此操作。其他语言也有类似的工具包来完成这样的事情。

第二个问题可能最好通过某种代理解决方案来解决。例如,如果您的浏览器配置为通过代理,并且该代理具有 API,您可以使用该 API 查询代理以询问网络活动何时结束。

这就是我们在 http://browsermob.com 所做的事情,这是我创立的一家使用 Selenium 进行负载测试的初创公司。我们已将一些代理代码作为开源发布,可从 http://browsermob.com/tools 获取。

但仍然存在两个问题:

  1. 您需要配置浏览器才能使用代理。在 Selenium 2 中这更容易,但在 Selenium 1 中也可以做到这一点。关键是确保您的浏览器启动器使用正确的配置文件/设置启动浏览器。
  2. 目前,BrowserMob 代理没有 API 可以告诉您网络流量何时停止!这是项目概念中的一个大漏洞,我想一有时间就修复它。不过,如果您热衷于提供帮助,请加入 Google 网上论坛,我绝对可以为您指明正确的方向。

希望能帮助您确定各种选择。祝你好运!

So you have two problems here:

  1. You need to cause the browser to download the file
  2. You need to measure when the downloaded file is complete

Neither problemc an be directly solved by Selenium (yet - 2.0 may help), but both are solvable problems. The first problem can be solved by GUI automation toolkits, such as AutoIT. But they can also be solved by simply sending an automated keypress at the OS level that simulates the enter key (works for Firefox, a little harder on some versions of Chrome and Safari). If you're using Java, you can use Robot to do that. Other languages have similar toolkits to do such a thing.

The second issue is probably best solved with some sort of proxy solution. For example, if your browser was configured to go through a proxy and that proxy had an API, you could query the proxy with that API to ask when network activity had ended.

That's what we do at http://browsermob.com, which is a a startup I founded that uses Selenium to do load testing. We've released some of the proxy code as open source, available at http://browsermob.com/tools.

But two problems still persist:

  1. You need to configure the browser to use the proxy. In Selenium 2 this is easier, but it's possible to do it with Selenium 1 as well. The key is just making sure that your browser launcher brings up the browser with the right profile/settings.
  2. There currently is no API for BrowserMob proxy to tell you when network traffic has stopped! This is a big hole in the concept of the project that I want to fix as soon as I get the time. However, if you're keen to help out, join the Google Group and I can definitely point you in the right direction.

Hope that helps you identify your various options. Best of luck!

丑丑阿 2024-10-04 03:42:41

这是 Chrome 测试专用的解决方案,用于使用 javascript 控制下载。

使用 WebDriver (Selenium2),它可以在 Chrome 的 chrome://(HTML/CSS)中完成/Javascript:“chrome://downloads/downloads.js

driver.get( "chrome://downloads/" );
waitElement( By.CssSelector("#downloads-summary-text") );

// next javascript snippet cancels the last/current download
// if your test ends in file attachment downloading 
// you'll very likely need this if you more re-instantiated tests left
((JavascriptExecutor)driver).executeScript("downloads.downloads_[0].cancel_();");

”中还有其他 Download.prototype.functions,

如果您只需要测试一些信息注释,例如,这适合您。由文件附件启动活动引起,而不是由文件本身引起。

当然,您需要控制第1步 - 上面的Patrick提到 - 通过此您可以控制第 2 步。 用于测试,而不是用于实际文件下载完成/取消的功能。

另请参阅:Javascript:取消/停止图像请求,与浏览器停止相关。

This is Chrome-testing-only solution for controlling the downloads with javascript..

Using WebDriver (Selenium2) it can be done within Chrome's chrome:// which is HTML/CSS/Javascript:

driver.get( "chrome://downloads/" );
waitElement( By.CssSelector("#downloads-summary-text") );

// next javascript snippet cancels the last/current download
// if your test ends in file attachment downloading 
// you'll very likely need this if you more re-instantiated tests left
((JavascriptExecutor)driver).executeScript("downloads.downloads_[0].cancel_();");

There are other Download.prototype.functions in "chrome://downloads/downloads.js"

This suites you if you just need to test some info note eg. caused by file attachment starting activity, and not the file itself.

Naturally you need to control step 1. - mentioned by Patrick above - and by this you control step 2. FOR THE TEST, not for the functionality of actual file download completion / cancel.

See also : Javascript: Cancel/Stop Image Requests which is relating to Browser stopping.

山有枢 2024-10-04 03:42:41

这属于“无法自动化的事情”类别。 Selenium 是使用 JavaScipt 构建的,由于 JavaScript 沙箱限制,它无法访问下载。

一旦实现了警报/提示,Selenium 2 或许就能做到这一点,但在接下来的一段时间内这还不会发生。

This falls under the "things that can't be automated" category. Selenium is built with JavaScipt and due to JavaScript sandbox restrictions it can't access downloads.

Selenium 2 might be able to do this once Alerts/Prompts have been implemented but that this won't happen for the next little while yet.

靖瑶 2024-10-04 03:42:41

如果您想检查下载对话框,请尝试使用 AutoIt。我用它来上传和下载文件。将 AutoIt 与 Se RC 结合使用会更容易。

If you want to check for the download dialog, try with AutoIt. I use that for uploading and downloading the files. Using AutoIt with Se RC is easier.

乱了心跳 2024-10-04 03:42:41
def file_downloaded?(file)
    while File.file?(file) == false
      p "File downloading in progress..."
      sleep 1
    end
end

*Ruby 语法

def file_downloaded?(file)
    while File.file?(file) == false
      p "File downloading in progress..."
      sleep 1
    end
end

*Ruby Syntax

对岸观火 2024-10-04 03:42:41

您可以使用浏览器下载管理器检查下载状态。对于 Firefox 来说,这看起来像这样:

#firefox-specific
def wait_for_downloads_completed(driver):
    original_window = driver.current_window_handle
    driver.switch_to.new_window('tab')
    driver.get("about:downloads")
    while True:
        items = driver.find_elements(By.TAG_NAME,"richlistitem")
        #state 0=downloading, 1=done, 4=paused, 3=cancelled
        if all(item.get_attribute("state") == "1" for item in items):
            break
        time.sleep(2)
    driver.close()
    driver.switch_to.window(original_window) 

You can use browsers download manager to check the states of downloads. For firefox this would look like this:

#firefox-specific
def wait_for_downloads_completed(driver):
    original_window = driver.current_window_handle
    driver.switch_to.new_window('tab')
    driver.get("about:downloads")
    while True:
        items = driver.find_elements(By.TAG_NAME,"richlistitem")
        #state 0=downloading, 1=done, 4=paused, 3=cancelled
        if all(item.get_attribute("state") == "1" for item in items):
            break
        time.sleep(2)
    driver.close()
    driver.switch_to.window(original_window) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文