How to test contents of UIAutomation textfield using Instruments

5 Comments

Here’s an example of how to test the contents of a textfield on your iPhone using test automation with Instruments.

#import "tuneup/tuneup.js"

test("Add friend", function(target, app) {
  
	target.frontMostApp().tabBar().buttons()["Friends"].tap();
	target.frontMostApp().navigationBar().buttons()["Add friend"].tap();
	target.frontMostApp().mainWindow().tableViews()["Empty list"].cells()["Jon Smith"].tap();
	target.frontMostApp().mainWindow().tableViews()["Empty list"].cells()["Jon Smith"].tap();
	target.frontMostApp().tabBar().buttons()["Email"].tap();
	
	// verify email toField has email address
	var toField = target.frontMostApp().mainWindow().scrollViews()[0].textFields()["toField"];
	assertEquals("jon@smith.com", toField.value());
	 
	 // cancel and delete draft
	target.frontMostApp().navigationBar().leftButton().tap();
	target.frontMostApp().actionSheet().buttons()["Delete Draft"].tap();
	 
});

My mode of operation so far is to record my test script by clicking through the application, and then I go back and add javascript asserts to check the contents of textfields and such.

The epiphany for me was that all the iPhone elements are exposed as HTML elements! That means if you want to test the contents of a text field of of an email, simply get the text field and check the contents as if it were HTML:

	var toField = target.frontMostApp().mainWindow().scrollViews()[0].textFields()["toField"];
	assertEquals("jon@smith.com", toField.value());

Pretty cool.

Note the assertEquals method comes via Alex Vollmer’s tuneup library.

Here’s a short intro to UIAutomation with Instruments.

Test Automation (UIAutomation) example with XCode Instruments

14 Comments

UIAutomation is the library/tool Apple added in iOS SDK 4.0 to help with test automation on the iOS platform.

Scripts are written in JavaScript and executed through the Instruments Automation Instrument.

Here a quick walkthrough of how to record a script from your iPhone and play it back on your device.

Open Instruments

You open instruments through XCode -> Open Developer Tool -> Instruments.

Add Automation Instrument

There’s lots of instruments to choose from. The one we want is the ‘Automation’ instrument.

Add a script

Choose your target

Your target isn’t your device. It’s the device and your application.

Hit the record button

Record your script by hitting the ‘record’ button at the top or bottom of the tool and then click through your app.

Play it back

To see your script hit the funny looking Trace Log/Editor Log drop down and switch to scrip log view.

You should then see your recorded script.

You can play it back by clicking the ‘play’ button beside the record button at the bottom and you should see the same script played back against your phone. Output should look like this.

Save your script

This simple script doesn’t do too much, but it’s a start and if you want to save it and play it back later you can create a directory and story it in your app like this:

Much more

There’s a lot more we can do with UIAutomation. This is just a start and we’ve only scratched the surface.

But you can do a lot with this tool, and this is merely the first step.

Note: If your tests don’t run, make sure you aren’t connected and running your device from XCode. Can’t run both simultaneously so stop the XCode one then try running your tests again.

Links that help:

http://alexvollmer.com/posts/2010/07/03/working-with-uiautomation/

UIAutomation reference