Skip to content

Instantly share code, notes, and snippets.

@sleekweasel
Last active February 10, 2019 11:44
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sleekweasel/227315961b0b102e82d9 to your computer and use it in GitHub Desktop.
Save sleekweasel/227315961b0b102e82d9 to your computer and use it in GitHub Desktop.
Hooks for recording video of cucumber tests
require 'open3'
require 'timeout'
require 'process'
def video_file(infix, ext = 'mp4')
"video_#{infix}.#{ext}"
end
capture_video = (ENV['CAPTURE_VIDEO'] || 'failure').downcase
class ScreenRecord
@@set = false
@@screenrecord = nil
def self.cmd
return @@screenrecord if @@set
out = Open3.capture2e("#{adb_command} shell screenrecord --help")
@@set = true
@@screenrecord =
case out
when /--bugreport/
'screenrecord --bugreport'
when /not found/
nil
else
'screenrecord'
end
end
end
class AndroidVideoCapture
def start_capture(_embedder)
if ScreenRecord.cmd
@video_file = video_file(Time.now.strftime('%H%M%S'))
@screenrecord = Open3.popen2e("#{adb_command} shell")
@screenrecord[0] << "rm -rf /sdcard/#{video_file('*')}\n"
@screenrecord[0] << "#{ScreenRecord.cmd} --verbose --bit-rate 100000 /sdcard/#{@video_file} &\n"
@screenrecord[0] << "CPID=$!\n"
end
end
def stop_capture(_embedder)
@screenrecord[0] << "if ps | grep screenrecord | grep $CPID; then kill -2 $CPID ; fi\n"
@screenrecord[0] << "while ps | grep screenrecord | grep $CPID ; do sleep 1; done; exit\n"
begin
Timeout.timeout(5) { @screenrecord[2].value }
rescue Timeout::Error
Process.detach(@screenrecord[2].pid)
end
File.open("#{@video_file}.log", 'w') { |f| f << @screenrecord[1].readlines.join("\n") }
@screenrecord[0].close
@screenrecord[1].close
end
def acquire_capture(embedder)
system "#{adb_command} pull /sdcard/#{@video_file} ."
if File.exist?(@video_file)
embedder.embed(@video_file, 'video/mp4', "video #{@video_file}")
# embed("#{@video_file}.log", 'text/plain', 'capture log')
end
end
def dispose_capture(_embedder)
File.delete("#{@video_file}.log")
end
end
class MacQuickTimeCapture
# http://www.paulcalnan.com/archives/2014/10/quicktime-screen-recording-of-a-single-window.html
def start_capture(_embedder)
%x( osascript #{File.dirname(__FILE__)}/hooks_video_ios/videocapture.scpt start Simulator 1 #{File.dirname(__FILE__)}/hooks_video_ios/clickdrag )
end
def stop_capture(_embedder)
end
def acquire_capture(embedder)
@video_base = video_file(Time.now.strftime('%H%M%S'), '')
# Sandboxing requires saving to somewhere official first. As if someone couldn't move it afterwards. Duh.
qtpxcomp = "#{ENV['HOME']}/Movies/#{@video_base}qtpxcomposition"
%x( osascript #{File.dirname(__FILE__)}/hooks_video_ios/videocapture.scpt save #{qtpxcomp} )
qtpxmovie = "#{qtpxcomp}/Screen Recording.mov"
if File.exist?(qtpxmovie)
FileUtils.mv(qtpxmovie, "#{@video_base}mov")
embedder.embed("#{@video_base}mov", 'video/mp4', "Movie #{@video_base}mov")
end
end
def dispose_capture(_embedder)
%x( osascript #{File.dirname(__FILE__)}/hooks_video_ios/videocapture.scpt stop )
end
end
Before do
@video_capture = nil
if capture_video != 'never'
@video_capture = case
when OS == ANDROID
AndroidVideoCapture.new
when OS == IOS && ENV['IOS_VIDEO_CAPTURE'] == 'quicktime'
MacQuickTimeCapture.new
# else
# nil # Pending TightVNC patches.
end
@video_capture.start_capture(self) if @video_capture
end
end
After do |scenario|
unless @video_capture.nil?
@video_capture.stop_capture(self)
if scenario.failed? || scenario.source_tag_names.include?('@capture_video') || capture_video == 'always'
@video_capture.acquire_capture(self)
else
@video_capture.dispose_capture(self)
end
end
end
@renatodefarias
Copy link

Hello,

I saw your code and try execute my cucumber scenario recording, but i got this error:

Failed to open TCP connection to 127.0.0.1:4723 (No connection could be made because the target machine actively refused it. - connect(2) for "127.0.0.1" port 4723) (Errno::ECONNREFUSED)

You have any idea the reason of this?

Thanks for help and sorry for bad english :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment