Question

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?

Was it helpful?

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());

OTHER TIPS

I've modified your code a bit. So here it is:

function checkIfEnabled(list, button_name) {
   var btn_enabled = false;
   var list_length = list.length;
   var list_item;

   for (var list_index=0; list_index < list_length; list_index++) {
       list_item = list[list_index];
       var item_name = list_item.name();

        if (list_item.isVisible () && list_item.isEnabled () &&
            item_name.match(button_name.toString())){
            UIALogger.logMessage ("We're IN !!! ");
            btn_enabled = true;
            break;
        } else {
            btn_enabled = false;
            UIALogger.logMessage ("Still looking for a button");
        }
    }
    return btn_enabled;
}

Here is my usage of this function:

    var btn_state = checkIfEnabled(app.navigationBar().buttons(), 
                                   YOUR_BTN_NAME);

Then you can simply check whether 'btn_state' is true or false depending on your needs.

Chears

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top