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

前端之家收集整理的这篇文章主要介绍了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

猜你在找的Xcode相关文章