Module: Calabash::Cucumber::PlaybackHelpers

Included in:
Core, PlaybackActions
Defined in:
lib/calabash-cucumber/playback_helpers.rb

Constant Summary collapse

DATA_PATH =
File.expand_path(File.dirname(__FILE__))

Instance Method Summary collapse

Instance Method Details

#find_compatible_recording(recording_name, os, rec_dir, device, candidates) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/calabash-cucumber/playback_helpers.rb', line 90

def find_compatible_recording (recording_name, os, rec_dir, device, candidates)
  recording = recording_name_for(recording_name, os, device)
  data = load_recording(recording, rec_dir)
  if data.nil?
    candidates << recording
    version_counter = os[-1, 1].to_i
    loop do
      version_counter = version_counter - 1
      break if version_counter < 5
      loop_os = "ios#{version_counter}"
      recording = recording_name_for(recording_name, loop_os, device)
      candidates << recording
      data = load_recording(recording, rec_dir)
      #noinspection RubyControlFlowConversionInspection
      break if !data.nil?
    end
  end
  data
end

#interpolate(recording, options = {}) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/calabash-cucumber/playback_helpers.rb', line 130

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

#load_playback_data(recording_name, options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/calabash-cucumber/playback_helpers.rb', line 42

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 <<EOF
    Unable to determine iOS major version
    Most likely you have updated your calabash-cucumber client
    but not your server. Please follow closely:

https://github.com/calabash/calabash-ios/wiki/B1-Updating-your-Calabash-iOS-version

    If you are running version 0.9.120+ then please report this message as a bug.
EOF
    end
    os = "ios#{major}"
  end

  rec_dir = ENV['PLAYBACK_DIR'] || "#{Dir.pwd}/features/playback"

  candidates = []
  data = find_compatible_recording(recording_name, os, rec_dir, device, candidates)

  if data.nil? and device=='ipad'
    if ENV['CALABASH_FULL_CONSOLE_OUTPUT'] == '1'
      puts "Unable to find recording for #{os} and #{device}. Trying with #{os} iphone"
    end
    data = find_compatible_recording(recording_name, os, rec_dir, 'iphone', candidates)
  end

  if data.nil?
    searched_for = "  searched for => \n"
    candidates.each { |file| searched_for.concat("    * '#{file}'\n") }
    searched_in = "  in directories =>\n"
    playback_file_directories(rec_dir).each { |dir| searched_in.concat("    * '#{dir}'\n") }
    raise "Playback file not found for: '#{recording_name}'\n#{searched_for}#{searched_in}"
  end

  data
end

#load_recording(recording, rec_dir) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/calabash-cucumber/playback_helpers.rb', line 17

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

#playback(recording, options = {}) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/calabash-cucumber/playback_helpers.rb', line 110

def playback(recording, options={})
  data = load_playback_data(recording)

  post_data = %Q|{"events":"#{data}"|
  post_data<< %Q|,"query":"#{escape_quotes(options[:query])}"| if options[:query]
  post_data<< %Q|,"offset":#{options[:offset].to_json}| if options[:offset]
  post_data<< %Q|,"reverse":#{options[:reverse]}| if options[:reverse]
  post_data<< %Q|,"uia_gesture":"#{options[:uia_gesture]}"| if options[:uia_gesture]
  post_data<< %Q|,"prototype":"#{options[:prototype]}"| if options[:prototype]
  post_data << '}'

  res = http({:method => :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

#playback_file_directories(rec_dir) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/calabash-cucumber/playback_helpers.rb', line 33

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

#record_beginObject



149
150
151
# File 'lib/calabash-cucumber/playback_helpers.rb', line 149

def record_begin
  http({:method => :post, :path => 'record'}, {:action => :start})
end

#record_end(file_name) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/calabash-cucumber/playback_helpers.rb', line 153

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 <<EOF
    Unable to determine iOS major version
    Most likely you have updated your calabash-cucumber client
    but not your server. Please follow closely:

https://github.com/calabash/calabash-ios/wiki/B1-Updating-your-Calabash-iOS-version

    If you are running version 0.9.120+ then please report this message as a bug.
EOF
    end
    os = "ios#{major}"
  end

  file_name = "#{file_name}_#{os}_#{device}.base64"
  system('/usr/bin/plutil -convert binary1 -o _recording_binary.plist _recording.plist')
  system("openssl base64 -in _recording_binary.plist -out '#{file_name}'")
  system('rm _recording.plist _recording_binary.plist')

  rec_dir = ENV['PLAYBACK_DIR'] || "#{Dir.pwd}/features/playback"
  unless File.directory?(rec_dir)
    if ENV['CALABASH_FULL_CONSOLE_OUTPUT'] == '1'
      puts "creating playback directory at '#{rec_dir}'"
    end
    system("mkdir -p #{rec_dir}")
  end

  system("mv #{file_name} #{rec_dir}")
  "#{file_name} ==> '#{rec_dir}/#{file_name}'"

end

#recording_name_for(recording_name, os, device) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/calabash-cucumber/playback_helpers.rb', line 8

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