无法在Xcode UI Test中点击UIBarButtonItem

Ian*_*nce 4 xcode objective-c ios xcode-ui-testing

我的导航栏上有一个"添加"按钮,我需要让Xcode的UI测试点击该按钮,在它打开的视图控制器中执行测试.我以编程方式添加按钮,如下所示:

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showAddVC)];
self.navigationItem.rightBarButtonItem = addButton;
Run Code Online (Sandbox Code Playgroud)

在我的测试中,我有:

XCUIApplication *app = [[XCUIApplication alloc] init];
XCTAssert([app.buttons[@"Add"] exists]); // <-- This passes, so the test runner does see the button.
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用以下两种方式来点击它时:

// Generated using the test recorder
[app.navigationBars[@"App Title"].buttons[@"Add"] tap];
Run Code Online (Sandbox Code Playgroud)

要么:

// Same expression used with the XCTAsset earlier
[app.buttons[@"Add"] tap];    
Run Code Online (Sandbox Code Playgroud)

什么都没发生.点击按钮时应该执行的操作不会发生.我尝试在行sleep(5)之间添加一些内容以让应用程序加载,但这没有多大帮助.

这是测试日志:

Test Case '-[xx]' started.
    t =     0.00s     Start Test
    t =     0.00s     Set Up
2015-12-22 16:25:02.898 XCTRunner[10978:384690] Continuing to run tests in the background with task ID 1
    t =     0.94s         Launch xx
    t =     1.01s             Waiting for accessibility to load
    t =     3.45s             Wait for app to idle
    t =     9.02s     Tap "Add" Button
    t =     9.02s         Wait for app to idle
    t =    39.07s             Assertion Failure: UI Testing Failure - App failed to quiesce within 30s
xx: error: -[xx] : UI Testing Failure - App failed to quiesce within 30s
Run Code Online (Sandbox Code Playgroud)

Ste*_*n S 5

上述答案都不适合我.经过几个小时的奋斗之后,最终使它发挥作用的是重复敲击.试试这个:

[app.navigationBars[@"App Title"].buttons[@"Add"] tap];
[app.navigationBars[@"App Title"].buttons[@"Add"] tap];
Run Code Online (Sandbox Code Playgroud)

虽然上面最初对我有用,但我发现有时第一个水龙头工作,这将导致两个水龙头.我的解决方案是在UI测试开始时点击不触发任何操作的任意UI元素,然后正常进行.我认为第一次点击适用于某些设备,或者可能在第一次UI测试运行之后.