Feature test with rspec + capybara + poltergeist

Behaviour driven test is important in agile development. Sometimes, we may face issue when development new function and it crashes the previous built function. It is dangerous if it’s test manually by human and it’ll take up quite a lot of time when repeating the test manually. Thanks to rspec test in rails, we can automate the testing process.

Sometimes we would like to automate our test with javascript instead of only testing only the controller and model. Poltergeist will do the job for us, as it can run the test with javascript.

Gems

  • rspec-rails
  • rspec-core
  • capybara
  • poltergeist

Project Setup

  1. Setup the rails_helper.rb
# rails_helper.rb gist content here
require "rails_helper"
include Warden::Test::Helpers

RSpec.feature "when user visit to root page", :type => :feature do
  scenario "expect page to have test content", js:true do
    
    visit root_path
    # this can trigger javascrip load
    within "#content" do
      page.find(".link").click
    end
    expect(page).to have_content("test")
    
  end
end
  1. Start testing with Poltergeist
# spec/rails_helper.rb
require 'capybara/poltergeist'

Capybara.javascript_driver = :poltergeist

options = {js_errors: false}
Capybara.register_driver :poltergeist do |app|
  Capybara::Poltergeist::Driver.new(app, options)
end