back

iconwrite Automate taking screenshots thanks to UIAutomation

August 10, 2011, 21:21

Taking screenshots is painful

Either by choice or simply because I failed to succeed there I decided to focus on countries other than the biggest market: the U.S.A. As a result, while never reaching the top #50 in U.S.A. some of my apps have ranked #1 in other countries including amongst others the relativiely big markets of France, UK, Germany, Spain, Japan and China. This has been a lucky move considering some ad networks (notably Admob and MobFox) tend to have higher eCPM for my apps in Europe.

To achieve this localization was an absolute requirement.

Once you use a streamlined system (such as iCanLocalize ) managing various languages isn't too hard, as long as your apps – like mine – do not hold too much text. If I need a new string, I'll just add it to my en.lproj strings file, upload it to iCanLocalize and pay only for the missing words, then in a few hours (or days) I'll download the resulting strings files for all languages.

Now the thing I hate the most with an app that has 10 languages is that if you change something visible you need to update all the screenshots. Shudder.

This includes two very painful steps:

  • A: change the language in your phone 10 times and pick every screenshot again with the Organizer and then give it a proper name by hand. You could also change the language 10 times in your simulator (slightly faster) but then you'd have to crop your shots and have to worry about gamma issues or any possible difference between simulator and device.

  • B: Upload each screenshot manually while insulting loudly the guys who designed iTunesConnect. A zip file with standardized names would be SO much faster... one can dream.

I am afraid I can't do much about B, so let's keep cursing about this. However, there's a solution to improve the speed for taking screenshots on device, making more cursing optional but still enjoyable.

Enter UIAutomation. Writing a simple script

As the name implies, UIAutomation let's you automate your apps. This is primarily aimed a running Unit Tests but who needs them, we have user reviews for that, right?

To use UIAutomation all you need to do is write a javascript file and run it together with your app in the Profiler.

Here's a simple example script, showcasing some of the stuff you can do. Here we'll take two screenshots on two different screens.



// Define the language for the current run (here, Japanese)
var forcelanguage  ='ja';

// Setup some variables for readability
var target  = UIATarget.localTarget();
var app     = target.frontMostApp();
var win      = app.mainWindow();

// Overwrite the language in NSUserDefaults
// Note that this will only take effect on next run.
// So you have to run this twice.
// Once to set the language, another to actually pick the screenshot.
app.setPreferencesValueForKey([forcelanguage], 'AppleLanguages');

// You can define your own settings of course, for example:
app.setPreferencesValueForKey("0.85", 'fakePercentageForShots');

// Store current language name for use in filenames
lang = target.frontMostApp().preferencesValueForKey("AppleLanguages")[0];

// Save screenshot for home screen
target.captureScreenWithName( lang+"_home");

// Pause for two seconds (screen capture takes a second !)
target.delay(2);

// Open settings screen by pressing a button
// (note that the button must have the xcode label 'settings' for this to work)
// you could also reference the button by its number (here: 0)
 win.buttons()['settings'].tap();

// Pause for two seconds (new screen needs some time to load and appear)
target.delay(2);

// Save screenshot for settings screen
target.captureScreenWithName( lang+"_settings");

// Pause for two seconds (screen capture takes a second !)
target.delay(2);

// Hit the back button to go back to home screen
// otherwise upon next run the app will still be on settings screen
win.toolbar().buttons()[0].tap();

Here's the downloadable autoshot.js with the code above. It has a bit more logging than the code above too.

Launching the script

Open your project In XCode, use the menu : Products -> Profile. The following screen will show up and you'll very wisely pick Automation, unless you have been speed-reading all along and you think this is about zombies.

Automation in Profiler

You can abort the first run immediately since you have to change the settings:

  • Select your .js script file and enable Run on Record.
  • Enable Continous logging at the bottom and point it to an empty folder. I named mine 'shots' and put it on my desktop.

Automation settings

Now press the Run button again and watch as your screenshots are saved:

execution log

Stuff to Remember:

  • taking screenshots just doesn't work from the simulator, so you have to run this on the real device.

  • the screenshots are saved in the format that you have defined as the default for XCode (I usually pick PNG)

  • this runs in a full blown javascript interpreter. You can create functions or whatever. I made this example all in one linear script so it could be easy to read and understand.

  • Overwriting the language in NSUserDefaults only affects the next run so for each session you have to define the language variable, run the script once and abort it (so it memorizes the language), then run it again to get the actual screenshots.

  • Don't forget that after doing this your app will NOT use the device's language anymore. I'd recommend deleting the app afterwards otherwise you might be puzzled next time you run it a week later and you totally forgot about this :-)

  • You can find how to reach each element in your app by calling this method which will dump the entire UI structure (at this particular moment)


UIATarget.localTarget().frontMostApp().logElementTree()

Looping over all languages ? Not yet.

As you might have noticed, the sad part here is you still have to run the script once in every language. I am still looking for a solution to update the language without having stop and restart the application but so far I haven't found a way. This would maket the full process 100% automated. Any suggestions ?

Update: Taking screenshots with the simulator

XCode 4.5+ fixed the issue where scripts wouldn't be able to take shots on the simulator. However, an issue remains: when taking shots from the simulator the language forced from the script isn't taken into account.

If you need to take shots in the simulator, as happened to me with the iPhone 5 because I could own one, you can just add this in your main.m:


  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];


#ifdef DEBUG
    NSString* forceLanguage = @"fr";
    [[NSUserDefaults standardUserDefaults] setObject: [NSArray arrayWithObjects:forceLanguage, nil] forKey:@"AppleLanguages"];
#endif


int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool release];
    return retVal;


Obviously the #ifdef DEBUG is a good security measure to make sure this never ends up in the final release if you forget to comment it.

38 responses to this post

Hi Santiago, Can I use setPreferencesValueForKey to define a value that the application read using NSUserDefaults? I haven't tried but it seemed that you cannot do that according to this: http://stackoverflow.com/questions/4977673/reading-preferences-set-by-uiautomations-uiaapplication-setpreferencesvaluefork But you're saying here that you should be able to do just that, I was wondering if you were able to try this?

December 05, 2011, 09:21 | Mohamed Zoweil

Yes, You can set a value in the nsuserdefaults. But when this value is a system value such as the language, you still have to manually relaunch the app for them to take effect.

February 19, 2012, 14:35 | Santiago Lema

Hello, I tried the steps to change the language for UIAutomation. It changes the language, eg: I set the language as french and when I print "app.preferencesValueForKey('AppleLanguages')", I get french back as the language but if I run the test with it the text on the simulator remains English i.e. I cannot see the language change on the simulator. Did you face any similar issues?

July 26, 2013, 01:47 | Swetha

Hello, I tried the steps to change the language for UIAutomation. It changes the language, eg: I set the language as french and when I print "app.preferencesValueForKey('AppleLanguages')", I get french back as the language but if I run the test with it the text on the simulator remains English i.e. I cannot see the language change on the simulator. Did you face any similar issues?

July 26, 2013, 01:58 | Swetha

Hello, I tried the steps to change the language for UIAutomation. It changes the language, eg: I set the language as french and when I print "app.preferencesValueForKey('AppleLanguages')", I get french back as the language but if I run the test with it the text on the simulator remains English i.e. I cannot see the language change on the simulator. Did you face any similar issues?

July 26, 2013, 01:58 | Swetha

About step B you can take a look at iTunes transporter. http://stackoverflow.com/questions/13191678/where-to-find-apples-new-command-line-delivery-tool-transporter With some additional script to create packages you can upload screenshots/descriptions/whats new automatically.

December 09, 2013, 16:10 | OlKir

I have the same issue with "Swetha" "Hello, I tried the steps to change the language for UIAutomation. It changes the language, eg: I set the language as french and when I print "app.preferencesValueForKey('AppleLanguages')", I get french back as the language but if I run the test with it the text on the simulator remains English i.e. I cannot see the language change on the simulator. Did you face any similar issues? " I don't know how to fix the issue. Help, please. Thanks

March 28, 2014, 17:47 | Linh

I have the same issue with "Swetha" "Hello, I tried the steps to change the language for UIAutomation. It changes the language, eg: I set the language as french and when I print "app.preferencesValueForKey('AppleLanguages')", I get french back as the language but if I run the test with it the text on the simulator remains English i.e. I cannot see the language change on the simulator. Did you face any similar issues? " I don't know how to fix the issue. Help, please. Thanks

March 28, 2014, 18:15 | Linh

710552369

May 23, 2014, 17:46 | sirichai conlareen

