Search code examples
iphoneiosipadios-ui-automationui-testing

How do I determine which buttons are enabled in iOS UI Automation?


Using the UI Automation instrument, I know how to test if a particular button is enabled in my iOS application:

if( btn[0].isEnabled() ) {
    UIALogger.logPass("button enabled");  
} else  {
    UIALogger.logFail("button not enabled");  
}

However, I'd like to be able to determine the number of buttons that have been enabled in the interface, not just whether a specific one is enabled. How could I determine the number of enabled buttons?

Also, how do I print details of these buttons to the console?


Solution

  • Here's a function which takes a UIAElementArray (ie app.mainWindow().buttons()) and logs the number of enabled buttons:

    function printEnabledButtons(list) {
        var enabledButtons = 0;
        for (var i=0;i<list.length;i++) {
            if (list[i].isEnabled()) {
                //UIALogger.logDebug("button " + list[i].name() + " is enabled");
                enabledButtons++;
            } else {
                //UIALogger.logDebug("button " + list[i].name() + " is not enabled");
            }
        }
        UIALogger.logDebug("number of enabled buttons: " + enabledButtons);
    }
    

    Example calling code:

    printEnabledButtons(app.mainWindow().buttons());