gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 08:17:53 25 4
gpt4 key购买 nike

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

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

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

但是,这有两个问题:

  • 如果文本不在导航栏中,它仍会匹配
  • 它进行精确匹配,而我希望能够匹配诸如“Rent”之类的子字符串

如何做到这一点?

最佳答案

要匹配子串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.

对于第一个选项,元素显示与不显示肯定是有区别的。您必须在 Debug模式下使用 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

关于ios - XCTest - 如何查询导航栏标题中的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39991728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com