【发布时间】:2015-12-23 00:28:08
【问题描述】:
我的导航栏上有一个“添加”按钮,我需要让 Xcode 的 UI 测试点击该按钮以在它打开的视图控制器中执行测试。我像这样以编程方式添加按钮:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showAddVC)];
self.navigationItem.rightBarButtonItem = addButton;
在我的测试中,我有:
XCUIApplication *app = [[XCUIApplication alloc] init];
XCTAssert([app.buttons[@"Add"] exists]); // <-- This passes, so the test runner does see the button.
但是当我尝试使用以下任一方法点击它时:
// Generated using the test recorder
[app.navigationBars[@"App Title"].buttons[@"Add"] tap];
或:
// Same expression used with the XCTAsset earlier
[app.buttons[@"Add"] tap];
什么都没有发生。当点击按钮时应该发生的动作不会发生。我尝试在行之间添加一些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
【问题讨论】:
-
日志告诉你主线程上发生了其他事情,因此无法点击按钮。你需要思考为什么会这样。
-
我将如何进行测试?暂停调试器显示线程 1 只是 main.m 调用。
-
断言失败可能是由框架中的错误引起的。试试这个——在你点击添加按钮之前,试着点击屏幕上的任何地方——例如导航栏本身。然后在该点击语句之后的行上放置一个断点。当您运行测试并到达该断点时,您可以在 LLDB 中打印出视图层次结构。确认 LLDB 中存在 Add 按钮。根据上面的错误陈述,我认为 XCTest 能够看到“添加”按钮 - 我不认为这是让你失望的错误。
标签: ios objective-c xcode xcode-ui-testing