Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automate Screenshots on iPhone Simulator?

Tags:

iphone

I'm tired of taking new screenshots everytime I change my UI for my iPhone application. I would like to be able to run a script/program/whatever to load my binary on the simulator and then take a few screenshots.

The solution can be in any language... it doesn't matter to me.

Thanks!

like image 522
JP Richardson Avatar asked Sep 01 '09 04:09

JP Richardson


People also ask

How do I make my iPhone screenshot automatically?

"Right Side+Volume Buttons" or "Home+Sleep" A simple way to capture a screenshot of your iPhone or iPad is to press and hold the "Home" button and simultaneously press the "sleep/wake" button. A screenshot will be saved to your camera roll.

Where do iOS simulator screenshots go?

If you take a screenshot from within your app running on the simulator, and then save it to photos app on that simulator, then it is stored in some library folder of that specific Simulator.

Can iphones do scrolling screenshots?

Hidden within iOS is a scrolling screenshot feature that allows you to capture multiple pages with only a single screenshot. There are third-party apps you can use to stitch together individual screenshots and create a longer one, but a scrolling screenshot makes the process easier.

Does iOS simulator simulate performance?

The simulator does a really lousy job of indicating app performance. In fact it doesn't try. For most things the simulator is much, much faster than an iOS device. It runs your code on an x86 processor, which is much faster than the ARM and has many times more memory.


2 Answers

With iPhone SDK 4 you can automate GUI tests, and it can take screenshots for you.

Basically, you write a Javascript script, and then Instruments (using Automation template) can run it on the device to test the UI, and can Log data, screenshot, etc., and also can alert if something's broken.

I couldn't find a reference guide for it, but in the SDK reference library search for UIA* classes (like UIAElement).

There's also a video demoing this from WWDC, session 306.

like image 101
mohsenr Avatar answered Sep 24 '22 05:09

mohsenr


I have the same wish. I want to be able to save screenshots from a few screens in my app without all the manual work. I'm not there yet, but I have started.

The idea is to tail /var/log/system.log, where the output from the NSLog statements go. I pipe the output to a python program. The python program reads all the lines from stdin and when the line matches a specific pattern, it calls screencapture.

NSLog(@"screenshot mainmenu.png"); 

That will cause a screenshot named "XX. mainmenu YY.png" to be created every time it is called. XX is the number of the screenshot since the program started. YY is the number of the "mainmenu" screenshot.

I have even added some unnecessary features:

NSLog(@"screenshot -once mainmenu.png"); 

This will only save "XX. mainmenu.png" once.

NSLog(@"screenshot -T 4 mainmenu.png"); 

This will make the screenshot after a delay of 4 seconds.

After running an app with the right logging, screenshots with the following names could have been created:

00. SplashScreen.png 01. MainMenu 01.png 03. StartLevel 01.png 04. GameOver 01.png 05. MainMenu 02.png 

Give it a try:

  1. Add some NSLog statements to your code

  2. $ tail -f -n0 /var/log/system.log | ./grab.py

  3. Start your iPhone app in the Simulator

  4. Play around with your app

  5. Have a look at the screenshots showing up where you started the grab.py program

grab.py:

#!/usr/bin/python  import re import os from collections import defaultdict  def screenshot(filename, select_window=False, delay_s=0):     flags = []     if select_window:         flags.append('-w')     if delay_s:         flags.append('-T %d' % delay_s)     command_line = 'screencapture %s "%s"' % (' '.join(flags), filename)     #print command_line     os.system(command_line)  def handle_line(line, count=defaultdict(int)):     params = parse_line(line)     if params:         filebase, fileextension, once, delay_s = params         if once and count[filebase] == 1:             print 'Skipping taking %s screenshot, already done once' % filebase         else:             count[filebase] += 1             number = count[filebase]             count[None] += 1             global_count = count[None]             file_count_string = (' %02d' % number) if not once else ''              filename = '%02d. %s%s.%s' % (global_count, filebase, file_count_string, fileextension)             print 'Taking screenshot: %s%s' % (filename, '' if delay_s == 0 else (' in %d seconds' % delay_s))             screenshot(filename, select_window=False, delay_s=delay_s)  def parse_line(line):     expression = r'.*screenshot\s*(?P<once>-once)?\s*(-delay\s*(?P<delay_s>\d+))?\s*(?P<filebase>\w+)?.?(?P<fileextension>\w+)?'     m = re.match(expression, line)     if m:         params = m.groupdict()         #print params         filebase = params['filebase'] or 'screenshot'         fileextension = params['fileextension'] or 'png'         once = params['once'] is not None         delay_s = int(params['delay_s'] or 0)         return filebase, fileextension, once, delay_s     else:         #print 'Ignore: %s' % line         return None  def main():     try:         while True:             handle_line(raw_input())     except (EOFError, KeyboardInterrupt):         pass  if __name__ == '__main__':     main() 

Issues with this version:

If you want to take a screenshot of only the iPhone Simulator window, you have to click the iPhone Simulator window for every screenshot. screencapture refuses to capture single windows unless you are willing to interact with it, a strange design decision for a command line tool.

Update: Now iPhone simulator cropper (at http://www.curioustimes.de/iphonesimulatorcropper/index.html) works from command line. So instead of using the built in screencapture, download and use it instead. So now the process is completely automatic.

like image 36
Offe Avatar answered Sep 23 '22 05:09

Offe