如何在Capybara单元测试中获取详细的错误消息

时间:2016-01-26 17:28:44

标签: ruby rspec capybara

如何在Capybara单元测试中获得详细的错误消息?

describe "About" do
it "should have the h1 'About Us'" do
  visit '/static_pages/about'
  page.should have_selector('h1', 
    :text => "About Us")
end
it "should have the title 'About'" do
  visit '/static_pages/about'
page.should have_title("About")
end

这将测试标题为"关于"。

如何添加自定义错误消息,如:

Expected "About" but found "ABT". Please Rectify the mistake.

2 个答案:

答案 0 :(得分:3)

您可以像这样添加“Customized message”中描述的自定义错误消息:

Print()

答案 1 :(得分:0)

您可以添加自定义错误消息,如下所示,您还应该添加以截屏以调试问题。

describe "About" do
it "should have the h1 'About Us'" do
  visit '/static_pages/about'
  page.should have_selector('h1', 
    :text => "About Us")
end
it "should have the title 'About'" do
  visit '/static_pages/about'
  textToSearch="About"
  begin
    page.should have_title("#{textToSearch}")
  rescue Exception => e
    puts "Expected '#{textToSearch}' but found '#{page.first("title", visible: false).native.text}'. Please Rectify the mistake."
    randomNumber=rand(100000)
    page.save_screenshot("abc-#{randomNumber}.png",:full=>true)
    raise e
  end
end

希望这会有助于:)