黄瓜+测试JS警报

我正在尝试使用Cucumber on Rails测试JS确认对话框。 我有一个window.onbeforeunload事件处理程序,如果你试图离开页面但我不知道如何测试它会提示你一个确认对话框,任何人都知道如何做到这一点?

您可以使用selenium的各种function来捕获警报/确认。 它们不能直接用于webrat selenium实现,但是当使用webrat的config.mode = :selenium它们可以按如下方式使用:

 Then /^I should see a JS alert$/ do selenium.is_alert_present.should be_true end # or Then /^I should see a "Are you sure?" JS confirm dialog$/ do selenium.get_alert.should eql("Are you sure?") end # you can also click the OK/Cancel buttons on confirm boxes with selenium.chooseOkOnNextConfirmation(); #and selenium.chooseCancelOnNextConfirmation(); 

可能没有最好的测试,但给你一个想法。 内部selenium覆盖JS的alert()和confirm()函数,因此它可以捕获此信息。

您可以在selenium faq或gem服务器上找到更多文档

请参阅http://selenium-client.rubyforge.org/classes/Selenium/Client/Idiomatic.html中的方法定义

您可以在Cucumber步骤定义中使用selenium helper对象调用它们 – 例如,

 Then /^I should see a JS confirm dialog saying "([^\"]*)"$/ do |statement| selenium.confirmation.should eql(statement) end 

您可以使用Webrat或Selenium with Cucumber来测试它。

我的猜测是你想要模拟浏览器或自动浏览器测试,

在这种情况下,您可以使用Webrat或Webrat :: Selenium或简单地使用Selenium和Cucumber。

我之前使用Selenium和Cucumber测试了这个,但似乎无法找到代码,如果我这样做就会编辑post。

HTH

我建议使用螺丝装置来测试页面上的javascript行为。 您还可以查看Relevance的蓝脊插件,它包含了螺丝装置,并增加了对命令行和浏览器js测试的支持。 您可以在相关/ blue-ridge下的github上找到它。 (我还没有代表发布多个链接:(

使用螺旋单元和/或蓝脊来驱动黄瓜测试是一项有趣的练习,并且可能不那么难以完成。

这个要点有步骤在任何Capybara驱动程序中测试Rails 2和3中的JS确认对话框,应该很容易适应警报框。