Skip to content

Instantly share code, notes, and snippets.

@gongo
Last active February 14, 2018 22:14
Show Gist options
  • Save gongo/5855755 to your computer and use it in GitHub Desktop.
Save gongo/5855755 to your computer and use it in GitHub Desktop.
Capybara の稼働状況を animation gif として保存するようなやつ

Requires Library

  • ImageMagick
    • OSX: brew install imagemagick
    • Ubuntu: sudo apt-get install libmagickcore-dev libmagickwand-dev

Setup

bundle install --path vendor/bundle

Run

bundle exec ruby capybara_animation.rb
# -*- coding: utf-8 -*-
require 'RMagick'
require 'capybara'
require 'capybara/session'
module CapybaraAnimation
class Recorder
class << self
def add_frame
tempfile = Tempfile.new(['screenshot', '.gif'])
Capybara.current_session.save_screenshot(tempfile.path)
frames << tempfile
end
def frames
@frames ||= []
end
def output
paths = frames.map(&:path)
images = Magick::ImageList.new(*paths)
images.delay = 50
images.write('/tmp/hoge.gif')
end
end
end
class Runner
include Capybara::DSL
def run
visit '/'
# Google it
within(:css, 'div.tsf-p') do
fill_in 'q', with: 'Capybara'
click_button 'Google 検索'
end
# Result page
click_link 'jnicklas/capybara · GitHub'
# https://github.com/jnicklas/capybara
click_link '.travis.yml'
# https://github.com/jnicklas/capybara/blob/master/.travis.yml
has_text? 'rbx-19mode'
end
end
end
module Capybara
class Session
SAVE_SCREENSHOT_METHODS = [
:attach_file, :check, :choose, :click_link_or_button, :click_button,
:click_link, :fill_in, :select, :uncheck, :unselect, :click_on,
:evaluate_script, :visit
]
SAVE_SCREENSHOT_METHODS.each do |method|
alias_method "after_hook_#{method}".to_sym, method
define_method method do |*args, &block|
send("after_hook_#{method}", *args, &block)
CapybaraAnimation::Recorder.add_frame
end
end
end
end
Capybara.default_driver = :selenium
Capybara.app_host = 'http://www.google.co.jp'
Capybara.run_server = false
CapybaraAnimation::Runner.new.run
CapybaraAnimation::Recorder.add_frame # last frame
CapybaraAnimation::Recorder.output
source 'https://rubygems.org'
gem 'capybara'
gem 'rake'
gem 'selenium-webdriver'
gem 'json'
gem 'rmagick'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment