riddler0212
riddler0212

Reputation: 105

How to get a detailed error message in Capybara unit tests

How do I get a detailed error message in Capybara unit tests?

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

This tests for title to be "About".

How do I add a custom error message like:

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

Upvotes: 1

Views: 1029

Answers (2)

Sarabjit Singh
Sarabjit Singh

Reputation: 790

You can add custom error messages as shown below, also you should add to take screenshot to debug the issue.

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

Hope this will Helps :)

Upvotes: 0

MilesStanfield
MilesStanfield

Reputation: 4639

You can add a custom error message described in "Customized message" like this:

it "should have the title 'About'" do
  visit '/static_pages/about'
  expect(page).to have_title("About"), lambda { "Expected 'About' but found '#{page.first("title", visible: false).native.text}'. Please Rectify the mistake."}
end

Upvotes: 4

Related Questions