ios – XCTest – 如何在导航栏标题中查询子字符串
发布时间:2020-12-14 17:44:27 所属栏目:百科 来源:网络整理
导读:我希望能够验证UI测试中导航栏中是否出现子字符串. 例如,如果导航栏标题是“Rent Properties”,那么我可以这样匹配: XCTAssert(XCUIApplication().staticTexts["Rent Properties"].exists) 但是,这有两个问题: 如果文本不在导航栏中,它仍将匹配 它完全匹配
我希望能够验证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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |