Skip to content

Instantly share code, notes, and snippets.

@jsteiner
Created April 22, 2013 14:27
Show Gist options
  • Save jsteiner/5435508 to your computer and use it in GitHub Desktop.
Save jsteiner/5435508 to your computer and use it in GitHub Desktop.
Example of page objects
require 'spec_helper'
feature 'Guest manages favorites', :js do
scenario 'can interact with the list of favorites' do
property = create(:property, :boston)
alternate_property = create(:property, :boston)
make_all_properties_available
search do
location 'Boston'
end
property_on_page = PropertyOnPage.new(property)
alternate_property_on_page = PropertyOnPage.new(alternate_property)
property_on_page.add_to_favorites
expect(property_on_page).to be_favorited
alternate_property_on_page.view_property_detail_from_results
expect(alternate_property_on_page).to have_detail_visible
property_on_page.view_property_detail_from_favorites
expect(property_on_page).to have_detail_visible
property_on_page.remove_from_favorites
expect(property_on_page).not_to be_favorited
end
end
class PropertyOnPage
include Capybara::DSL
def initialize(property)
@property = property
end
def view_property_detail_from_results
unless has_detail_visible?
within property_in_results do
view_property_detail
end
end
end
def view_property_detail_from_favorites
within favorites_list do
view_property_detail
end
end
def add_to_favorites
view_property_detail_from_results
within property_detail do
click_on 'Add to Favorites'
end
end
def remove_from_favorites_from_detail
view_property_detail_from_results
within property_detail do
click_on 'Remove from Favorites'
end
end
def remove_from_favorites
within property_in_favorites do
find('.remove').click
end
end
def check_availability_for_favorites
add_to_favorites
click_on 'Check Availability'
end
def check_availability
view_property_detail_from_results
click_on 'Select Hotel'
end
def favorited?
begin
property_in_favorites.visible?
rescue Capybara::ElementNotFound
false
end
end
def has_favorite_button_disabled?
view_property_detail_from_results
!!find('.favorites-button')['disabled']
end
def has_detail_visible?
begin
property_detail.visible? &&
property_in_results['class'].include?('active')
rescue Capybara::ElementNotFound
false
end
end
private
def view_property_detail
find('.title', text: property_text).click
end
def property_detail
find('.hotel-details', text: property_text)
end
def property_in_results
find('.hotel', text: property_text)
end
def property_in_favorites
find('.favorites-item', text: property_text)
end
def favorites_list
find('.favorites')
end
def property_text
/#{@property.name}\b/
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment