UI Automation, Xcode 6 and Running Simulator from Command Line

Upon upgrading from Xcode 5.1.1 to Xcode 6.x, UI Automations scripts from the command line, targeting the simulator, will fail. After a great deal of time troubleshooting, seems Apple has changed what was previously an optional parameter to a required value. Let me explain…

Here is the command line I used with Xcode 5.1.1 to invoke Instruments:

instruments -t /Applications/Xcode.app/Contents/Applications/
Instruments.app/Contents/PlugIns/AutomationInstrument.bundle/
Contents/Resources/Automation.tracetemplate 
"/Users/JOHN/Library/Application Support/iPhone Simulator/7.1/
Applications/B9812A59-EBE4-454E-B910-B2969B4D3702/TestApp.app" 
-e UIASCRIPT ./Tests.js

Copy/paste version:

instruments -t /Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.bundle/Contents/Resources/Automation.tracetemplate "/Users/JOHN/Library/Application Support/iPhone Simulator/7.1/Applications/B9812A59-EBE4-454E-B910-B2969B4D3702/TestApp.app" -e UIASCRIPT ./Tests.js

As referenced on a previous post, Locating Xcode 6 Simulator Documents and Apps, the location of Xcode 6 simulator files has changed. Here is the path to the same app, updated for Xcode 6.

instruments -v -t /Applications/Xcode.app/Contents/Applications/
Instruments.app/Contents/PlugIns/AutomationInstrument.xrplugin/
Contents/Resources/Automation.tracetemplate 
"/Users/JOHN/Library/Developer/CoreSimulator/Devices/
D634BAC6-A15B-468E-B9AE-6467DB260621/data/Containers/Bundle/
Application/16912244-3D8A-4CF3-B121-29554FFAC1DF/TestApp.app" 
-e UIASCRIPT ./Tests.js

Copy/paste version:

instruments -v -t /Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.xrplugin/Contents/Resources/Automation.tracetemplate "/Users/JOHN/Library/Developer/CoreSimulator/Devices/D634BAC6-A15B-468E-B9AE-6467DB260621/data/Containers/Bundle/Application/16912244-3D8A-4CF3-B121-29554FFAC1DF/TestApp.app" -e UIASCRIPT ./Tests.js

Problem is, when you run this, you will receive the error:

“‘Automation – TestApp’ rejected command ‘Instrument Command …”

The trick to get this to work is include an additional parameter, -w which specifies a hardware device or simulator name. One reason this took so long to troubleshoot is that the Apple documentation states:

“Omit the device identifier option (-w deviceID in this example) to target the Simulator instead of a device.”

This was true with Xcode 5.x however, the parameter is no longer optional with Xcode 6.x

Here’s how to pull all this together.

You can get a list of all the known devices as follows:

$ instruments -s devices
Known Devices:
Resizable iPad (8.0 Simulator) [269D8D20-26D8-43B0-8A11-9A65479100AA]
Resizable iPhone (8.0 Simulator) [F52A03AF-0A78-457A-915A-A7303B54D73E]
iPad 2 (8.0 Simulator) [43D72D60-5DEF-4A91-95B2-4FC2D7329DCD]
iPad Air (8.0 Simulator) [657FCEB4-CAFE-4245-95E8-52C0967E4083]
iPad Retina (8.0 Simulator) [86CAA6D3-4528-44E3-ADA3-B261CA89A3A5]
iPhone 4s (8.0 Simulator) [6480BEE1-223F-4CA3-B5B3-AF1FE8C80079]
iPhone 5 (8.0 Simulator) [9A586EF0-D8B6-450A-A39D-E9802BE42333]
iPhone 5s (8.0 Simulator) [500E9E9B-06F2-4988-AD83-356766A06A97]
iPhone 6 (8.0 Simulator) [D634BAC6-A15B-468E-B9AE-6467DB260621]
iPhone 6 Plus (8.0 Simulator) [C2EE22D0-9233-4FD1-88A3-5DDC00081D50]

The -w parameter needs to match the device type as specified when the build was created. Notice in the command line for Xcode 6 above I specified /CoreSimulator/Devices/D634BAC6-A15B-468E-B9AE-6467DB260621…, which is iPhone 6 (8.0 Simulator).

D634BAC6-A15B-468E-B9AE-6467DB260621 is the device type we need to pass along on the command line to Instruments:

instruments -v -w D634BAC6-A15B-468E-B9AE-6467DB260621 -t 
/Applications/Xcode.app/Contents/Applications/
Instruments.app/Contents/PlugIns/AutomationInstrument.xrplugin/
Contents/Resources/Automation.tracetemplate 
"/Users/JOHN/Library/Developer/CoreSimulator/Devices/
D634BAC6-A15B-468E-B9AE-6467DB260621/data/Containers/Bundle/
Application/16912244-3D8A-4CF3-B121-29554FFAC1DF/TestApp.app" 
-e UIASCRIPT ./Tests.js

Cut/paste version:

instruments -v -w D634BAC6-A15B-468E-B9AE-6467DB260621 -t /Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.xrplugin/Contents/Resources/Automation.tracetemplate "/Users/JOHN/Library/Developer/CoreSimulator/Devices/D634BAC6-A15B-468E-B9AE-6467DB260621/data/Containers/Bundle/Application/16912244-3D8A-4CF3-B121-29554FFAC1DF/TestApp.app" -e UIASCRIPT ./Tests.js