黄瓜+测试JS警报

时间:2009-08-10 18:32:08

标签: ruby-on-rails cucumber

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

5 个答案:

答案 0 :(得分:6)

您可以使用硒的各种功能来捕获警报/确认。 它们不能直接用于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或您的宝石服务器上找到更多文档

答案 1 :(得分:1)

请参阅http://selenium-client.rubyforge.org/classes/Selenium/Client/Idiomatic.html

中的方法定义

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

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

答案 2 :(得分:0)

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

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

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

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

HTH

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

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