Parents 和兄弟姐妹在 XCTest 中查询(UI 测试)

Parents and siblings in query in XCTest (UI testing)

在其他语言中可以进行这样的查询:

class:B text:"text_I_looking_for" sibling class:C

class:B text:"text_I_looking_for" parent class:A child class:C

如何在XCTest中执行这样的查询?我需要找到元素 1.检查它是否存在 2. 点击它。

PS 我知道 XCTest 的语法很简单,但在某些情况下,这还不够解决。也许可以使用 NSPredicate 语法?

今天发现Xcode7.3就是用这个机制:

XCUIApplication().otherElements.containingType(.Image, identifier:"circle").staticTexts["3"]

此查询将 return 值为“3”的 staticText 对象。 此对象的父对象为 'otherElement',兄弟对象为 ID 为 'circle' 的 'Image'。

.containingType 在 Swift 中被废弃了 3. 你可以使用 .containing 代替(我在 Xcode 13):

XCUIApplication().otherElements.containing(.image, identifier: "circle").staticTexts["3"]