微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