如何使用 Cucumber 测试确认对话框?

发布于 2024-08-25 13:52:01 字数 118 浏览 8 评论 0原文

我正在将 Ruby on Rails 与 Cucumber 和 Capybara 一起使用。

我将如何测试一个简单的确认命令(“你确定吗?”)?

另外,我在哪里可以找到有关此问题的更多文档?

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?

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

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

发布评论

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

评论(10

忆依然 2024-09-01 13:52:01

selenium 驱动程序现在支持此功能

从 Capybara 中,您可以像这样访问它:

page.driver.browser.switch_to.alert.accept

page.driver.browser.switch_to.alert.dismiss

 page.driver.browser.switch_to.alert.text

The selenium driver now supports this

From Capybara you would access it like this:

page.driver.browser.switch_to.alert.accept

or

page.driver.browser.switch_to.alert.dismiss

or

 page.driver.browser.switch_to.alert.text
三岁铭 2024-09-01 13:52:01

不幸的是,似乎在水豚中没有办法做到这一点。但如果您使用 Selenium 驱动程序(可能还有其他支持 JavaScript 的驱动程序)运行测试,则可以破解它。在执行将弹出确认对话框的操作之前,重写 confirm 方法以始终返回 true。这样,该对话框将永远不会显示,并且您的测试可以继续,就像用户按下了“确定”按钮一样。如果想模拟相反的情况,只需将其改为return false即可。

page.evaluate_script('window.confirm = function() { return true; }')
page.click('Remove')

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')
美胚控场 2024-09-01 13:52:01

我已在 /features/step_definitions/web_steps.rb 中实现了这两个 Web 步骤:

When /^I confirm popup$/ do
  page.driver.browser.switch_to.alert.accept    
end

When /^I dismiss popup$/ do
  page.driver.browser.switch_to.alert.dismiss
end

I've implemented these two web steps in /features/step_definitions/web_steps.rb:

When /^I confirm popup$/ do
  page.driver.browser.switch_to.alert.accept    
end

When /^I dismiss popup$/ do
  page.driver.browser.switch_to.alert.dismiss
end
神回复 2024-09-01 13:52:01

如果您想专门测试所显示的消息,可以使用一种特别巧妙的方法。我不认为它是漂亮的代码,但它可以完成工作。您需要加载 http://plugins.jquery.com/node/1386/release,或者如果您不需要 jQuery,则将其更改为本地执行 cookies。

使用此类故事:

Given I am on the menu page for the current booking
And a confirmation box saying "The menu is £3.50 over budget. Click Ok to confirm anyway, or Cancel if you want to make changes." should pop up
And I want to click "Ok"
When I press "Confirm menu"
Then the confirmation box should have been displayed

以及这些步骤

Given /^a confirmation box saying "([^"]*)" should pop up$/ do |message|
  @expected_message = message
end

Given /^I want to click "([^"]*)"$/ do |option|
  retval = (option == "Ok") ? "true" : "false"

  page.evaluate_script("window.confirm = function (msg) {
    $.cookie('confirm_message', msg)
    return #{retval}
  }")
end

Then /^the confirmation box should have been displayed$/ do
  page.evaluate_script("$.cookie('confirm_message')").should_not be_nil
  page.evaluate_script("$.cookie('confirm_message')").should eq(@expected_message)
  page.evaluate_script("$.cookie('confirm_message', null)")
end

If you want to specifically test the message being displayed, here's a particularly hacky way to do so. I don't endorse it as beautiful code, but it gets the job done. You'll need to load http://plugins.jquery.com/node/1386/release, or change it to do cookies natively if you don't want jQuery.

Use this sort of story:

Given I am on the menu page for the current booking
And a confirmation box saying "The menu is £3.50 over budget. Click Ok to confirm anyway, or Cancel if you want to make changes." should pop up
And I want to click "Ok"
When I press "Confirm menu"
Then the confirmation box should have been displayed

And these steps

Given /^a confirmation box saying "([^"]*)" should pop up$/ do |message|
  @expected_message = message
end

