xcode - 如何找出(使用 XCTest UITest)在 tvOS AppleTV 的表格 View 中具有焦点的单元格?

标签 xcode swift tvos xctest xcode-ui-testing

我正在尝试自动化 Apple TV (tvOS) 上应用的 UI 测试。

在登录屏幕上,我选择“输入用户名”字段。然后出现以前使用过的登录信息的屏幕。在屏幕上我想选择“清除全部”单元格。

澄清一下:“以前使用的”菜单似乎是内置在 tvOS 中的,只要有人在登录菜单中选择用户名文本字段,它就可能会自动出现。我对此不确定,所以我仍然需要确认该信息。

enter image description here

所以我认为这样的代码将是一个解决方案:

    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)
    }

但是即使在这样的状态下:

enter image description here

单元格的属性 hasFocusselected 似乎返回 false:

po self.app.cells.staticTexts["Clear All"].hasFocus
po self.app.cells.staticTexts["Clear All"].selected

enter image description here

当单元格处于“聚焦状态”时,如何找到“全部清除”单元格?

更新:

我已经尝试了@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. 每次按下远程按钮后始终延迟 1 秒,因为断言评估通常比焦点更改更快,因此返回 false

    XCUIRemote.sharedRemote().pressButton(.Down)
    sleep(1)
    
  2. 查找焦点元素的确切 UI 层次结构位置。例如 print(XCUIApplication().debugDescription) 将为您提供详细的 UI 树,并让您轻松正确地访问您的元素。

enter image description here

此断言已返回 true:

    XCTAssert(XCUIApplication().tabBars.buttons["Home"].hasFocus)

关于xcode - 如何找出(使用 XCTest UITest)在 tvOS AppleTV 的表格 View 中具有焦点的单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37466762/

相关文章:

ios - Storyboard Segue 过渡效果

javascript - tvOS 应用程序不加载 JavaScript 代码

ios - 如何判断游戏玩家是否在附近?

swift - 单个 Vision/CoreML 推理的多个 CALayer 更新

ios - 滚动 UICollectionView 时卡住

ios - tvos 应用程序是否需要约束?

ios - AVPlayer 元数据图稿不显示在 tvOS 上

ios - Storyboard 中自定义类集的本地化不起作用

ios - 无法定位或生成匹配的歌唱 Assets Xcode 7.2.1

ios - Storyboard UI 元素未显示在编辑器中