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

6

我希望能够在UI测试中验证子字符串是否出现在导航栏中。与此类似,如果导航栏标题为“租赁属性”,则可以使用以下方法进行匹配:

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

然而,这存在两个问题:

  • 如果文本不在导航栏中,它仍将匹配
  • 它进行精确匹配,而我想能够匹配子字符串,如“Rent”

怎么做呢?

2个回答

7

要匹配子字符串“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

5

试试这些方法。

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

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

有用的链接:http://masilotti.com/ui-testing-cheat-sheet/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接