Last active
October 26, 2016 12:30
-
-
Save codenamev/8696027 to your computer and use it in GitHub Desktop.
Simple base class implementing "Page Objects"
as referenced: http://gaslight.co/blog/6-ways-to-remove-pain-from-feature-testing-in-ruby-on-rails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class PageObject | |
| include Capybara::DSL | |
| include RSpec::Matchers | |
| include Rails.application.routes.url_helpers | |
| attr_accessor :path, :object | |
| def initialize(object = nil) | |
| raise "#{self.class} must be initialized with a model instance" unless !is_singular_object_page? or object | |
| @path = "#{path_name}_path" | |
| @object = object | |
| visit_page | |
| end | |
| def visit_page(options = {}) | |
| if @object.is_a? Hash | |
| visit send(@path, @object.merge(options)) | |
| else | |
| visit send(@path, @object, options) | |
| end | |
| end | |
| private | |
| def path_name | |
| self.class.to_s.gsub(/Page\Z/, '').underscore | |
| end | |
| def is_singular_object_page? | |
| (!path_name.ends_with?('s') and !path_name.starts_with?('new_')) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class PostPage < PageObject | |
| def has_category?(category) | |
| has_css?('.category', category) | |
| end | |
| def has_categories?(*categories) | |
| categories.each do |category| | |
| has_category?(category) | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'spec_helper' | |
| feature 'User views post' do | |
| let(:post) { create :post, categories: [ruby_category, ninja_category] } | |
| let(:ruby_category) { create :category, name: 'ruby' } | |
| let(:ninja_category) { create :category, name: 'ninja' } | |
| let(:post_page) { PostPage.new } | |
| scenario "User sees categories within a post" do | |
| expect(post_page).to have_categories(ruby_category, ninja_category) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment