如何测试与黄瓜的确认对话框?
-
20-09-2019 - |
题
我使用红宝石与黄瓜和水豚导轨。
我将如何去测试一个简单的确认命令(“你确定吗?”)?
另外,我在哪里能找到这个问题的进一步资料?
解决方案
好像没有办法做到这一点的水豚,很遗憾。但是,如果你正在运行与硒的驱动程序(也可能是其他驱动程序支持JavaScript)你的测试,你可以破解它。只是执行,将弹出确认对话框行动之前,覆盖confirm
方法总是返回true。这样,将永远不会被显示在对话框中,你的测试可以继续,如果用户已经按下OK按钮。如果你要模拟的反向,简单地改变它返回false。
page.evaluate_script('window.confirm = function() { return true; }')
page.click('Remove')
其他提示
在硒驱动现在支持该
从水豚,你会进入这样的:
page.driver.browser.switch_to.alert.accept
或
page.driver.browser.switch_to.alert.dismiss
或
page.driver.browser.switch_to.alert.text
我实现在/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
如果您想具体测试信息显示,这里是这样做的一个特别哈克的方式。我不认可它作为漂亮的代码,但它能够完成任务。你需要加载 http://plugins.jquery.com/node/1386/release一>,或将其更改为本地做饼干,如果你不想要的jQuery。
使用这类故事的:
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
更新此为水豚的当前版本。今天,大多数水豚驱动程序支持模式的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
这将点击命名按钮,然后接受与文本匹配MSG确认框
在水豚-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
刺痒增加了一些方便的方便的方法用于与硒和WebKit弹出窗口的工作
此要旨具有步骤测试在轨道2和3一个JS确认对话框与任何水豚驱动程序。
这是一个以前的答案的改编,但并不需要jQuery的Cookie的插件。
尝试没有运气上述答案。到底这个工作对我来说:
@browser.alert.ok
不隶属于 StackOverflow