繁体   English   中英

你如何在 Swift/iOS 的 UI 测试中迭代元素?

[英]how do you iterate through elements in UI testing in Swift/iOS?

我明白我如何可以,例如查看表格中的一个元素,但是简单地遍历所有元素的正确方法是什么,无论找到多少元素,例如点击它们?

let indexFromTheQuery = tables.staticTexts.elementBoundByIndex(2)

好的,我成功地找出了简单的语法。 我刚刚开始使用 Swift,所以我花了很长时间才想出答案。

此代码有效:

var elementLabels = [String]()
for i in 0..<tables.staticTexts.count {
    elementLabels.append (tables.staticTexts.elementBoundByIndex(i).label)
}  
print (elementLabels)

我的猜测是答案是没有办法这样做。

原因是我在进行大量 UI 测试时使用了一个关键的 iOS 组件:UIDatePicker。

如果您在打开页面的位置录制测试,然后旋转选择器,您会注意到所有命令都是非特定的,并且与屏幕相关。 然而,在选择器的情况下,社区请求导致添加了一种进行测试的方法: 如何在 Xcode 的 iOS UI 测试中选择选择器视图项? .

也许您可以向包含此表的任何控制器添加一个辅助方法。 另外,请记住,通过将方法定义为仅在测试范围内的扩展,您可以轻松添加方法而不会污染类接口。

对于基于 Xcode 11.2.1、SwiftUI 和 swift 5 的应用程序,以下代码适用于测试列表,在这种情况下,每个元素在测试代码中都显示为一个按钮。 该表是这样设置的(对于每一行):

         NavigationLink(destination: TopicDetail(name: "Topic name", longDesc: "A description")) {
                TopicRow(thisTopic: top).accessibility(identifier: "newTopicRow_\(top.name!)")
            }

然后我通过将按钮放入数组来捕获表的成员:

    let myTable = app.tables.matching(identifier: "newTopicTable")

    var elementLabels = [String]()

    for i in 0..<myTable.buttons.count {
        elementLabels.append (tablesQuery.buttons.element(boundBy: i).label)
    }

    print (elementLabels)

最后,我通过选择我有删除按钮的详细视图删除了表的每个成员,再次使用

.accessibility(identifier: "deleteTopic"

我想删除表的所有成员:

    for topicLabel  in elementLabels {

        let myButton = app.buttons[topicLabel]

        myButton.firstMatch.tap()

        app.buttons["deleteTopic"].tap()


    }

暂无
暂无

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

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