如何找到(使用XCTest UITest)在tvOS AppleTV的表格视图中具有焦点的单元格?

问题描述 投票:1回答:3

我正在尝试在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似乎总是假的。

    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)
    }
xcode swift tvos xctest xcode-ui-testing
3个回答
1
投票

我的应用程序遇到类似的问题,发现有两件事有助于编写运行的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)

0
投票

使用expectationForPredicatewaitForExpectationsWithTimeout检查细胞是否有焦点。使用期望会导致每次检查发生时刷新可访问性层次结构快照。您的while循环每次都会检查相同的缓存层次结构快照,因此结果不会更改。

let app = XCUIApplication()
let clearAllCell = app.cells.staticTexts["Clear All"]
let focusedPredicate = NSPredicate(format: "exists == true")
let remote = XCUIRemote.sharedRemote()

expectationForPredicate(focusedPredicate, evaluatedWithObject: clearAllCell, handler: {() -> Bool in
    // Check if the cell has focus
    if clearAllCell.hasFocus {
        // Fulfill the expectation
        return true
    } else {
        // Move down one cell before the next check
        remote.pressButton(.Down)
        return false
    }
})
waitForExpectationsWithTimeout(5, handler: nil)

remote.pressButton(.Select)

XCUIRemote.sharedRemote()实际上并没有为我工作,但我猜这对你有用 - 我没有Apple TV应用程序可以测试。


0
投票

首先,它是tvOS中的原生屏幕所以所有与try和set accessibility id相关的建议都不相关。当你到达那个屏幕并打印出哪个元素被聚焦时,尝试设置一个断点

我相信答案会是。屏幕上的位置。或没有标识符的单元格我在Apple TV上遇到同样的问题,想知道你遇到的所有问题,因为我想知道tvOS有多成熟xcuitest

© www.soinside.com 2019 - 2024. All rights reserved.