使用Capybara Webkit 1.3.1测试警报对话框文本 [英] Test Alert Dialog Text Using Capybara Webkit 1.3.1
问题描述
我希望使用Capybara Webkit在警报对话框中测试文本。我知道 accept_js_confirms
和 reject_js_confirms
方法,但是我想先评估对话框的内容执行动作。例如,在Selenium中,以下工作原理:
I'm looking to test the text in an alert dialog using Capybara Webkit. I'm aware of the accept_js_confirms
and reject_js_confirms
methods, but I'm looking to evaluate the contents of the dialog prior to performing an action. In Selenium for example, the following works:
alert = page.driver.browser.switch_to.alert
Expect(alert.text ).to eq t('report_periods.edit.delete_confirmation')
alert.accept
我正在使用 expect(page.driver.confirm_messages)。包含t('report_periods.edit.delete_confirmation')
来测试同一对话框的文本,但是,在更新了capybara-webkit gem之后我们的测试输出了弃用警告: [DEPRECATION] Capybara :: Webkit :: Driver#confirm_messages已弃用。请改使用Capybara :: Session#accept_confirm或Capybara :: Session#dismiss_confirm。
使用警告中的任何一种方法都不会测试对话框的内容。
I'm currently using expect(page.driver.confirm_messages).to include t('report_periods.edit.delete_confirmation')
to test the text of the same dialog, however, after updating our capybara-webkit gem our tests output a deprecation warning: [DEPRECATION] Capybara::Webkit::Driver#confirm_messages is deprecated. Please use Capybara::Session#accept_confirm or Capybara::Session#dismiss_confirm instead.
Using either of those methods from the warning will not test the contents of the dialog though.
推荐答案
accept_confirm和dismiss_confirm将仅接受或关闭模态。
accept_confirm and dismiss_confirm will only accept or dismiss the modal.
如果要比较文本您可以在变量中获取accept_confirm的返回值,然后再进行比较。
If you want to compare the text the you can take the return value of accept_confirm in a variable and then later you can compare it.
message = accept_prompt(with: 'Linus Torvalds') do
click_link('Show Prompt About Linux')
end
expect(message).to eq('Who is the chief architect of Linux?')
在此处查看模式: https://github.com/jnicklas/capybara
这篇关于使用Capybara Webkit 1.3.1测试警报对话框文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!