altf

Automating iOS User Interface Testing with UIAutomation

Posted in Apple, mobile, QA by AltF on November 14, 2010

Update: 25 June 2011
iOS 5 now supports recording these user interactions in UIAutomation which means no need to write these scripts manually.

Apple included Automated Testing support in iOS 4.0 which lets you automate testing of your iOS applications by scripting touch events. It allows you to write test scripts to exercise your application’s user interface elements as the application runs on a connected device or simulator. You write the tests in JavaScript to the UI Automation API, which simulates user interaction with the application as it runs and returns log information to the host computer.

In a nutshell:
– Automates UIKit based applications.
– Works on all iOS devices (iPhone, iPod Touch, iPad) and simulator.
– JavaScript automation scripts.
– Integrated in Instruments.
– Accessibility based.

Step 1 – Getting Started.
– Make sure that you have the latest iOS SDK (4.0+) installed | iOS SDK
– Familiarize yourself with Instruments | Instruments User Guide

Step 2 – Preparing your application.
Ensure accessibility is enabled and all the UI controls and views in your application are tagged with unique accessibility labels which makes it easy to access them in our test scripts. Using Interface Builder, you can set an element’s accessibility status and provide custom content for the label.

Step 3 – Writing test scripts.
In essence, your test script is an ordered set of commands, each of which accesses a user interface element in your application to perform a user action on it or to use the information associated within it. All the user interface elements in your application are represented to the script through an ordered hierarchy of objects defined by the UIAElements class and its subclasses.
To reach a specified UI element, the script simply calls down the element hierarchy, starting with the top level target object obtained by calling UIATarget.localTarget() The control hierarchy looks like:

var target = UIATarget.localTarget();
var application = UIATarget.localTarget().frontMostApp();
var window = UIATarget.localTarget().frontMostApp().mainWindow();

For example, if your application has a table view in the main window the hierarchy should looks like:
• Target application ■ Main window ■ View
window.tableViews()[0];

Similarly, within a view there are elements:
• Target application ■ Main window ■ View ■ Element
window.tableViews()[0].cells()[0];

And to access the child elements within an element:
• Target application ■ Main window ■ View ■ Element ■ Child element
window.tableViews()[0].cells()[0].elements()[“0”];

The Automation instrument maintains a complete element hierarchy that represents your application’s user interface. To view that hierarchy, use the logElementTree method to write an outline of it to the log:
application.logElementTree();

Once you are able to access the elements you can perform various actions on it. I’ll show you how to simulate basic touch events.

Tapping buttons:
Example: If you want to tap on the Add button on the top navigation bar. (Note that the navigation bar is outside the main window, hence we access it via:)
application.navigationBar().buttons()["Add"].tap();

Scrolling to an element:
Example: If your main application is an address book app, with the main window consisting of a tableview with elements in it. You can simply scroll to a particular element:
window.tableViews()[0].scrollToElementWithName("John Appleseed");

Logging
You can write a few assertion functions to verify if the expected results matches the actual results. You can log the results based on pass/fail. Simple example:


var testName = "Test Case 1";
UIALogger.logStart(testName);
UIALogger.logMessage("Assert Text - Check if element 5 is John Appleseed");
var expected = "John Appleseed";
var actual = window.tableViews()[0].cells()[5].elements()[5].name());
if (expected = actual) {
UIALogger.logPass(testName);
}
else {
UIALogger.logFail(testName);
}

Step 4 – Executing the test.
– Once you are ready with the test script. Compile your app in the debug mode and create a simulator build.
– Launch Instruments and select Automation from iOS Simulator templates.
– Select your target application from “Choose Target” option on the top.
– Select the test script from “Choose Script” option on the left window.
– Hit the red record button once done. It will fire up the simulator with your target app and start executing the tests.

Update:
– If you are facing any issues running UIAutomation on the iPad, refer UIAutomation on iPad

References:
API Documentation


Tagged with: , , , , , , ,

Preparing Devices for iPhone Development

Posted in Apple by AltF on February 20, 2010

In order to test your application on a device, you must configure your computer and your device for iPhone OS development. The following steps will show you how to get started. You can also follow these steps if you have already started and getting the code sign errors like:

  • Code Sign error: The identity ‘iPhone Developer’ doesn’t match any valid certificate/private key pair in the default keychain
  • Code Sign error: Provisioning profile ‘xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx’ can’t be found

