Last active
August 26, 2015 13:57
-
-
Save ryz310/a5814a629782721f489e to your computer and use it in GitHub Desktop.
feature js: true のテストを実行する際、実行毎にスクリーンショットを自動的に保存します。また、scenario 内に小さな example を記述できます。scenario 内で flow(alias: chapter) ブロックを作成し、ブロック内で expect を実行すると、一連の操作をテストすることが可能となります。
This file contains 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
# USAGE | |
# - spec/rails_helper.rb | |
# RSpec.configure do |config| | |
# config.include FeatureSpecMacro, type: :feature | |
# end | |
module FeatureSpecMacro | |
RSpec.configure do |config| | |
config.before(:suite) do | |
unless ENV['CIRCLE_ARTIFACTS'] | |
if Dir.exist? File.join('tmp', 'screenshot') | |
FileUtils.rm_r File.join('tmp', 'screenshot') | |
end | |
end | |
end | |
config.before(:each, js: true) do |example| | |
@current_example ||= example | |
@screenshot_path ||= | |
@current_example.location.match(%r!^./spec/[^/]+/(.+).rb:\d+$!)[1] | |
end | |
config.after(:each, js: true) do |example| | |
debug_screenshot File.join( | |
example.location.match(%r!^./spec/[^/]+/(.+).rb:\d+$!)[1], | |
"#{ example.full_description }.png" | |
) | |
page.driver.reset! | |
end | |
end | |
# screnario 内で複数の expect を実行する際のシンタックスシュガー | |
def flow(name, screenshot: true) | |
@count ||= 0 | |
message = "#{ current_indentation }#{ @count += 1 }. #{ name }" | |
debug_screenshot File.join( | |
@screenshot_path, | |
"#{ message.gsub('/', '_') }_1_before.png" | |
) | |
yield if block_given? | |
colorize_puts 32, message | |
rescue RSpec::Expectations::ExpectationNotMetError => e | |
colorize_puts 31, message | |
raise e | |
ensure | |
debug_screenshot File.join( | |
@screenshot_path, | |
"#{ message.gsub('/', '_') }_2_after.png" | |
) | |
end | |
alias_method :chapter, :flow | |
# 現在の画面スクリーンショットを取得する | |
def debug_screenshot(file_path, full: true) | |
screenshot_file_name = | |
if ENV['CIRCLE_ARTIFACTS'] | |
File.join(ENV['CIRCLE_ARTIFACTS'], 'screenshot', file_path) | |
else | |
File.join('tmp', 'screenshot', file_path) | |
end | |
page.save_screenshot(screenshot_file_name, full: full) | |
unless ENV['CIRCLE_ARTIFACTS'] | |
# リアルタイムに現在処理中のファイルを表示する | |
FileUtils.copy( | |
screenshot_file_name, | |
File.join('tmp', 'screenshot', 'poltergeist_last_page.png') | |
) | |
end | |
end | |
private | |
# 色付きでコンソール出力を行う | |
def colorize_puts(color_code, text) | |
puts "\e[#{ color_code }m#{ text }\e[0m" | |
end | |
# メッセージのインデントを取得する | |
def current_indentation | |
unless @group_level | |
@group_level = 0 | |
example_group = @current_example.metadata[:example_group] | |
until example_group.nil? do | |
example_group = example_group[:parent_example_group] | |
@group_level += 1 | |
end | |
end | |
' ' * @group_level | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment