Kyle
Kyle

Reputation: 17687

Cache XCUIElement (improve performance for multiple calls to single element)

I have some UI testing which involves text manipulation in a NSTextView. I'm current setting the XCUIElement via:

let sqlTextViewTextView = sqlWindow.textViews["SQL text view"]

This works fine, but in one of the tests, a bunch of text is entered, then deleted one character at a time.

sqlTextViewTextView.typeText(substring)

// Delete backwards until we are clear
while(true)
{
    let targetString:String = sqlTextViewTextView.value as! String
    if(0 == targetString.characters.count)
    {
        break;
    } // End of we have no targetString

    sqlTextViewTextView.typeKey(XCUIKeyboardKeyDelete, modifierFlags:.None)
} // End of we have text entered

This ends up being extremely slow, as every call to sqlTextViewTextView evaluates the query to find the element.

Is there a way I can "cache" the element to skip querying it during this tight loop?

Testing code is (probably poorly written) Swift 2. I did not tag the question as such, because its not a swift specific question.

Upvotes: 0

Views: 349

Answers (1)

Aaron Sofaer
Aaron Sofaer

Reputation: 726

The query through the hierarchy should be pretty close to instantaneous. It's much more likely that the runtime of your test is being affected by the UI Testing app's logic of waiting until the target app idles every time it takes an action (in this case, every time you hit the XCUIKeyboardKeyDelete).

You can isolate this by timing the elements of the loop; I would be astonished if the query itself takes substantial time, but if it does, could you tell us what the view hierarchy of your app looks like? If you have an enormous number of view elements, it may speed up the query to use more specific set of selectors rather than trawling the full hierarchy.

Upvotes: 1

Related Questions