As we said in a previous post, executing instruments using the command line is a key feature of a continuous environment with automated tests.
The new release of Xcode 6 has revealed some differences in automation using instruments that we think that it is important to point them out. We are also going to describe how we can launch automated tests on different devices using instruments and AppleScript.
The first difference is the path to Automation. The former path in Xcode 5 was:
/Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.bundle/Contents/Resources/Automation.tracetemplate
And now in Xcode 6 path has changed a little bit:
/Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.xrplugin/Contents/Resources/Automation.tracetemplate
Other difference is that -w parameter is now mandatory. You have to include the UDID of device where the test is going to be run. You can find out the UDIDs of your devices by running:
instruments -s
So, in order to run a test using instruments in the command line you have to execute:
instruments -t whereAutomationIs -w UDID pathToThe.app -e UIASCRIPT pathJavascriptFile.js -e UIARESULTSPATH pathToSaveTheResults
For instance:
instruments -t "/Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.xrplugin/Contents/Resources/Automation.tracetemplate" -w “xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx” "/Users/xxx/automationTest/myApp.app" -e UIASCRIPT “/Users/xxx/automationTest/firstTestSuite.js” -e UIARESULTSPATH “/Users/xxx/automationTest/output/”
Xcode 6 also includes simctl
, which provides a new way of working with the iOS Simulator. You can see all its options running:
xcrun simctl
We will remark the option to delete content and settings from a device:
xcrun simctl erase xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx
In this way you can run your tests using a clean environment.
Launching the automated tests on all the available devices is another key feature. We can rely on AppleScript to launch the desired device:
osascript -e 'tell application "iOS Simulator" to activate' -e 'tell application "System Events"' -e 'click menu item "iPhone 6" of menu "iOS 8.1" of menu item "iOS 8.1" of menu "Device" of menu item "Device" of menu "Hardware" of menu bar item "Hardware" of menu bar 1 of process "iOS Simulator"' -e 'end tell'
This can also be done by:
open -a "iOS Simulator" --args -CurrentDeviceUDID xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx
Once the iOS Simulator is opened, we could launch the test using instruments, and we could close the iOS Simulator after test finishes by:
osascript -e 'tell app "iOS Simulator" to quit'
Finally, we could delete content and settings from the device using xcrun simctl erase
. Once the environment is cleaned we could launch the test on the following device.