Check out this new tool: https://github.com/KrauseFx/snapshot it does screen shots with a single command line command!

March 11, 2015, 15:56 | Simon

If anyone is looking for Xcode 7 / UI Tests solution to automate screenshot capture, here's what I come up with: https://github.com/zmeyc/UITestUtils

July 12, 2015, 19:19 | zmeyc

I want 4 screen short for published his apps Please help me

December 28, 2016, 06:40 | dinesh rai

Recommending this add free app monetization platform - https://globalhop.net

May 08, 2019, 10:29 | normahamilton

Recommending this add free app monetization platform - https://globalhop.net

May 08, 2019, 10:29 | normahamilton

I was wondering if you wanted to submit your new site to our free business directory? https://bit.ly/submit_site_2

December 25, 2022, 04:50 | Ingeborg Barbour

Give your new site a boost, submit your site now to our free directory and start getting more clients bit.ly/submit_site_1

January 11, 2023, 05:44 | Jodie Hurtado

Free submission of your new website to over 1000 business directories here bit.ly/submit_site_t9qPdO4E2oF2

January 12, 2023, 18:34 | Meagan Justus

Free submission of your new website to over 1000 business directories here https://bit.ly/submit_site_9jxc1c6t8mpn

January 18, 2023, 11:51 | Brodie Macias

Take advantage of our free directory submission and give your new site a boost. https://bit.ly/g5tig5

February 02, 2023, 15:50 | Cameron Alger

Get your new site noticed by submitting it to our free directory. http://bit.ly/3Yjum2a

February 04, 2023, 14:58 | Ethan Whetsel

Submit your site to 1000 business directories with one click here-> http://bit.ly/3X2uwK3

February 24, 2023, 16:23 | Candra Shinn

Take control of your advertising budget, our guaranteed results ensure success! Visit: https://9cz6.short.gy/guaranteed_results

May 31, 2023, 20:43 | Edwin Funkhouser

Do you do contact form blasting to get sales? If you do I can provide you with lists of millions of verified contact forms. If you don't I can show you how I did it on your website contact form just now and it's easy for you to do it too. For details add me on Skype and let's chat, my ID is: live:.cid.7aad4787a72a11d0

June 15, 2023, 04:36 | Daisy Bolivar

I'm messaging you via your contact form on your website at smallte.ch. So by reading this message you just proved that contact form advertising works! Want to blast your ad to millions of contact forms? Or maybe you prefer a more targeted approach and only want to blast your ad out to websites in certain business verticals? We charge $99 to blast your ad to 1 million contact forms. Volume discounts are available. I have over 35 million contact forms. Let's discuss, contact me via Skype here: live:.cid.7aad4787a72a11d0

June 20, 2023, 19:00 | Kasey Carver

Hi, I'm sending you this message via your contact form on your website at smallte.ch. By reading this message you're living proof that contact form advertising works! Do you want to blast your ad to millions of contact forms? Maybe you prefer a more targeted approach and only want to blast our ad out to websites in certain business categories? Pay just $99 to blast your ad to 1 million contact forms. Volume discounts available. I have more than 35 million contact forms. Let's get the conversation started, contact me via Skype here: live:.cid.b4d79bd7b12c4757

June 28, 2023, 19:17 | Augustina Paredes

Is this your website? smallte.ch? I just sent you a message via the contact form on your site and was wondering if you wanted to try some unique advertising that reaches business owners worldwide? How do we do it? Well you just witnessed our process. We send your ad text to contact forms on websites worldwide. Plans start at a hundred dollars for posting your ad to one million sites.

July 08, 2023, 18:41 | Hiram Wetherspoon

Quick question to ask you... Are you aware that by reading this message you just proved that contact form marketing works? That's right, and we can get eyeballs on your offer too! Pricing starts at just $100 to blast YOUR ad message to 1 MILLION contact forms on websites just like yours worldwide. Contact me on Skype and let's discuss what will work for your product/service. My Skype ID: live:.cid.83c9da999a4f9f this message was sent to your website contact form at: smallte.ch

July 19, 2023, 01:31 | Sheldon MacCullagh

Quick question to ask you... Are you aware that by reading this message you just proved that contact form marketing works? That's right, and we can get eyeballs on your offer too! Pricing starts at just $100 to blast YOUR ad message to 1 MILLION contact forms on websites just like yours worldwide. Contact me on Skype and let's discuss what will work for your product/service. My Skype ID: live:.cid.83c9da999a4f9f this message was sent to your website contact form at: smallte.ch

