Capybara, save_and_open_page for the lazy!

- 1 min

When I’m writting Rails integration tests with capybara, every once in a while there is something not working as I wish. When misfourtune occures, usually I want to see how my rendered page looks like save_and_open_page to the rescue!

Lets for the moment imagine that we are testing site navigation links. When Guest clicks on About navigation link, he/she should see About page.

describe "Guest sees the about page" do
  it "when about page link is clicked" do
    visit root_path

    click_link "About"

    expect(page).to have_css ".title", text: "About page"
  end
end

After running test, it is failling

Hmm….that is strange! Lets see what my page looks like, just add save_and_open_page method call!

describe "Guest sees the about page" do
  it "when about page link is clicked" do
    visit root_path

    click_link "About"

    save_and_open_page
    expect(page).to have_css ".title", text: "About page"
  end
end

Capybara will open browser and render my current page. Sueprise….surprise, there is no about link rendered.

Thanks save_and_open_page you just save me a lot of the debugging!

Only thing that bugs me with save_and_open_page is size of method name, I’m just lazy typing it. It would be a lot nicer if I could just type page!.

Well… I could, lest write small Capybara extenstion and add it to spec/support directory.

Now I could just type page!

describe "Guest sees the about page" do
  it "when about page link is clicked" do
    visit root_path

    click_link "About"

    page!
    expect(page).to have_css ".title", text: "About page"
  end
end
Dino Maric

Dino Maric

Working class hero

comments powered by Disqus
rss facebook twitter github youtube mail spotify instagram linkedin google pinterest medium