module Calabash module Cucumber module PlaybackHelpers DATA_PATH = File.expand_path(File.dirname(__FILE__)) def recording_name_for(recording_name, os, device) #noinspection RubyControlFlowConversionInspection if !recording_name.end_with? '.base64' "#{recording_name}_#{os}_#{device}.base64" else recording_name end end def load_recording(recording, rec_dir) directories = playback_file_directories(rec_dir) directories.each { |dir| path = "#{dir}/#{recording}" if File.exists?(path) # useful for debugging recordings, but too verbose for release # suggest (yet) another variable CALABASH_DEBUG_PLAYBACK ? #if ENV['CALABASH_FULL_CONSOLE_OUTPUT'] == '1' # puts "found compatible playback: '#{path}'" #end return File.read(path) end } nil end def playback_file_directories (rec_dir) # rec_dir is either ENV['PLAYBACK_DIR'] or ./features/playback [File.expand_path(rec_dir), "#{Dir.pwd}", "#{Dir.pwd}/features", "#{Dir.pwd}/features/playback", "#{DATA_PATH}/resources/"].uniq end def load_playback_data(recording_name, options={}) os = options['OS'] || ENV['OS'] device = options['DEVICE'] || ENV['DEVICE'] || 'iphone' unless os if @calabash_launcher && @calabash_launcher.active? major = @calabash_launcher.ios_major_version else major = Calabash::Cucumber::SimulatorHelper.ios_major_version end unless major raise < :post, :raw => true, :path => 'play'}, post_data) res = JSON.parse(res) if res['outcome'] != 'SUCCESS' raise "playback failed because: #{res['reason']}\n#{res['details']}" end res['results'] end def interpolate(recording, options={}) data = load_playback_data(recording) post_data = %Q|{"events":"#{data}"| post_data<< %Q|,"start":"#{escape_quotes(options[:start])}"| if options[:start] post_data<< %Q|,"end":"#{escape_quotes(options[:end])}"| if options[:end] post_data<< %Q|,"offset_start":#{options[:offset_start].to_json}| if options[:offset_start] post_data<< %Q|,"offset_end":#{options[:offset_end].to_json}| if options[:offset_end] post_data << '}' res = http({:method => :post, :raw => true, :path => 'interpolate'}, post_data) res = JSON.parse(res) if res['outcome'] != 'SUCCESS' raise "interpolate failed because: #{res['reason']}\n#{res['details']}" end res['results'] end def record_begin http({:method => :post, :path => 'record'}, {:action => :start}) end def record_end(file_name) res = http({:method => :post, :path => 'record'}, {:action => :stop}) File.open('_recording.plist', 'wb') do |f| f.write res end device = ENV['DEVICE'] || 'iphone' os = ENV['OS'] unless os if @calabash_launcher && @calabash_launcher.active? major = @calabash_launcher.ios_major_version else major = Calabash::Cucumber::SimulatorHelper.ios_major_version end unless major raise < '#{rec_dir}/#{file_name}'" end end end end