2010-03-16 26 views
82

我正在使用Ruby on Rails和黄瓜和水豚。如何用黄瓜测试确认对话框?

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

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

+0

如果你使用的是Capybara-webkit,你会在那里找到你的答案:http://stackoverflow.com/questions/6930927/how-do-i-confirm-a-javascript-popup-with-capybara# – Adrien 2014-02-18 15:42:54

回答

59

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

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

太棒了!谢谢! – 2010-04-10 00:56:54

+0

这似乎不再适用于Firefox 4 ...下面的@ derek-ekins解决方案,根据谷歌告诉我的,似乎更加向前兼容,虽然我还不能确认(我被困在水豚0.3.9)。 – carpeliam 2011-04-06 17:34:36

+1

查看下面的答案,使用“page.driver.browser.switch_to ...” – 2012-02-24 09:01:12

8

如果您想特别测试显示的消息,这是一个非常冒险的方式。我并不认可它是美丽的代码,但它完成了工作。您需要加载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 
+2

很酷的解决方案!我翻转了一下,这让我感觉更加自然:https://gist.github.com/727614 – 2010-12-03 21:48:20

+0

这里是支持警告和确认框的代码的另一个版本,https://gist.github.com/ 919116 – 2011-04-14 12:46:11

131

硒司机now supports this

从水豚,你会进入这样的:

page.driver.browser.switch_to.alert.accept 

page.driver.browser.switch_to.alert.dismiss 

page.driver.browser.switch_to.alert.text 
+2

对于其他人,请注意Derek的答案确实有效,我发现官方Selenium文档中的代码没有(cucumber/Selenium)。请注意在Derek的答案中存在'page.driver.browser' – 2011-07-31 13:59:09

+0

Peter - 这里的代码是专门为使用capybara而设计的,而文档中的代码是用于直接使用selenium-webdriver时的代码 - 我也是这样写的我希望它能起作用! – 2011-08-01 12:27:34

+0

啊。是的,好点,完全错过了我。在这种情况下,谢谢你的例子。 – 2011-08-01 13:48:09

38

我实现了这两个网页步骤/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 
+0

甜美!有用! – corroded 2011-06-01 11:41:38

0

This gist有步骤测试在轨道2与3用JS确认对话框任何水豚司机。

这是以前答案的改编,但不需要jQuery Cookie插件。

1

Prickle增加了一些方便的便捷方法,在硒和WebKit

2
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 
0

试过上面的答案没有运气。最后这个工作对我来说:

@browser.alert.ok 
3

更新目前版本的水豚。大多数水豚司机今天支持模态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 

将点击指定的按钮,然后接受文本匹配味精确认框