如何验证 ruby capybara 中弹出消息的内容

how to verify the content of the pop message in ruby capybara

我想在提交表单后验证弹出消息的内容"Only Whole numbers"。下面是提交表单的代码:

fill_in 'allocationRegContrib[12].newFundValue', :with => workbook.cell(2,3)
find(:xpath, '//*[@id="allocationChangeDetails"]').click
sleep 5
expect(page).to have_content("Only Whole numbers")

第4行是我要查看弹窗内容的地方。我收到错误 "unexpected alert open"

然后我尝试了另一种方式,在我接受弹出窗口的地方添加第 4 行。下面是代码:

Then (/^I accept popup for fractional part$/) do
expect(page).to have_content("Only Whole numbers")
page.driver.browser.switch_to.alert.accept 
end

这里我也遇到同样的错误。请指教

要以跨驱动方式与水豚一起执行此操作,您需要稍微更改您的步骤,因为触发警报的步骤需要知道它即将到来

When (/^I do something and accept popup for fractional part$/) do
  accept_alert "Only Whole numbers" do
    # the do something code that triggers the alert box
  end
end

如果您在多个地方使用接受弹出窗口,您可以按照

When (/^(.+) and accept popup for fractional part$/) do |step_name|
  accept_alert "Only Whole numbers" do
    step(step_name)
  end
end

或更普遍

When (/^(.+) and accept "([^"]+)"$/) do |step_name, alert_text|
  accept_alert alert_text do
    step(step_name)
  end
end