Module: Calabash::Cucumber::PlaybackHelpers

Includes:
Logging
Included in:
Core
Defined in:
lib/calabash-cucumber/playback_helpers.rb

Overview

Note:

In iOS 7, the record-and-playback feature was dropped from UIAutomation. As such, using the record-and-playback API is not recommended.

Note:

We expect that this module will be deprecated once iOS 6 testing is no longer supported.

This module provides methods for interaction with UIAutomation’s record-and-playback features.

Instance Method Summary collapse

Methods included from Logging

#calabash_info, #calabash_warn

Instance Method Details

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

Plays back a recording but interpolates it first.

Parameters:

  • recording (String)

    the filename of the recording

  • options (Hash) (defaults to: {})

    can control the behavior of the recording



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/calabash-cucumber/playback_helpers.rb', line 145

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

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

Plays back a recording.

Parameters:

  • recording (String)

    the filename of the recording

  • options (Hash) (defaults to: {})

    can control the behavior of the recording



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/calabash-cucumber/playback_helpers.rb', line 122

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

#record_beginObject

Begins a recording.



165
166
167
# File 'lib/calabash-cucumber/playback_helpers.rb', line 165

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

#record_end(file_name) ⇒ Object

Ends a recording and saves it.

Parameters:

  • file_name (String)

    where to save the recording.



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
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/calabash-cucumber/playback_helpers.rb', line 171

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'

  major = Calabash::Cucumber::Launcher.launcher.ios_major_version

  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}"

  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 full_console_logging?
      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