- $ rails new memlog // .railsrcで bundle installはskip
- https://gist.github.com/saiwaki/b4bf543977f1bbf89793
- $ bundle install
- $ rails g rspec:install
- $ guard init // Guardfile作成
- .rspecファイル => --format documentation
- rails_helper.rb 編集
- spec/features 作成
- spec/support 作成
rails_helper.rb
rails_helper.rb
require 'rspec/rails'
require 'capybara/rails'
require 'capybara/rspec'
require 'selenium-webdriver'
require 'turnip'
require 'turnip/capybara'
# Capybara
Capybara.server_port = 57124
Capybara.default_wait_time = 10
Capybara.javascript_driver = ENV['DRIVER'] ? ENV['DRIVER'].to_sym : :webkit
Capybara.default_driver = ENV['DRIVER'] ? ENV['DRIVER'].to_sym : :webkit
# FactoryGirl
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
support/capybara_drivers.rb
=begin
Stopped to use 'poltergeist' for headless test.
because I can't resolve below.
Poltergeist detected another element with CSS selector 'html body.phone div.modal-backdrop.fade.in' at this position. It may be overlapping the element you are trying to interact with. If you don't care about overlapping elements, try using node.trigger('click').
=end
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome)
end
Capybara.register_driver :safari do |app|
Capybara::Selenium::Driver.new(app, browser: :safari)
end
Capybara.register_driver :firefox do |app|
Capybara::Selenium::Driver.new(app, browser: :firefox)
end
Capybara.register_driver :ie do |app|
Capybara::Selenium::Driver.new app,
browser: :remote,
url: 'http://10.0.1.8:4444/wd/hub',
desired_capabilities: :internet_explorer
end
Capybara.register_driver :iphone do |app|
Capybara.default_wait_time = 30
Capybara::Selenium::Driver.new app,
browser: :remote,
url: "http://127.0.0.1:4723/wd/hub",
desired_capabilities: {
browserName: 'iphone',
platform: 'Mac',
version: '6.1',
app: 'safari'
}
end
Capybara.register_driver :ipad do |app|
Capybara.default_wait_time = 30
Capybara::Selenium::Driver.new app,
browser: :remote,
url: "http://127.0.0.1:4723/wd/hub",
desired_capabilities: {
browserName: 'ipad',
platform: 'Mac',
version: '6.1',
app: 'safari',
deviceOrientation: 'landscape'
}
end
Capybara.register_driver :android do |app|
Capybara.default_wait_time = 30
Capybara::Selenium::Driver.new app,
browser: :remote,
url: "http://127.0.0.1:4723/wd/hub",
desired_capabilities: {
browserName: 'android',
platform: 'Mac',
version: '2.3',
app: 'safari',
deviceOrientation: 'landscape'
}
end
Capybara.register_driver :phone do |app|
Capybara::Webkit::Driver.new(app)
end
Capybara.register_driver :tablet do |app|
Capybara::Webkit::Driver.new(app)
end
if %i(iphone ipad phone tablet ios android).include?(Capybara.javascript_driver)
$el = {
edit_title: '#edit-page-title-phone',
edit_body: '#edit-page-body-phone',
edit_save: '#edit-page-save-phone'
}
RSpec.configure do |config|
config.before :each do
end
end
else
$el = {
edit_title: '#edit-page-title',
edit_body: '#edit-page-body',
edit_save: '#edit-page-save'
}
end
support/wait_until.rb
def wait_until(time=Capybara.default_wait_time)
require "timeout"
Timeout.timeout(time) do
sleep(0.1) until value = yield
value
end
end
def wait_until_visible(selector, time=Capybara.default_wait_time)
wait_until(time) do
expect(page).to have_selector(selector, visible: true)
end
end
def wait_and_find_css(selector, time=Capybara.default_wait_time)
wait_until(time) do
page.has_css?(selector)
end
find(selector)
end
alias wait_and_find wait_and_find_css
def wait_and_find_xpath(selector, time=Capybara.default_wait_time)
wait_until(time) do
page.has_xpath?(selector)
end
find(:xpath, selector)
end
support/window.rb
def within_window(name)
sleep 0.5
original = current_window
use_window(name)
yield
ensure
use_window(original)
end