Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to clear/refresh the accessibility hierarchy cache

I have a UI test that checks the value of static text element, waits a few seconds and checks again to confirm a change. At first it wasn't working because the hierarchy was not updating. I noticed this in the log;

Use cached accessibility hierarchy for

I've put in a workaround for this by simply adding a tap to a menu and opening/closing it so that an event is synthesized and the hierarchy is updated.

It would be better, however, if there was a way to clear the cache directly or force and update. I haven't found one in the API. Am I missing something?

Any ideas?

this is what I am doing;

XCTAssertEqual(app.staticTexts["myText"].label, "Expected 1")
sleep(20)
menu.tap()
sleep(1)
menu.tap()
XCTAssertEqual(app.staticTexts["myText"].label, "Expected 2")

What I'd like to be able to do it

XCTAssertEqual(app.staticTexts["myText"].label, "Expected 1")
sleep(20)
app.elements.refresh()
XCTAssertEqual(app.staticTexts["myText"].label, "Expected 2")
like image 430
Robert Schmid Avatar asked Dec 16 '15 19:12

Robert Schmid


2 Answers

The following works for me in Xcode 10.2 (10E125):

import XCTest

extension XCUIApplication {
    // WORKAROUND:
    // Force XCTest to update its accessibility cache. When accessibility data
    // like NSObject.accessibility{Label|Identifier} changes, it takes a while
    // for XCTest to catch up. Calling this method causes XCTest to update its
    // accessibility cache immediately.
    func updateAccessibilityCache() {
        _ = try? snapshot()
    }
}
like image 156
Rudolf Adamkovič Avatar answered Oct 23 '22 10:10

Rudolf Adamkovič


In order to force an update of the accessibility hierarchy, request the count property for any XCUIElementQuery:

// refresh
_ = XCUIApplication().navigationBars.count

// examine
print(XCUIApplication().debugDescription)

The above results in: "Get number of matches for: Descendants matching type NavigationBar" and "Snapshot accessibility hierarchy for com.myapp".

like image 31
Gary Avatar answered Oct 23 '22 09:10

Gary