Given /^I want to click "([^"]*)"$/ do |option|
  retval = (option == "Ok") ? "true" : "false"

  page.evaluate_script("window.confirm = function (msg) {
    $.cookie('confirm_message', msg)
    return #{retval}
  }")
end

Then /^the confirmation box should have been displayed$/ do
  page.evaluate_script("$.cookie('confirm_message')").should_not be_nil
  page.evaluate_script("$.cookie('confirm_message')").should eq(@expected_message)
  page.evaluate_script("$.cookie('confirm_message', null)")
end
泪意 2024-09-01 13:52:01

更新当前版本的 Capybara。目前大多数 Capybara 驱动程序都支持模态 API。要接受确认模式,您可以这样做

accept_confirm do  # dismiss_confirm if not accepting
  click_link 'delete'  # whatever action triggers the modal to appear
end

这可以在 Cucumber 中使用,类似的操作

When /^(?:|I )press "([^"]*)" and confirm "([^"]*)"$/ do |button, msg|
  accept_confirm msg do
    click_button(button)
  end
end

将单击指定的按钮,然后接受带有文本匹配 msg 的确认框

Updating this for current releases of Capybara. Most Capybara drivers today support the modal API. To accept a confirm modal you would do

accept_confirm do  # dismiss_confirm if not accepting
  click_link 'delete'  # whatever action triggers the modal to appear
end

This can be used in Cucumber with something like

When /^(?:|I )press "([^"]*)" and confirm "([^"]*)"$/ do |button, msg|
  accept_confirm msg do
    click_button(button)
  end
end

which will click the named button and then accept a confirm box with text matching msg

伴我老 2024-09-01 13:52:01

capybara-webkit 驱动程序也支持此功能。

The capybara-webkit driver supports this as well.

逆光飞翔i 2024-09-01 13:52:01
Scenario: Illustrate an example has dialog confirm with text
    #     
    When I confirm the browser dialog with tile "Are you sure?"
    #
=====================================================================
my step definition here:

And(/^I confirm the browser dialog with title "([^"]*)"$/) do |title|
  if page.driver.class == Capybara::Selenium::Driver
    page.driver.browser.switch_to.alert.text.should eq(title)
    page.driver.browser.switch_to.alert.accept
  elsif page.driver.class == Capybara::Webkit::Driver
    sleep 1 # prevent test from failing by waiting for popup
    page.driver.browser.confirm_messages.should eq(title)
    page.driver.browser.accept_js_confirms
  else
   raise "Unsupported driver"
 end
end
Scenario: Illustrate an example has dialog confirm with text
    #     
    When I confirm the browser dialog with tile "Are you sure?"
    #
=====================================================================
my step definition here:

And(/^I confirm the browser dialog with title "([^"]*)"$/) do |title|
  if page.driver.class == Capybara::Selenium::Driver
    page.driver.browser.switch_to.alert.text.should eq(title)
    page.driver.browser.switch_to.alert.accept
  elsif page.driver.class == Capybara::Webkit::Driver
    sleep 1 # prevent test from failing by waiting for popup
    page.driver.browser.confirm_messages.should eq(title)
    page.driver.browser.accept_js_confirms
  else
   raise "Unsupported driver"
 end
end
半透明的墙 2024-09-01 13:52:01

Prickle 添加了一些方便的方法来处理 selenium 和 webkit 中的弹出窗口

Prickle adds some handy convenience methods for working with popups in selenium and webkit

原来分手还会想你 2024-09-01 13:52:01

这个要点包含使用任何 Capybara 驱动程序在 Rails 2 和 3 中测试 JS 确认对话框的步骤。

它是对之前答案的改编,但不需要 jQuery Cookie 插件。

This gist has steps to test a JS confirm dialog in Rails 2 and 3 with any Capybara driver.

It's an adaptation of a previous answer, but doesn't need the jQuery Cookie plugin.

对你再特殊 2024-09-01 13:52:01

尝试了上面的答案,但没有运气。最后这对我有用:

@browser.alert.ok

Tried the above answers with no luck. In the end this worked for me:

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