Xcode 7 中的 UITests 发现错误的 'Next' 按钮

UITests in Xcode 7 finds wrong 'Next' button

我有一个如下所示的测试:

func testNextButtonDisabled() {
  let app = XCUIApplication()
  XCTAssertFalse(app.buttons["Next"].enabled)
}

此测试失败,因为除了我自己创建的 "Next" 按钮外,键盘 return 按钮还标记为 'Next'。此测试失败并出现错误:

UI Testing Failure - Multiple matches found

如何区分我自己的 'Next' 按钮和键盘 'Next' 按钮?

这个问题的具体解决方案是寻找作为主要 window.

后代的元素
func testNextButtonDisabled() {
  let app = XCUIApplication()
  XCTAssertFalse(app.childrenMatchingType(.Window).elementBoundByIndex(0).buttons["Next"].enabled)
}

对于解决此类问题的一般解决方案:在 Xcode 运行 中再次查看 "Record UI Test" 以查看 Xcode 认为您应该引用其中的元素你有兴趣。