黄瓜 + 测试 JS 警报

发布于 2024-08-01 15:04:44 字数 129 浏览 2 评论 0原文

我正在尝试使用 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

孤独难免 2024-08-08 15:04:44

您可以使用 Webrat 或 Selenium 与 Cucumber 来测试这一点。

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

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

我之前已经使用 Selenium 和 Cucumber 对此进行了测试,但似乎找不到代码,如果我找到了,将编辑帖子。

华泰

You can use Webrat or Selenium with Cucumber to test this.

My guess is that you want Simulated Browser or Automated Browser testing,

You can use Webrat or Webrat::Selenium or simply Selenium with Cucumber in such cases.

I have tested this using Selenium and Cucumber before, but can't seem to find the code, will edit the post it if i do.

HTH

不美如何 2024-08-08 15:04:44

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

使用螺丝单元和/或蓝脊来驱动黄瓜测试将是一项有趣的练习,而且可能并不难实现。

I would recommend using screw-unit for testing javascript behavior on a page. You can also take a look at Relevance's blue-ridge plugin which incorporates screw-unit and adds support for command line and in browser js testing. You can find it on github under relevance/blue-ridge. (I don't have the rep yet to post more than one link :(

It would be an interesting exercise to use screw-unit and/or blue-ridge to drive cucumber tests, and probably not that hard to pull off.

当爱已成负担 2024-08-08 15:04:44

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

This gist has steps to test a JS confirm dialog in Rails 2 and 3 with any Capybara driver, should be easy to adapt to an alert box.

扎心 2024-08-08 15:04:44

请参阅 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

See the method definitions in http://selenium-client.rubyforge.org/classes/Selenium/Client/Idiomatic.html

You can invoke them with the selenium helper object in your Cucumber step definitions -- e.g.,

Then /^I should see a JS confirm dialog saying "([^\"]*)"$/ do |statement|    
  selenium.confirmation.should eql(statement)                               
end
桃气十足 2024-08-08 15:04:44

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

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();

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文