ios – XCTest – 如何在导航栏标题中查询子字符串

我希望能够验证UI测试中导航栏中是否出现子字符串.

例如,如果导航栏标题是“Rent Properties”,那么我可以这样匹配:

XCTAssert(XCUIApplication().staticTexts["Rent Properties"].exists)

但是,这有两个问题:

>如果文本不在导航栏中,它仍将匹配
>它完全匹配,而我希望能够匹配一个子字符串,如“租”

如何才能做到这一点?

解决方法

对于匹配子字符串Rent,您可以使用以下代码

XCUIApplication().staticTexts.matchingPredicate(nspredicate(format: "label CONTAINS 'Rent'")).elementBoundByIndex(0)
//it may contains one or more element with substring Rent.
//you have to find out which element index you want in debug mode using p print() options.

对于第一个选项,当元素显示不显示时,肯定必须存在差异.你必须在调试模式下使用po或p print选项找出它.

例如,可能计数不同或元素不可命中等等….

你可以尝试使用:

let app = XCUIApplication()
XCTAssert(app.staticTexts["Rent Properties"].exists)

or 
let app = XCUIApplication()
app.staticTexts["Rent Properties"].hittable

or
let app = XCUIApplication()
app.staticTexts["Rent Properties"].enabled

or 

app.staticTexts.matchingIdentifier("Rent Properties").count
//take count while showing the text and take the count while not showing the text

相关文章

在有效期内的苹果开发者账号(类型为个人或者公司账号)。还...
Appuploader官网--IOS ipa上传发布工具,证书制作工具跨平台...
苹果在9月13号凌晨(北京时间)发布 iOS 16,该系统的设备可...
计算机图形学--OpenGL递归实现光线追踪
Xcode 14打出来的包在低版本系统运行时会崩溃,报错信息是Li...