黄瓜+测试JS警报 [英] Cucumber + testing JS alert

查看:150
本文介绍了黄瓜+测试JS警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Cucumber on Rails测试一个JS确认对话框。我有一个window.onbeforeunload事件处理程序,如果你尝试离开页面,但我不知道如何测试,任何人都有一个想法,如何这样做,将提示您一个确认对话框。

I'm trying to test a JS confirmation dialog with Cucumber on Rails. I have a window.onbeforeunload event handler that will prompt you with a confirmation dialog if you try to navigate away from the page but I have no idea how to test it, anyone have an idea on how this can be done?

推荐答案

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

There are various functions of selenium you can use to capture alerts/confirms. They are not directly available with the webrat selenium implementation, but when using webrat's config.mode = :selenium they can be used as follows:

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()函数,所以它可以捕获这些信息。

There are probably not the greatest tests, but gives you an idea. Internally selenium overrides the alert() and confirm() functions of JS so it can captures this information.

你可以在 selenium faq 或您的宝石服务器上

You can find more docs on the selenium faq or on your gem server

这篇关于黄瓜+测试JS警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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