Search code examples
objective-carraysios6ios-ui-automation

Search a text/element in UIAElementArray


I am dealing with UIAElementArray for the ios automation and looking for an easier way to search inside the UIAElementArray.

Below is the code:

var textArray = this.myCollectionViews.staticTexts();

textArray.toArray() // Trying to convert UIAElementArray to a javascript array.

logger.logDebug('Array object is' + textArray); // Here the output is [object UIAElementArray]; which I guess should be Array and not UIAElementArray.

if (textArray.indexOf('Hello') > -1) 
{
       // do this
}

Here I am getting a crash on textArray.indexOf :

TypeError: '[object UIAElementNil]' is not a function (evaluating 'textArray.indexOf('Hello')')

Has anybody tried converting UIAElementArray to javascript array using toArray() method? Or any ideas to make an easier search inside UIAElementArray?

Thanks!!


Solution

  • You can search using predicates. I'm not quite sure what you're trying to accomplish, but it looks like you want to do something if a text label exists. I would implement it like so:

    if (textArray.withPredicate('name contains "Hello"').length > 0)
    {
        // do this
    }
    

    You can build predicates that work on any property of a UIAElement, we're doing name() in this case.

    Here's Apple's docs on the basics of the predicate string format.