提问人:Tibor Nagy 提问时间:2/18/2014 最后编辑:Vadim KotovTibor Nagy 更新时间:6/9/2022 访问量:5994
如何等待和接受带有水豚/硒的警报框
How to wait for and accept an alert box with capybara/selenium
问:
我在 rspec 测试中使用以下代码:
describe "Save should create a BasketItem and a Basket" do
subject {
lambda {
click_button I18n.t(:create_basket_and_add_items)
page.driver.browser.switch_to.alert.accept # close the alert box
}
}
it { should change(BasketItem, :count).by(1) }
it { should change(Basket, :count).by(1) }
end
触发一个不显眼的 javascript 调用,该调用显示一个警报弹出窗口。但是,仅在大约 50% 的测试运行中成功关闭警报框,我猜是因为在命令运行时,警报框并不总是出现在屏幕上。当然,如果警报框未关闭,下一个测试用例会遇到“超时错误”。click_button
page.driver.browser.switch_to.alert.accept
如果我在 和 之间使用,它总是可以正常工作,但这不是一个非常好的解决方案。有什么想法吗?sleep 1
click_button
...alert.accept
答:
22赞
Tim Moore
2/22/2014
#1
这是我为此使用的一些代码。
wait = Selenium::WebDriver::Wait.new ignore: Selenium::WebDriver::Error::NoAlertPresentError
alert = wait.until { page.driver.browser.switch_to.alert }
alert.accept
评论
1赞
Jeff
12/30/2022
对于 v4.5.0,我不得不使用wait = Selenium::WebDriver::Wait.new ignore: Selenium::WebDriver::Error::NoSuchAlertError
4赞
Alexei.B
11/15/2015
#2
expect{
accept_alert "Are you sure?" do
click_link "Destroy"
end
sleep 1.second # !important
}.to change(Post, :count).by(-1)
0赞
Goaul
6/9/2022
#3
这是我的解决方案,没有其他方法:
scenario 'admin deletes user', js: true do
within(confirmed_user_row) do
expect {
accept_confirm do
click_link_or_button('Delete')
end
sleep(0.5) # or it checks too quick as entry in db not updated yet
}.to change(User, :count).by(-1)
end
expect(page).to have_content("User was deleted from system.")
end
评论