如何用 Cucumber 测试确认对话框? [英] How to test a confirm dialog with Cucumber?

查看:17
本文介绍了如何用 Cucumber 测试确认对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Cucumber 和 Capybara 中使用 Ruby on Rails.

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?

推荐答案

不幸的是,Capybara 似乎没有办法做到这一点.但是,如果您使用 Selenium 驱动程序(可能还有其他支持 JavaScript 的驱动程序)运行测试,您可以破解它.在执行将打开确认对话框的操作之前,重写 confirm 方法以始终返回 true.这样,对话框将永远不会显示,并且您的测试可以继续进行,就像用户按下了 OK 按钮一样.如果要模拟反向,只需将其更改为返回 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')

这篇关于如何用 Cucumber 测试确认对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