Selenium 等待下载?
我正在尝试测试一段需要很长时间才能响应的代码的快乐路径,然后开始将文件写入响应输出流,这会在浏览器中提示下载对话框。
问题是这个过程过去失败了,在这么长时间的工作后抛出异常。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我也有同样的问题。我发明了一些东西来解决这个问题。临时文件由 Python 创建,扩展名为“.part”。因此,如果我们仍然有临时值,Python 可以等待 10 秒并再次检查文件是否已下载。
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.
所以这里有两个问题:
这两个问题都不能通过 Selenium 直接解决(但 2.0 可能会有所帮助),但这两个问题都是可以解决的。第一个问题可以通过GUI自动化工具包来解决,例如AutoIT。但也可以通过简单地在操作系统级别发送一个模拟回车键的自动按键来解决(适用于 Firefox,在某些版本的 Chrome 和 Safari 上有点困难)。如果您使用 Java,则可以使用 Robot 来执行此操作。其他语言也有类似的工具包来完成这样的事情。
第二个问题可能最好通过某种代理解决方案来解决。例如,如果您的浏览器配置为通过代理,并且该代理具有 API,您可以使用该 API 查询代理以询问网络活动何时结束。
这就是我们在 http://browsermob.com 所做的事情,这是我创立的一家使用 Selenium 进行负载测试的初创公司。我们已将一些代理代码作为开源发布,可从 http://browsermob.com/tools 获取。
但仍然存在两个问题:
希望能帮助您确定各种选择。祝你好运!
So you have two problems here:
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:
Hope that helps you identify your various options. Best of luck!
这是 Chrome 测试专用的解决方案,用于使用 javascript 控制下载。
使用 WebDriver (Selenium2),它可以在 Chrome 的 chrome://(HTML/CSS)中完成/Javascript:“chrome://downloads/downloads.js
”中还有其他 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:
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.
这属于“无法自动化的事情”类别。 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.
如果您想检查下载对话框,请尝试使用 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.
*Ruby 语法
*Ruby Syntax
您可以使用浏览器下载管理器检查下载状态。对于 Firefox 来说,这看起来像这样:
You can use browsers download manager to check the states of downloads. For firefox this would look like this: