Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?

Taking creenshots without status bars with UIAutomation

The essential bits from [https://github.com/jonathanpenn/ui-screen-shooter](ui-screen shooter).

  • To install, clone this Gist into your project's root:
git clone git@gist.github.com:/962170d4b533e5c5e3d5.git Scripts
  • Update the list of simulators in screenshots.rb
  • Update the languages array, unless you need English, German and French too
  • Build your project so it ends up in DerivedData/*/Build/Products/*/*.app, or update the build path in screenshots.rb
  • Run with ./Scripts/screenshots.rb
target = UIATarget.localTarget()
window = target.frontMostApp().mainWindow();
// Wait for the app to finish loading
target.delay(1)
UIALogger.logMessage('Creating screenshot with the status bar')
target.captureScreenWithName('home');
UIALogger.logMessage('Creating screenshot without the status bar')
rectWithoutStatusBar = target.rect()
rectWithoutStatusBar.origin.y = 20.0
target.captureRectWithName(rectWithoutStatusBar, 'home_without_status_bar');
// To learn how to navigate around your app, read "Automating UI Testing" in the
// Instruments User Guide: https://developer.apple.com/library/ios/documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/UsingtheAutomationInstrument/UsingtheAutomationInstrument.html
#!/usr/bin/env ruby
require 'tmpdir'
require 'pathname'
class Screenshot
def tracetemplate
'/Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.xrplugin/Contents/Resources/Automation.tracetemplate'
end
def destination_directory
File.expand_path('screenshots')
end
def build
Dir['DerivedData/*/Build/Products/*/*.app'].first
end
def script
'Scripts/screenshots.js'
end
# Run `instruments -w help!` to get the list of available simulators.
def simulators
{
ipad: "iPad Retina (8.0 Simulator)",
iphone_4: "iPhone 4s (8.0 Simulator)",
iphone_5: "iPhone 5s (8.0 Simulator)",
iphone_6: "iPhone 6 (8.0 Simulator)",
iphone_6_plus: "iPhone 6 Plus (8.0 Simulator)"
}
end
def languages
%w(en de fr)
end
def command(command)
puts "Running `#{command}`..."
IO.popen(command).each do |line|
print line
end
end
def run
Dir.mktmpdir do |results_directory|
languages.each do |language|
simulators.each do |key, simulator|
command(
"instruments " <<
"-w \"#{simulator}\" " <<
"-t #{tracetemplate} " <<
"-D #{results_directory} " <<
"#{build} -e UIARESULTSPATH #{results_directory} -e UIASCRIPT #{script} " <<
"-AppleLanguages \"(#{language})\" " <<
"-AppleLocale \"#{language}\""
)
Dir.glob("#{results_directory}/**/*.png").each do |path|
filename = "#{key}_#{language}_#{Pathname.new(path).basename}"
File.rename(path, File.join(destination_directory, filename))
puts "Saved #{filename} to #{destination_directory}"
end
end
end
end
end
end
Screenshot.new.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment