如何用黄瓜测试确认对话框? [英] How to test a confirm dialog with 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?
推荐答案
不幸的是,似乎没有办法在水豚中做到这一点.但是,如果您正在使用Selenium驱动程序(可能还有其他支持JavaScript的驱动程序)运行测试,则可以对其进行破解.在执行将弹出确认对话框的操作之前,请覆盖confirm
方法以始终返回true.这样,对话框将永远不会显示,并且您的测试可以继续进行,就像用户按下了确定"按钮一样.如果要模拟反向,只需将其更改为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.
page.evaluate_script('window.confirm = function() { return true; }')
page.click('Remove')
这篇关于如何用黄瓜测试确认对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!