Skip to content

Instantly share code, notes, and snippets.

@yurikoval
Last active March 12, 2016 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yurikoval/97af1eb6ec0dcfa94156 to your computer and use it in GitHub Desktop.
Save yurikoval/97af1eb6ec0dcfa94156 to your computer and use it in GitHub Desktop.
Take screenshot on capybara's every browser action.
class Capybara::Session
%i(visit click_on fill_in).each do |method_name|
original_method_name = "old_#{method_name}".to_sym
alias_method original_method_name, method_name
define_method method_name do |*args|
return_value = send original_method_name, *args
save_screenshot(save_path)
increment_counter
return_value
end
private
def save_path
timestamp = Time.new.strftime("%Y%m%d%H%M%S")
name_parts = ['step', '%03d' % counter, timestamp, rand(10**10)]
"steps/#{@s_now ||= timestamp}/#{name_parts.join(?_)}.png"
end
def increment_counter
@counter = counter + 1
end
def counter
@counter || 1
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment