XCTest - 如何查询导航栏标题中的子字符串

XCTest - How to query for substring in navbar title

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

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

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

但是,这有两个问题:

如何做到这一点?

试试这些。

1.Get 元素中的静态文本而不是从应用程序中获取。

  Eg:`XCUIApplication().navigationBars["Rent Properties"].staticTexts["Rent Properties"]`
  1. 使用 elementMatchingPredicateexpectationForPredicate 来匹配元素。

有用Link:http://masilotti.com/ui-testing-cheat-sheet/

要匹配子字符串 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 打印选项找出它。

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

您可以尝试使用:

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