July 27, 2023, 18:12 | Fanny Byron

Is this your website? smallte.ch? I just sent you a message via the contact form on your site and was wondering if you wanted to try some unique advertising that reaches business owners worldwide? How do we do it? Well you just witnessed our process. We send your ad text to contact forms on websites worldwide. Plans start at a hundred dollars for posting your ad to one million sites.

August 03, 2023, 09:35 | Darlene Lemon

I have a question. You just read this message right? That means you're now a potential customer and I can do the same thing for your business. I can blast YOUR ad to 1 million websites just like I did to yours for just $98. More pricing plans are also available, contact me on Skype for details. Here's my id : live:.cid.83c9da999a4f9f

August 11, 2023, 18:09 | Omer Brink

I just left you this message on your website contact form at smallte.ch and I have also sent it to millions of other sites. I get new customers every day using this method and so can you! For info and pricing, just reach out to me via Skype here: live:.cid.aebc78a94c13344c

September 08, 2023, 00:28 | Dian Thiele

I have a question. You just read this message right? That means you're now a potential customer and I can do the same thing for your business. I can blast YOUR ad to 1 million websites just like I did to yours for just $98. More pricing plans are also available, contact me via email for details: Terese@gomail2.xyz

September 14, 2023, 22:42 | Terese Fenner

Want to start getting 10X more customers today? Email me and I'll show you how it's done: contact@leadsblue.com or visit: https://leadsblue.net/

September 15, 2023, 01:52 | Margarette Cagle

Accidental overdose is the #1 cause of death for people aged 18-45 in the United States. Narcan is a nasal spray that can reverse an opioid overdose, saving the person’s life. Our group of volunteers has compiled a website that tracks organizations that are giving out free and discount Narcan, this site is https://www.narcan-finder.com/. Eventually this life saving medication will be as easy to find as a fire extinguisher, but at the moment, it’s only gradually becoming available, and often expensive at drugstores, etc. Would you consider sharing our resource on your website? It will save lives. If you have any questions or want to help spread the word, contact us at narcanfinder@gmail.com. Thank you for your consideration!

September 17, 2023, 03:29 | Donette Bitner

I'm posting this message to your website at smallte.ch to show you how effective contact form blasting is. The same way I sent you this message, I can also send your ad message to websites worldwide. You can reach a million sites for less than a hundred dollars. For info Skype me here: live:.cid.dd8a3501619891fe

September 22, 2023, 19:09 | Beulah Clary

Hello there, We're thrilled to share our totally free e-book: "The Ultimate ChatGPT-4 Training Guide"! It's loaded with AI insights for impactful discussions. Bonus: Download the book now and get ChatGPT prompts for 4 weeks, curated by our experts. Get it here: https://chatgptpromptpacks.net/free-chatgpt-4-ebook/ For those all set to raise their game, explore our unique ChatGPT prompt packs, handcrafted for specific niches, and take a step towards success. Explore now: https://chatgptpromptpacks.net/prompt-packs Kind regards, Gretta ChatGPT Prompt Packs Unsubscribe: https://contactwebsites.net/chatgptpromptpacks

September 24, 2023, 18:58 | Gretta Deal

TimeSuite is the most advanced Construction and Project ERP ever produced. Advanced because it’s simplistic, comprehensive and dynamic. The system conforms to your needs. Advanced because it’s a single system (no modules). Web, desktop and mobile interfaces to a single database. Project Management, Accounting, Estimating, Scheduling, CRM, Task Management, Resource Management, On-Screen Take Off, Form Creation, Property Management, RFQs/Bid Packages and more. Fully automated percentage of completion accounting with a full job schedule that ties to the income statement. TimeSuite is new generation. Learn more at TimeSuite.com.

September 28, 2023, 00:02 | Warren Prindle

I have a question. You just read this message right? That means you're now a potential customer and I can do the same thing for your business. I can blast YOUR ad to 1 million websites just like I did to yours for just $98. More pricing plans are also available, contact me via Skype for details: live:.cid.303294bd15a81bc7

September 30, 2023, 18:02 | Shellie Darosa

Name
E-mail (will not be published)
Comment
rss Blog RSS Feed

rss Comments RSS Feed