iOS Automation Script

This guide will explain how to write an iOS automation script to perform automated testing.

Default user interface automation in Lab uses a heuristic-based approach to detect common UI elements, e.g., login, password, registration forms. It may be necessary to write a custom UI automation script to achieve better coverage of your iOS application.

Occasionally the target iOS application has a complex or unique workflow that requires more sophisticated automation control. Default UI automation in Lab uses a heuristic-based approach to detect common UI elements, e.g., login, password, registration forms. It may be necessary to write a custom UI automation script to achieve better coverage of your application.

Here’s a boiler-plate interaction script to begin with:

JavaScript
var target = UIATarget.localTarget();

// [1]
var alert = target.frontMostApp().alert();

// [2]
if (!(alert instanceof UIAElementNil)) {
  target.captureScreenWithName('prompt');
  onAlertHandler(alert);
  target.delay(2)
}

// [3] alert handler -- accept push notification alert
function onAlertHandler (alert) {
  UIALogger.logDebug('on alert handler');
  target.captureScreenWithName('prompt'); // take a screenshot called 'prompt'

  var buttons = alert.buttons().toArray();
  // [4] accept push notifications
  if (buttons.length === 2 &&
    (buttons[0].label() === 'Don’t Allow' ||
      buttons[0].label() === 'Settings')) {
        buttons[1].tap();
        return true;
      } else {
        return false;
      }
}

// allow the app to settle down/load before we expect login screen
target.delay(10); // [5] delay 10 seconds

// [6] make sure we're on the anticipated screen for logging in
if (target.frontMostApp().mainWindow().scrollViews()[1].webViews()[0].textFields()[0].checkIsValid()) {
  // [7] sign-in
  target.captureScreenWithName('startlogin');
  target.frontMostApp().mainWindow().scrollViews()[1].webViews()[0].textFields()[0].tap();
  target.delay(2);
  target.frontMostApp().keyboard().typeString("ENTER_YOUR_EMAIL_OR_USERNAME_HERE");  // [8]
  target.captureScreenWithName('username');
  target.delay(2);
  target.frontMostApp().mainWindow().scrollViews()[1].webViews()[0].secureTextFields()[0].tap();
  target.delay(2);
  target.frontMostApp().keyboard().typeString("ENTER_YOUR_PASSWORD_HERE");
  target.captureScreenWithName('password');
  target.delay(2);
  target.frontMostApp().mainWindow().scrollViews()[1].webViews()[0].buttons()["Sign In"].tap();
  target.captureScreenWithName('finish');
}
else {
  // for some reason login isn't on the screen
  // [9]
  target.captureScreenWithName('nologin');
  UIALogger.logDebug('did not see login stuff');
}
  1. Get a handle to alerts to add our own alert handler

  2. Call our default alert handler, take a screenshot anytime we see an alert dialog

  3. Define the default alert handler

  4. Accept push notifications by hitting “Allow” for this particular app

  5. Delay 10 seconds to allow the app to finish loading and for the login screen to appear

  6. We wrap the rest of our script to ensure the login UI elements are on the screen, if we don’t UI

  7. automation will error because the elements are not present

  8. We capture a screenshot – called ‘startlogin’ before starting the login process

  9. Have the automator type the login into the login box for us

  10. Capture a screenshot in the case the login screen did not pop up