如何通过 cron 使用 selenium 运行 rspec 测试
我在通过 cron 运行 rspec 测试时遇到问题。我正在使用 selenium 访问 Firefox 并测试登录页面。我的所有文件在 cron 之外执行时都可以正常工作。据我所知,问题可能是 cron 需要一个显示环境来运行 Firefox 等程序。这就是我陷入困境的地方,如何为 selenium 设置显示环境以从 cron 中启动 firefox。
我已经尝试在 crontab 中以几种方式设置显示。
*/25 * * * * /home/justin/test.sh --display=:0 > testlog
*/25 * * * * DISPLAY=:0; /home/justin/test.sh > testlog;
我也尝试在脚本中设置它。
#!/bin/sh
cd /home/justin/widget_ui_testing/
DISPLAY=:0
/home/justin/.rvm/bin/rvm exec /home/justin/.rvm/gems/ruby-1.9.2-p290/bin/rspec
-fdoc /home/justin/widget_ui_testing/spec/requests/log_in_spec.rb
我不断收到的错误消息是
Selenium::WebDriver::Error::WebDriverError:
unable to obtain stable firefox connection in 60 seconds (127.0.0.1:7055)
# ./spec/requests/log_in_spec.rb:22:in `block (2 levels) in <top (required)>'
我想知道是否有办法更改 selenium 的代码来运行 firefox具有指定的显示。或者如果我一路上错过了什么。任何帮助将不胜感激。
I'm having trouble running rspec tests through cron. I'm using selenium to access firefox and test logging into a page. All my files work properly when executed outside of cron. From what I've read, the problem could be that cron needs a display environment to run programs like firefox. This is where I'm stuck, how can I set up a display environment for selenium to start up firefox from within cron.
I've tried setting the display a couple ways in the crontab..
*/25 * * * * /home/justin/test.sh --display=:0 > testlog
*/25 * * * * DISPLAY=:0; /home/justin/test.sh > testlog;
I've also tried setting it in the script..
#!/bin/sh
cd /home/justin/widget_ui_testing/
DISPLAY=:0
/home/justin/.rvm/bin/rvm exec /home/justin/.rvm/gems/ruby-1.9.2-p290/bin/rspec
-fdoc /home/justin/widget_ui_testing/spec/requests/log_in_spec.rb
The error message I keep getting is
Selenium::WebDriver::Error::WebDriverError:
unable to obtain stable firefox connection in 60 seconds (127.0.0.1:7055)
# ./spec/requests/log_in_spec.rb:22:in `block (2 levels) in <top (required)>'
I'm wondering if there is a way to change selenium's code to run firefox with a specified display. Or if I've missed something along the way. Any help would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须先启动 Xvfb。我建议您使用 headless gem 来简化与 Xvfb 的交互。另请参阅自述文件,它有类似的用例。
另请查看这篇博客文章设置詹金斯和无头。
PS:只是一个建议 - 不要使用 cron 来达到这样的目的,使用 CI 工具。我推荐 Jenkins CI 它可以通过轮询 SCM 和定期(您的确切用例)进行构建。
You have to start Xvfb first. I recommend you to use headless gem that simplifies interaction with Xvfb. Also take a look into readme it has similar use cases.
Also take a look into this blog post about setting up Jenkins and headless.
P.S: Just a suggestion - don't use cron for such purpose use CI tool. I recommend Jenkins CI it can do builds both by polling SCM and periodically (your exact use case).