我正在尝试自动化 Apple TV (tvOS) 上应用的 UI 测试。
在登录屏幕上,我选择“输入用户名”字段。然后出现以前使用过的登录信息的屏幕。在屏幕上我想选择“清除全部”单元格。
澄清一下:“以前使用的”菜单似乎是内置在 tvOS 中的,只要有人在登录菜单中选择用户名文本字段,它就可能会自动出现。我对此不确定,所以我仍然需要确认该信息。
所以我认为这样的代码将是一个解决方案:
if self.app.staticTexts["Previously Used"].exists {
let clearAllCell = self.app.cells.staticTexts["Clear All"]
while clearAllCell.hasFocus == false {
XCUIRemote.sharedRemote().pressButton(.Down)
sleep(1)
}
XCUIRemote.sharedRemote().pressButton(.Select)
}
但是即使在这样的状态下:
单元格的属性 hasFocus
和 selected
似乎返回 false:
po self.app.cells.staticTexts["Clear All"].hasFocus
po self.app.cells.staticTexts["Clear All"].selected
当单元格处于“聚焦状态”时,如何找到“全部清除”单元格?
更新:
我已经尝试了@Oletha提出的解决方案,但没有成功。 clearAllCell.hasFocus
不幸的是,似乎总是返回 false。
if self.app.staticTexts["Previously Used"].exists {
let clearAllCell = self.app.cells.staticTexts["Clear All"]
let predicateHasFocus = NSPredicate(format: "exists == true", argumentArray: nil)
expectationForPredicate(predicateHasFocus, evaluatedWithObject: clearAllCell, handler: { () -> Bool in
if clearAllCell.hasFocus {
return true
} else {
sleep(1)
XCUIRemote.sharedRemote().pressButton(.Down)
return false
}
})
waitForExpectationsWithTimeout(10, handler: nil)
XCUIRemote.sharedRemote().pressButton(.Select)
}
更新:
目前一个不太优雅的解决方案是:
if self.app.staticTexts["Previously Used"].exists {
var cellsCount = self.app.cells.count
// Select Clear All menu button, hack as it was not possible to detect "Clear All" cell in another way
while cellsCount > 0 {
sleep(1)
XCUIRemote.sharedRemote().pressButton(.Down)
cellsCount -= 1
}
sleep(1)
XCUIRemote.sharedRemote().pressButton(.Up)
sleep(1)
XCUIRemote.sharedRemote().pressButton(.Select)
}
最佳答案
我的应用程序也遇到了类似的问题,并发现有两件事有助于编写运行的 UI 测试:
每次按下远程按钮后始终延迟 1 秒,因为断言评估通常比焦点更改更快,因此返回
false
XCUIRemote.sharedRemote().pressButton(.Down) sleep(1)
查找焦点元素的确切 UI 层次结构位置。例如
print(XCUIApplication().debugDescription)
将为您提供详细的 UI 树,并让您轻松正确地访问您的元素。
此断言已返回 true:
XCTAssert(XCUIApplication().tabBars.buttons["Home"].hasFocus)
关于xcode - 如何找出(使用 XCTest UITest)在 tvOS AppleTV 的表格 View 中具有焦点的单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37466762/