Step 1 Generate a Certificate Signing Request (CSR) with a public key

  • In your Applications folder, open the Utilities folder and launch Keychain Access.
  • Choose Keychain Access > Certificate Assistant > Request a Certificate from a Certificate Authority.
  • In the Certificate Information window, enter or select the following information:
  • In the User Email Address field, enter your email address
  • In the Common Name field, enter your name
  • In the Request is group, select the Saved to disk option
  • Click Continue.

The Certificate Assistant saves a Certificate Signing Request (CSR) file to your Desktop.

The public/private key pair will be generated when you create the Certificate Signing Request (CSR) if you use the Key Chain Assistant to create the CSR.

Step 2 Submit the CSR through the Program Portal.

  • Click the Development tab
  • Upload the certificate by choosing the (CSR) file
  • Click Submit

Refresh the page. Click on Approve (Or get your Admin to approve if you are not the team leader).

Note: If you are getting an error like – “The Certificate selected file is invalid. Please check the file and try again.” Try using Safari browser, Certificate upload has some known issues with Chrome browser.

Step 3 Adding Your Development Certificate to Your Keychain

Your development certificate must be in your keychain so that Xcode can digitally sign your iPhone applications. To add your development certificate to your keychain, in your computer:

  • Once you approve the development certificate in step 2. You will see a download button in “action” tab. Download and save the development certificate (developer_identity.cer) on your computer.
  • Open your development certificate with the Keychain Access application by double-clicking it or dragging it to the Keychain Access application icon.
  • In the Add Certificates dialog, ensure Keychain is set to “login” and click OK.

Similarly install WWDR intermediate certificate if you haven’t already. You will see the link to download WWDR certificate (AppleWWDRCA.cer) just below the development certificate (The text reads as *If you do not have the WWDR intermediate certificate installed, click here to download now.)

To verify the successful install, you should see the following certificates in keychain:

  • Apple Worldwide Developer
  • iPhone Developer: <Developers name>
  • Public and private keys for developer

Step 4 Register Device, Add App IDs and Create Development Provisioning Profile

More details and detailed how-to steps can be found in Device, App ID and Provisioning Tabs in the Program Portal. Just need to make sure that the App ID is same as the name of the app and while creating the provisioning file correct development certificate and devices are added.

Step 5 Installing the Development Provisioning Profile

  • Download the profile developer_profile.mobileprovision
  • Open Xcode and then open Organizer from Window->Organizer
  • Drag the downloaded file into the ‘Organizer’ window within Xcode under iPhone Development. This will automatically copy the .mobileprovision file to the proper directory.

Step 6 Build & Install App on device

  • Launch Xcode and open your project. Select ‘Device – iPhone OS’ from the ‘Device | Debug’ drop down menu.
  • Select your project in Targets under the left navigation column and select the ‘Info’ icon from the top menu bar. Select the ‘Build’ Tab. Click the ‘Any iPhone OS Device’ pop-up menu below the ‘Code Signing Identity’ field and select the iPhone Development Certificate/Provisioning Profile pair you wish to sign and install your code with.
  • Your iPhone Development certificate will be in bold with the Provisioning Profile associated with it in grey above.
  • Build and Go

object-oriented programming explained by steve jobs

Posted in Apple, Tech by AltF on February 15, 2010

Jeff Goodell: Would you explain, in simple terms, exactly what object-oriented software is?

Steve Jobs: Objects are like people. They’re living, breathing things that have knowledge inside them about how to do things and have memory inside them so they can remember things. And rather than interacting with them at a very low level, you interact with them at a very high level of abstraction, like we’re doing right here.

Here’s an example: If I’m your laundry object, you can give me your dirty clothes and send me a message that says, “Can you get my clothes laundered, please.” I happen to know where the best laundry place in San Francisco is. And I speak English, and I have dollars in my pockets. So I go out and hail a taxicab and tell the driver to take me to this place in San Francisco. I go get your clothes laundered, I jump back in the cab, I get back here. I give you your clean clothes and say, “Here are your clean clothes.”

You have no idea how I did that. You have no knowledge of the laundry place. Maybe you speak French, and you can’t even hail a taxi. You can’t pay for one, you don’t have dollars in your pocket. Yet I knew how to do all of that. And you didn’t have to know any of it. All that complexity was hidden inside of me, and we were able to interact at a very high level of abstraction. That’s what objects are. They encapsulate complexity, and the interfaces to that complexity are high level.

(In 1994, Apple’s kingpin sat down for a free-ranging session with Rolling Stone reporter Jeff Goodell. Full interview)