Skip to content

Instantly share code, notes, and snippets.

@sunekaae
Created August 5, 2012 08:43
Show Gist options
  • Save sunekaae/3263012 to your computer and use it in GitHub Desktop.
Save sunekaae/3263012 to your computer and use it in GitHub Desktop.
# This is the same regex/phrasing as the original step from the compendium
# It's just passes on the work to a method which can be used via code from other code
Then /^I save a screenshot with prefix (\w+)$/ do |prefix|
screenshotFromPrefix(prefix)
end
def screenshotFromPrefix prefix
# contruct filename based on the prefix and a timestamp (in millis to avoid overwriting screenshots taken close to each other)
filenameWithRelativePath = "#{prefix}_#{DateTime.now.strftime('%Q')}.png"
# use iOS-Simulator Cropper if it's available, and use the standard screencapture if it's not available
if (isCropperAvailable) then
screenshotWithCropper(filenameWithRelativePath)
else
screenshotWithScreencapture(filenameWithRelativePath)
end
sleep 0.5 # wait a bit to give any transitions and screenshot time to finish.
# add thumbnail of screenshot to the standard cucumber report as an HTML <img> element, with <a> link to full size
showImageInReport(filenameWithRelativePath)
end
# return true/false depending on whether the cropper tool is available for use
def isCropperAvailable
# Suggest to include a .zip'ed version of the .app dir in your SCM, eg "iOS-Simulator Cropper.app.zip"
# then use build system / rake to do the unzip
# Or, download the .zip from a trusted/reliable location, and include in build process
File.directory? "./iOS-Simulator Cropper.app"
end
# do a screenshot capture with the standard Mac screencapture command
def screenshotWithScreencapture filename
%x[screencapture #{filename}]
end
# do a screenshot capture with the cropper tool
def screenshotWithCropper filename
cmd = "./iOS-Simulator\\ Cropper.app/Contents/MacOS/iOS-Simulator\\ Cropper -f #{filename}"
value = system( cmd )
end
# add thumbnail to output/html-report
def showImageInReport imageFileName
image = "<img src=\"#{imageFileName}\" width=\"50\">"
hyperlink = "<a href=\"#{imageFileName}\">#{image}</a>"
puts hyperlink
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment