ios - 用户界面测试 : Check if text with prefix exists

标签 ios xcode swift xcode-ui-testing

在进行 UI 测试时,我可以测试文本是否存在,如下所示:

XCTAssertTrue(tablesQuery.staticTexts["Born: May 7, 1944"].exists)

但是,如果我只知道前缀,我该如何测试文本是否存在?

我想做这样的事情:

XCTAssertTrue(tablesQuery.staticTextWithPrefix["Born: "].exists)

甚至更好:

XCTAssertTrue(tablesQuery.staticTextWithRegex["Born: .+"].exists)

最佳答案

您可以使用谓词按前缀查找元素。例如:

let app = XCUIApplication()
let predicate = NSPredicate(format: "label BEGINSWITH 'Born: '")
let element = app.staticTexts.elementMatchingPredicate(predicate)
XCTAssert(element.exists)

请注意,如果不止一个元素与谓词匹配,这可能会失败。更多信息可以在博客文章中找到,Cheat Sheet for UI Testing .

关于ios - 用户界面测试 : Check if text with prefix exists,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37442240/

相关文章:

ios - 查询 Parse 中可能为 nil 的值

xcode - Xcode 5 中缺少配置文件菜单项

swift - 根据条件添加约束的奇怪行为

ios - UITableViewCell 上的 AutoLayout 问题

ios - ObjC : differentiating between NSValue and NSNumber

ios - Swift/XCode 编辑类或 UIButton 的所有实例

ios - 通过标识符快速转场导致 EXC_BAD_ACCESS

ios - 苹果拒绝了与ipv6连接有关的iOS App

Xcode 搜索无法正常工作

Swift 4 map 不显示用户当前位置