Skip to content

Instantly share code, notes, and snippets.

@amyroi
Last active September 7, 2015 07:12
Show Gist options
  • Save amyroi/c1cb5ec74314da446e40 to your computer and use it in GitHub Desktop.
Save amyroi/c1cb5ec74314da446e40 to your computer and use it in GitHub Desktop.
# spec/support/site_prism/user_page.rb
class UserPage
class FormBase < SitePrism::Section
element :first_name, "input[name='user[first_name]']"
element :last_name, "input[name='user[last_name]']"
element :button, "input[name='commit']"
def input(args)
first_name.set args[:first_name]
last_name.set args[:last_name]
button.click
end
end
class Form < FormBase
end
class SearchForm < FormBase
element :accordion, :link, nil, href: '#search-form'
element :reset, 'button'
end
class PageBase < SitePrism::Page
element :title, 'h1'
end
class Index < PageBase
set_url 'users'
element :new_link, :link, nil, href: '/users/new'
section :search_form, SearchForm, '#search-form'
element :show_link, :link, I18n.t('simple_form.show')
element :edit_link, :link, I18n.t('simple_form.edit')
element :destroy_link, :link, I18n.t('simple_form.destroy')
element :success_destroy, '.alert-success', I18n.t('simple_form.notice.destroy')
end
class New < PageBase
set_url 'users/new'
section :form, Form, '#new_user'
element :back_btn, :link, nil, href: '/users'
element :error, '.alert', I18n.t('simple_form.default_message.error_notification')
end
class Edit < PageBase
set_url 'users/{id}'
element :back_btn, :link, nil, href: '/users'
section :form, Form, '.edit_user'
end
class Show < PageBase
set_url 'users/{id}'
element :back_btn, :link, nil, href: '/users'
element :edit_btn, :link, nil, href: '/users/{id}/edit'
element :success_create, '.alert-success', I18n.t('simple_form.notice.create')
element :success_update, '.alert-success', I18n.t('simple_form.notice.update')
end
end
# spec/feature/user_spec.rb
require 'rails_helper'
feature 'users', type: :feature do
include_context 'システム管理者がログインしている'
feature 'creates user' do
let(:index_page) { UserPage::Index.new }
let(:new_page) { UserPage::New.new }
let(:edit_page) { UserPage::Edit.new }
let(:show_page) { UserPage::Show.new }
let(:user) { build(:user) }
scenario 'with invalid input' do
index_page.load
index_page.new_link.click
expect(new_page).to have_back_btn
new_page.form.button.click
expect(new_page).to have_error
new_page.form.first_name.set user.first_name
new_page.form.button.click
expect(new_page).to have_error
new_page.form.input(first_name: '', last_name: user.last_name)
expect(new_page).to have_error
end
scenario '新規作成から検索・編集・削除が問題なく操作できる' do
index_page.load
index_page.new_link.click
new_page.form.input(first_name: user.first_name, last_name: user.last_name)
# 詳細
expect(show_page).to have_success_create
expect(show_page.title.text).to eql I18n.t('activerecord.models.user') + I18n.t('simple_form.show')
expect(show_page).to have_content user.first_name
expect(show_page).to have_content user.last_name
show_page.back_btn.click
# 一覧
expect(index_page.title.text).to eql I18n.t('activerecord.models.user') + I18n.t('simple_form.list')
expect(index_page).to have_content '計1件'
expect(index_page).to have_content user.first_name
expect(index_page).to have_content user.last_name
# 検索結果
index_page.search_form.input(first_name: user.first_name, last_name: user.last_name)
expect(index_page).to have_content '計1件'
expect(index_page).to have_content user.first_name
expect(index_page).to have_content user.last_name
# 編集
index_page.edit_link.click
expect(edit_page.title.text).to eql I18n.t('activerecord.models.user') + I18n.t('simple_form.edit')
expect(edit_page).to have_field '年', user.first_name
expect(edit_page).to have_field User.human_attribute_name(:last_name), user.last_name
edit_page.form.input(first_name: user.first_name + 1, last_name: user.last_name + 1)
expect(show_page).to have_success_update
expect(show_page).to have_content user.first_name + 1
expect(show_page).to have_content user.last_name + 1
show_page.back_btn.click
# 削除
index_page.destroy_link.click
expect(index_page).to have_success_destroy
expect(index_page).to_not have_content user.first_name
expect(index_page).to_not have_content user.last_name
expect(index_page).to have_content '計0件'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment