当我在 XCode UI 测试中运行以下步骤时。我得到多个输出。
print(collectionViews.staticTexts.element(matching: NSPredicate(format: "(%@ IN identifiers), "price")).debugDescription)
输出:
Find: Elements matching predicate '"price" IN identifiers'
Output: {
StaticText 0x608000375f00: traits: 8589934656, {{24.0, 384.0}, {197.0, 27.5}}, identifier: 'price', label: '123'
StaticText 0x6080003768c0: traits: 8589934656, {{24.0, 762.0}, {197.0, 27.5}}, identifier: 'price', label: '456'
}
无论如何,我可以使用 swift.获得单个输出而不是多个输出吗?我正在尝试打印标签,即123。
我正在寻找的输出:123
注意:标签值不断变化。
最佳答案
如果您在测试期间知道元素的索引,那么您可以根据索引路径定义accessibilityIdentifier,然后在 uiautomation 测试中使用它来获取元素。
例如:
// suppose label has traits of a button
label.accessibilityIdentifier = "<some constant>:\(indexPath.row)"
// test class
let label = table.buttons["<some constant>:\(<row index>)"] //this will give u only one element
label.tap()
关于ios - 使用 NSPredicate 选择任一输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39961667/