简体   繁体   English

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

[英]How to find out the (using XCTest UITest) a cell that has a focus in a table view in tvOS AppleTV?

I'm trying to automate UI tests of an app on Apple TV (tvOS).我正在尝试在 Apple TV (tvOS) 上自动执行应用程序的 UI 测试。

On the login screen I select the 'enter username' field.在登录屏幕上,我选择“输入用户名”字段。 Then the screen with previously used logins appears.然后会出现带有以前使用过的登录信息的屏幕。 On the screen I would like to select 'clear all' cell.在屏幕上,我想选择“全部清除”单元格。

To clarify: the "Previously Used" menu seems to be built-in in the tvOS and it might appear automatically whenever someone selects User name textfield in a login menu.澄清一下:“以前使用过”菜单似乎内置在 tvOS 中,只要有人在登录菜单中选择用户名文本字段,它就会自动出现。 I'm unsure about that, so still I need to confirm that information.我对此不确定,所以我仍然需要确认该信息。

在此处输入图片说明

So I thought the code like this would be a solution:所以我认为这样的代码将是一个解决方案:

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

however even in a state like this:然而,即使在这样的状态:

在此处输入图片说明

the properties hasFocus and selected of the cell seem to return false:单元格的属性hasFocusselected似乎返回false:

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

在此处输入图片说明

How to find the "Clear All" cell when it has "focused state"?具有“聚焦状态”时如何找到“全部清除”单元格?

UPDATE:更新:

I've tried the solution proposed by @Oletha, but with no success.我已经尝试了@Oletha 提出的解决方案,但没有成功。 clearAllCell.hasFocus unfortunately seems to return always false.不幸的是, 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)
    }

UPDATE:更新:

A non-elegant solution at the moment is to:目前一个不优雅的解决方案是:

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

I had similar problems with my app and found 2 things helpful to write running UI tests:我的应用程序遇到了类似的问题,并发现 2 件事有助于编写正在运行的 UI 测试:

  1. Always make an 1 second delay after every remote button press, because the assert evaluation is often faster than the focus change and therefore returns false每次按下远程按钮后始终延迟 1 秒,因为断言评估通常比焦点更改快,因此返回false

     XCUIRemote.sharedRemote().pressButton(.Down) sleep(1)
  2. Look up the the exact UI hierarchy position of your focused element.查找焦点元素的确切 UI 层次结构位置。 For example print(XCUIApplication().debugDescription) will give you an detailed UI tree and makes it easy to access your element properly.例如print(XCUIApplication().debugDescription)将为您提供详细的 UI 树,并使您可以轻松正确地访问您的元素。

在此处输入图片说明

This assert has returned true:此断言已返回 true:

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

Use expectationForPredicate and waitForExpectationsWithTimeout to check if the cell has focus.使用expectationForPredicatewaitForExpectationsWithTimeout来检查单元格是否有焦点。 Using expectations causes the accessibility hierarchy snapshot to be refreshed for each time the check occurs.使用期望会导致每次检查发生时刷新可访问性层次结构快照。 Your while loop is checking against the same cached hierarchy snapshot every time, so the result won't change.您的 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() didn't actually work for me, but I'd guess this is working for you - I've not got an Apple TV app to test with. XCUIRemote.sharedRemote()实际上对我不起作用,但我猜这对你XCUIRemote.sharedRemote() - 我没有 Apple TV 应用程序可以测试。

first of all it's a native screen in tvOS So all the suggestions related to try and set accessibility ids are not relevant.首先,它是 tvOS 中的本机屏幕,因此所有与尝试和设置辅助功能 ID 相关的建议都无关紧要。 Tryi to put a breakpoint when you reach that screen and print out which element is focused尝试在到达该屏幕时放置一个断点并打印出聚焦的元素

I believe the answer will be.我相信答案会是。 A position on screen.屏幕上的位置。 Or cell without identifier.或没有标识符的单元格。 I'm struggling with Same issues on Apple TV and would like to know about all the problems you have since I wonder how mature xcuitest is for tvOS我在 Apple TV 上遇到了同样的问题,我想知道您遇到的所有问题,因为我想知道 xcuitest 对 tvOS 有多成熟

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM