SitePrism gives you a simple, clean and semantic DSL for describing your site using the Page Object Model pattern, for use with Capybara in automated acceptance testing.
Before we go into SitePrism, let’s talk about page objects, because page objects are central to understanding the philosophy behind SitePrism.
A Page Objects Primer
Page Objects encapsulate the implementation details of the page (in our example, an HTML document) and expose a specific API that allows you, the tester, to test elements on the page. This encapsulation of details make page objects especially handy as a testing tool.
Let’s illustrate this with an example. Here, we have a page object that inherits from the SitePrism::Page
class:
(class LoginPage < SitePrism::Page
element :username_field, "input[name='username']"
element :password_field, "input[name='password']"
element :flash, "div.flash"
def log_in(username, password)
username_field.set(username)
password_field.set(password)
click_on('Log In')
end
end
class ProfilePage < SitePrism::Page
element :flash, "div.flash"
def flash_message
flash.text
end
end)
Let’s see how we can use our example page object:
( feature 'Login' do
let(:login_page) { LoginPage.new }
before do
login_page.load
end
scenario 'a successful login' do
profile_page = login_page.log_in('admin', 'password')
expect(profile_page.flash_message).to eq("Ohai! Welcome Admin.")
end
end )
The above code snippet illustrates how we can use SitePrism’s page objects in our RSpec feature tests.
Getting Site Prism Into Your Project
Installing Site Prism is a painless affair. In your Gemfile:
( group :test do
# ...
gem 'rspec-rails', '~> 3.0.0'
gem 'capybara-rails'
gem 'site_prism'
# ...
end )
Note that for the example project, we are going to use RSpec 3. Also, you will need Capybara in order to use SitePrism