回答
不幸的是,似乎没有办法在水豚做。但是,如果您使用Selenium驱动程序(以及可能支持JavaScript的其他驱动程序)运行测试,则可以对其进行破解。在执行可以调出确认对话框的动作之前,请覆盖confirm
方法以始终返回true。这样,对话框将永远不会显示,并且您的测试可以继续,就好像用户按下了“确定”按钮一样。如果您想模拟相反,只需将其更改为返回false。
page.evaluate_script('window.confirm = function() { return true; }')
page.click('Remove')
太棒了!谢谢! – 2010-04-10 00:56:54
这似乎不再适用于Firefox 4 ...下面的@ derek-ekins解决方案,根据谷歌告诉我的,似乎更加向前兼容,虽然我还不能确认(我被困在水豚0.3.9)。 – carpeliam 2011-04-06 17:34:36
查看下面的答案,使用“page.driver.browser.switch_to ...” – 2012-02-24 09:01:12
如果您想特别测试显示的消息,这是一个非常冒险的方式。我并不认可它是美丽的代码,但它完成了工作。您需要加载http://plugins.jquery.com/node/1386/release,或者如果您不想使用jQuery,请将其更改为原生Cookie。
使用这类故事:
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
很酷的解决方案!我翻转了一下,这让我感觉更加自然:https://gist.github.com/727614 – 2010-12-03 21:48:20
这里是支持警告和确认框的代码的另一个版本,https://gist.github.com/ 919116 – 2011-04-14 12:46:11
从水豚,你会进入这样的:
page.driver.browser.switch_to.alert.accept
或
page.driver.browser.switch_to.alert.dismiss
或
page.driver.browser.switch_to.alert.text
对于其他人,请注意Derek的答案确实有效,我发现官方Selenium文档中的代码没有(cucumber/Selenium)。请注意在Derek的答案中存在'page.driver.browser' – 2011-07-31 13:59:09
Peter - 这里的代码是专门为使用capybara而设计的,而文档中的代码是用于直接使用selenium-webdriver时的代码 - 我也是这样写的我希望它能起作用! – 2011-08-01 12:27:34
啊。是的,好点,完全错过了我。在这种情况下,谢谢你的例子。 – 2011-08-01 13:48:09
我实现了这两个网页步骤/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
甜美!有用! – corroded 2011-06-01 11:41:38
This gist有步骤测试在轨道2与3用JS确认对话框任何水豚司机。
这是以前答案的改编,但不需要jQuery Cookie插件。
Prickle增加了一些方便的便捷方法,在硒和WebKit
弹出窗口工作的capybara-webkit驱动程序支持这一点。
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
试过上面的答案没有运气。最后这个工作对我来说:
@browser.alert.ok
更新目前版本的水豚。大多数水豚司机今天支持模态API。要接受一个确认模态,你会做
accept_confirm do # dismiss_confirm if not accepting
click_link 'delete' # whatever action triggers the modal to appear
end
这可以在黄瓜使用的东西,如
When /^(?:|I)press "([^"]*)" and confirm "([^"]*)"$/ do |button, msg|
accept_confirm msg do
click_button(button)
end
end
将点击指定的按钮,然后接受文本匹配味精确认框
- 1. 如何测试黄瓜对话框?
- 2. 黄瓜测试框架
- 3. 黄瓜测试
- 4. 如何写黄瓜测试?
- 5. 如何测试与黄瓜
- 6. 如何使用黄瓜/水豚测试文件下载对话框
- 7. 的黄瓜测试
- 8. 黄瓜is_admin?测试
- 9. 如何写黄瓜测试用例?
- 10. 如何使用黄瓜测试sidekiq
- 11. 用黄瓜测试SWFUpload
- 12. 用黄瓜测试外键
- 13. 如何调试黄瓜测试?
- 14. 如何使用poltergeist测试确认对话框的内容?
- 15. BDD测试框架-RSpec和黄瓜
- 16. 如何测试黄瓜邮件链接?
- 17. 如何写黄瓜测试frank
- 18. 我黄瓜测试场景
- 19. 黄瓜测试重定向
- 20. 黄瓜Junit测试代码
- 21. 黄瓜+测试JS警报
- 22. 黄瓜和JUnit测试
- 23. NoMethodError运行黄瓜测试
- 24. 黄瓜/ RSpec的测试
- 25. 测试设计与黄瓜
- 26. jQuery.live()和黄瓜测试
- 27. 黄瓜测试停止
- 28. 在黄瓜堆积测试
- 29. 黄瓜测试java示例?
- 30. 重复小黄瓜测试
如果你使用的是Capybara-webkit,你会在那里找到你的答案:http://stackoverflow.com/questions/6930927/how-do-i-confirm-a-javascript-popup-with-capybara# – Adrien 2014-02-18 15:42:54