Overview
PrismChecker is an extension for rspec and minitest, built on top of the SitePrism
gem and using its page object model.
It allows you to write short, easy-to-read browser tests with clear error messages
Let's assume your application has the following shop cart:
Corresponding SitePrism PDO::
class Cart < SitePrism::Page
set_url '/cart.html'
element :header, 'h1'
sections :cart_items, '[data-test="cart-item"]' do
element :name, '[data-test="cart-item-name"]'
element :image, '[data-test="cart-item-image"]'
element :price, '[data-test="cart-item-price"]'
element :quantity, '[data-test="cart-item-quantity"]'
element :total, '[data-test="cart-item-total"]'
end
section :checkout, '[data-test="cart-checkout"]' do
element :total, '[data-test="cart-checkout-total"]'
element :checkout_button, '[data-test="cart-checkout-button"]'
end
end
A typical test would look like this:
describe 'Cart' do
it 'is correct' do
page = Cart.new
page.load
expect(page.header.text).to eq('Shopping Cart')
expect(page.cart_items.size).to eq(2)
expect(page.cart_items[0].visible?).to be_truthy
expect(page.cart_items[0].name.text).to match('Cup')
expect(page.cart_items[0].quantity.value).to match('1')
expect(page.cart_items[0].image[:src]).to match('cup.png')
expect(page.cart_items[0].price.text).to match('19.00')
expect(page.cart_items[0].total.text).to match('19.00')
expect(page.cart_items[1].visible?).to be_truthy
expect(page.cart_items[1].name.text).to match('Cap')
expect(page.cart_items[1].quantity.value).to match('2')
expect(page.cart_items[1].image[:src]).to match('cap.png')
expect(page.cart_items[1].price.text).to match('24.00')
expect(page.cart_items[1].total.text).to match('48.00')
expect(page.checkout.visible?).to be_truthy
expect(page.checkout.total.text).to match('67.99')
expect(page.checkout.checkout_button.text).to match('Checkout')
end
end
Using gem prism_checker, the same test will look much cleaner and simpler:
describe 'Cart' do
it 'is correct' do
page = Cart.new
page.load
expect(page).to be_like(
header: 'Shopping Cart',
cart_items: [
{
name: 'Cup',
quantity: '1',
image: 'cup.png',
price: '19.00',
total: '19.00'
},
{
name: 'Cap',
quantity: '2',
image: 'cap.png',
price: '24.00',
total: '48.00'
}
],
checkout: {
total: '67.00',
checkout_button: 'Checkout'
}
)
end
end
In case of errors, an easy-to-read message will be displayed:
Install
To install PrismChecker:
RSpec
gem install prism_checker_rspec
MiniTest
gem install prism_checker_minitest
Examples
Common cases
Page and Page Object Model:
class Cart < SitePrism::Page
element :header, 'h1'
sections :cart_items, '[data-test="cart-item"]' do
element :name, '[data-test="cart-item-name"]'
element :price, '[data-test="cart-item-price"]'
end
section :checkout, '[data-test="cart-checkout"]' do
element :total, '[data-test="cart-checkout-total"]'
element :checkout_button, '[data-test="cart-checkout-button"]'
end
end
Text check
@page = Cart.new
page.load
expect(page).to be_like('Shopping Cart')
Element and section check
@page = Cart.new
page.load
expect(page).to be_like(
header: 'Shopping Cart',
checkout: {
total: '67.99',
checkout_button: 'Checkout'
}
)
Element check
@page = Cart.new
page.load
expect(page.header).to be_like('Shopping Cart')
@page = Cart.new
page.load
expect(page).to be_like(header: 'Shopping Cart')
Section check
expect(page.checkout).to be_like(
total: '67.99',
checkout_button: 'Checkout'
)
expect(page).to be_like(
checkout: {
total: '67.99',
checkout_button: 'Checkout'
}
)
Sections check
expect(page).to be_like(
cart_items: [
{
name: 'Cup',
image: 'cup.png'
},
{
name: 'Cap',
image: 'cap.png'
}
])
expect(page).to be_like(cart_items: 2)
expect(page).to be_like(cart_items: [{name: 'Cup'}, {}])
Warning! It is not possible to pass directly array:
expect(page.cart_items).to be_like([{name: 'Cup'}, {name: 'Cap'}])
expect(page).to be_like(cart_items: [{name: 'Cup'}, {name: 'Cap'}])
Elements check
expect(page).to be_like(
items: [
'Item 1',
'Item 2'
])
expect(page).to be_like(items: 2)
HTML elements
Page Object Model:
class HtmlElements < SitePrism::Page
set_url '/html_elements.html'
element :input, 'input.input'
element :textarea, 'textarea.textarea'
element :button, 'button'
element :button_input, 'input.button'
element :image, 'img.image'
elements :radios, 'input.radio'
elements :checkboxes, 'input.checkbox'
elements :selects, 'select.select'
end
Input
expect(page).to be_like(input: 'Some text')
expect(page.input).to be_like(
value: 'Some text',
class: 'input',
readonly: false,
disabled: false
)
Button
expect(page).to be_like(button: 'Button')
expect(page.button).to be_like(
text: 'Button',
disabled: false
)
Textarea
expect(page).to be_like(textarea: 'Some text')
expect(page.textarea).to be_like(
value: /Some text/,
class: 'textarea'
)
Image
expect(page).to be_like(image: /logo.png$/)
expect(page.image).to be_like(
src: 'logo.png',
class: 'logo',
alt: 'Logo'
)
Radio
expect(page).to be_like(radios: [
{
checked: false,
name: 'radio1'
},
{
checked: true,
},
{
checked: false,
},
])
expect(page).to be_like(radios: [
false,
true,
false
])
Check box
expect(page).to be_like(checkboxes: [
{
checked: false,
id: "checkbox-not-checked"
},
{
checked: true,
}
])
expect(page).to be_like(checkboxes: [
false,
true
])
expect(page.checkboxes[0]).to be_like(
checked: false,
id: "checkbox-not-checked"
)
Select
expect(page).to be_like(selects: [
{
value: /^$/,
id: 'select-not-selected'
},
{
value: 'option2',
id: 'select-selected'
}
])
Visibility
Page Object Model:
class Visibility < SitePrism::Page
set_url '/visibility.html'
elements :list_items, 'li'
section :article, 'article' do
end
end
Element or section
expect(page).to be_like(article: :invisible)
Elements or sections
expect(page).to be_like(list_items: [
'Element 1',
:invisible,
:invisible,
:visible
])
Absence
Page Object Model:
class Absence < SitePrism::Page
set_url '/absence.html'
element :article, 'article'
end
Element or section
expect(page).to be_like(article: :absent)
Delay before checking absence
Sometimes we need to sleep before checking absence. Use :absent[delay in seconds]
expect(page).to be_like(article: :absent3)
String comparison
Exact match and inclusion
By default the inclusion of a substring in a string is checked
PrismChecker.string_comparison = :inclusion
expect(page.header).to be_like('Shopping Cart')
To compare strings exactly, use :exact
PrismChecker.string_comparison = :exact
expect(page.header).to be_like('Shopping Cart')
Empty string
When the comparison method is :inclusion, and it is necessary to compare with an empty string, it can be done in this way
expect(page.element_without_text).to be_like(:empty)
or
expect(page.element_without_text).to be_like(/^$/)