您如何在 Swift/iOS 中遍历 UI 测试中的元素?
how do you iterate through elements in UI testing in Swift/iOS?
我知道我该怎么做,例如查看 table 中的一个元素,但是简单地遍历所有元素的正确方法是什么,无论找到多少元素,例如点击它们?
let indexFromTheQuery = tables.staticTexts.elementBoundByIndex(2)
我猜答案是没有办法这样做。
原因是因为我在进行大量 UI 测试时使用了一个关键 iOS 组件:UIDatePicker。
如果您录制一个测试,将页面调高,然后旋转选择器,您会注意到所有命令都是非特定的并且与屏幕相关。然而,在选择器的情况下,社区请求导致添加了一个进行测试的方法:。
也许您可以向包含此 table 的任何控制器添加辅助方法。另外,请记住,通过将它们定义为仅在测试范围内的扩展,您可以轻松添加方法而不会污染 class 接口。
好的,我成功地找出了简单的语法。我刚刚开始使用 Swift,所以我花了很长时间才想到答案。
此代码有效:
var elementLabels = [String]()
for i in 0..<tables.staticTexts.count {
elementLabels.append (tables.staticTexts.elementBoundByIndex(i).label)
}
print (elementLabels)
对于基于 Xcode 11.2.1、SwiftUI 和 swift 5 的应用程序,以下代码适用于测试列表,本例中的每个元素在测试代码中显示为一个按钮。 table 是这样设置的(每一行):
NavigationLink(destination: TopicDetail(name: "Topic name", longDesc: "A description")) {
TopicRow(thisTopic: top).accessibility(identifier: "newTopicRow_\(top.name!)")
}
然后我通过将按钮放入数组中来捕获 table 的成员:
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)
最后,我通过选择有删除按钮的详细视图删除了 table 的每个成员,再次使用
.accessibility(identifier: "deleteTopic"
我想删除table的所有成员:
for topicLabel in elementLabels {
let myButton = app.buttons[topicLabel]
myButton.firstMatch.tap()
app.buttons["deleteTopic"].tap()
}
我知道我该怎么做,例如查看 table 中的一个元素,但是简单地遍历所有元素的正确方法是什么,无论找到多少元素,例如点击它们?
let indexFromTheQuery = tables.staticTexts.elementBoundByIndex(2)
我猜答案是没有办法这样做。
原因是因为我在进行大量 UI 测试时使用了一个关键 iOS 组件:UIDatePicker。
如果您录制一个测试,将页面调高,然后旋转选择器,您会注意到所有命令都是非特定的并且与屏幕相关。然而,在选择器的情况下,社区请求导致添加了一个进行测试的方法:
也许您可以向包含此 table 的任何控制器添加辅助方法。另外,请记住,通过将它们定义为仅在测试范围内的扩展,您可以轻松添加方法而不会污染 class 接口。
好的,我成功地找出了简单的语法。我刚刚开始使用 Swift,所以我花了很长时间才想到答案。
此代码有效:
var elementLabels = [String]()
for i in 0..<tables.staticTexts.count {
elementLabels.append (tables.staticTexts.elementBoundByIndex(i).label)
}
print (elementLabels)
对于基于 Xcode 11.2.1、SwiftUI 和 swift 5 的应用程序,以下代码适用于测试列表,本例中的每个元素在测试代码中显示为一个按钮。 table 是这样设置的(每一行):
NavigationLink(destination: TopicDetail(name: "Topic name", longDesc: "A description")) {
TopicRow(thisTopic: top).accessibility(identifier: "newTopicRow_\(top.name!)")
}
然后我通过将按钮放入数组中来捕获 table 的成员:
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)
最后,我通过选择有删除按钮的详细视图删除了 table 的每个成员,再次使用
.accessibility(identifier: "deleteTopic"
我想删除table的所有成员:
for topicLabel in elementLabels {
let myButton = app.buttons[topicLabel]
myButton.firstMatch.tap()
app.buttons["deleteTopic"].tap()
}