-[XCUIElement tap] works intermittently - Hangs at "Wait for app to idle"

I'm having difficulty getting `XCUIElementEventSynthesis` methods to consistently work. Here's a sample UI Test:


- (void)testPostingQuestion
{
    XCUIApplication *app = [[XCUIApplication alloc] init];
    [app.tabBars.buttons[@"discussion"] tap];
  
    XCUIElement *addQuestionButton = app.navigationBars[@"Discussion"].buttons[@"Add"];
    [addQuestionButton tap];
  
    XCUIElement *addQuestionTitleTextField = app.textFields[@"Add question title..."];
    [addQuestionTitleTextField tap];
    [addQuestionTitleTextField typeText:@"Test Question"];
  
    XCUIElement *addQuestionBodyTextView = [app.textViews elementBoundByIndex:0];
    [addQuestionBodyTextView tap];
    [addQuestionBodyTextView typeText:@"Test Body"];
  
    XCUIElement *postQuestioButton = app.navigationBars[@"add question"].buttons[@"post"];
    [postQuestioButton tap];
}


When the event synthesis calls are made, the console outputs...


> t = 4.59s Synthesize event

> t = 4.85s Wait for app to idle


... then hangs for 30 seconds, and finally fails with ...


> UI Testing Failure - App failed to quiesce within 30.0s


The surprising part is that the event synthesis call that fails is different on almost every test run.

Finally, I've discovered that if I watch the simulator during a UI Test and notice that it is pausing for longer-than-expected on a event synthesis call, if I rotate clockwise and then counter-clockwise (or vice versa) the event synthesis will successfully continue onward until the next `tap` event. Maybe my assessment isn't that the synthesized event is hanging, but the "wait for app to idle" never notices that the tab bar controller switched view controllers or a new view controller was pushed on the stack or ... etc. Might this be an accessibility problem with my code (I don't think so, VoiceOver recognizes the transitions just fine)?

Is anyone else noticing similar behavior?

Some background context about the project that may prove helpful:

- The project has been under active development since 2012

- It supports iOS 8 only

- It builds without compiler warnings using the -Weverything flag

- Xcode 7 has "touched" all of the various files it likes auto-modifying (storyboard, xibs, build scheme, etc)

- Xcode 7 has upgraded info.plists and build settings

Accepted Answer

Hi,


I experience the same issue and had previously started a message about this here:


https://forums.developer.apple.com/message/14504


I have debugged this to the point that I know it is because I am doing some work updating the GUI after some background work occurs so I suspect that the testing framework isn't correctly detecting when the main thread has finished updating.

I kept debugging this and actually traced it down to a point where I was calling endRefreshing on a UIRefreshControl without a preceding startRefreshing. Once I checked the refreshing attribute, the test was able to succeed. While this isn't documented anywhere and my application seemed to run just fine in this case, the test clearly couldn't handle that.


My application is very asynchronous since it is relying on responses from a separate server and I was able to get it past the previous point. I would suggest commenting things out in your application until you could limit it down to a single offending line that causes it to stop.

-[XCUIElement tap] works intermittently - Hangs at "Wait for app to idle"
 
 
Q