如何使用 Cucumber 测试确认对话框?
我正在将 Ruby on Rails 与 Cucumber 和 Capybara 一起使用。
我将如何测试一个简单的确认命令(“你确定吗?”)?
另外,我在哪里可以找到有关此问题的更多文档?
I am using Ruby on Rails with Cucumber and Capybara.
How would I go about testing a simple confirm command ("Are you sure?")?
Also, where could I find further documentation on this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
selenium 驱动程序现在支持此功能
从 Capybara 中,您可以像这样访问它:
或
或
The selenium driver now supports this
From Capybara you would access it like this:
or
or
不幸的是,似乎在水豚中没有办法做到这一点。但如果您使用 Selenium 驱动程序(可能还有其他支持 JavaScript 的驱动程序)运行测试,则可以破解它。在执行将弹出确认对话框的操作之前,重写
confirm
方法以始终返回 true。这样,该对话框将永远不会显示,并且您的测试可以继续,就像用户按下了“确定”按钮一样。如果想模拟相反的情况,只需将其改为return false即可。Seems like there's no way to do it in Capybara, unfortunately. But if you're running your tests with the Selenium driver (and probably other drivers that support JavaScript), you can hack it. Just before performing the action that would bring up the confirm dialog, override the
confirm
method to always return true. That way the dialog will never be displayed, and your tests can continue as if the user had pressed the OK button. If you want to simulate the reverse, simply change it to return false.我已在
/features/step_definitions/web_steps.rb
中实现了这两个 Web 步骤:I've implemented these two web steps in
/features/step_definitions/web_steps.rb
:如果您想专门测试所显示的消息,可以使用一种特别巧妙的方法。我不认为它是漂亮的代码,但它可以完成工作。您需要加载 http://plugins.jquery.com/node/1386/release,或者如果您不需要 jQuery,则将其更改为本地执行 cookies。
使用此类故事:
以及这些步骤
If you want to specifically test the message being displayed, here's a particularly hacky way to do so. I don't endorse it as beautiful code, but it gets the job done. You'll need to load http://plugins.jquery.com/node/1386/release, or change it to do cookies natively if you don't want jQuery.
Use this sort of story:
And these steps
更新当前版本的 Capybara。目前大多数 Capybara 驱动程序都支持模态 API。要接受确认模式,您可以这样做
这可以在 Cucumber 中使用,类似的操作
将单击指定的按钮,然后接受带有文本匹配 msg 的确认框
Updating this for current releases of Capybara. Most Capybara drivers today support the modal API. To accept a confirm modal you would do
This can be used in Cucumber with something like
which will click the named button and then accept a confirm box with text matching msg
capybara-webkit 驱动程序也支持此功能。
The capybara-webkit driver supports this as well.
Prickle 添加了一些方便的方法来处理 selenium 和 webkit 中的弹出窗口
Prickle adds some handy convenience methods for working with popups in selenium and webkit
这个要点包含使用任何 Capybara 驱动程序在 Rails 2 和 3 中测试 JS 确认对话框的步骤。
它是对之前答案的改编,但不需要 jQuery Cookie 插件。
This gist has steps to test a JS confirm dialog in Rails 2 and 3 with any Capybara driver.
It's an adaptation of a previous answer, but doesn't need the jQuery Cookie plugin.
尝试了上面的答案,但没有运气。最后这对我有用:
Tried the above answers with no luck. In the end this worked for me: