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.