从Alert中获取文本以与字符串进行比较 - Cucumber + Capybara + Rspec

时间:2017-07-12 15:39:19

标签: ruby rspec capybara

我需要获取警报中显示的文本,以便与测试所期望的文本字符串进行比较。

我正在使用Cucumber + Ruby + rspec和Capybara。

我尝试了以下内容:

Then(/^I see the message "([^"]*)"$/) do |mensagemsucesso|

    TextAlert = page.driver.browser.switch_to.alert.text
    sleep(5)
    Expect(textAlert).to eql mensagemsucesso

End

错误:

  

目前尚未打开任何模态对话框   (硒::的webdriver ::错误:: NoSuchAlertError)

1 个答案:

答案 0 :(得分:1)

假设您正在讨论由JS alertconfirmprompt触发的模态系统警报,那么您需要使用水豚方法accept_alertaccept_confirmdismiss_confirmaccept_promptdismiss_prompt - http://www.rubydoc.info/gems/capybara/Capybara/Session#accept_alert-instance_method

所有这些都将返回模态显示的文本,或者您可以传入一个字符串,该字符串将根据模态的文本进行测试,如果它不匹配则会引发错误。他们还需要采取具有触发模态的动作的块,因此如果单击链接触发它,则您的步骤可能类似于

When /^(?:|I ) click "([^"]*)" and accept "([^"]*)"$/ do |link, msg|
  accept_alert msg do
    click_link(link)
  end
end