UITest:检查是否存在带前缀的文本
UITest: Check if text with prefix exists
在进行 UI 测试时,我可以测试文本是否存在,如下所示:
XCTAssertTrue(tablesQuery.staticTexts["Born: May 7, 1944"].exists)
但是,如果我只知道前缀,我该如何测试文本是否存在?
我想做这样的事情:
XCTAssertTrue(tablesQuery.staticTextWithPrefix["Born: "].exists)
甚至更好:
XCTAssertTrue(tablesQuery.staticTextWithRegex["Born: .+"].exists)
您可以使用谓词按前缀查找元素。例如:
let app = XCUIApplication()
let predicate = NSPredicate(format: "label BEGINSWITH 'Born: '")
let element = app.staticTexts.elementMatchingPredicate(predicate)
XCTAssert(element.exists)
请注意,如果有多个元素与谓词匹配,这可能会失败。可以在博客 post、Cheat Sheet for UI Testing.
中找到更多信息
在进行 UI 测试时,我可以测试文本是否存在,如下所示:
XCTAssertTrue(tablesQuery.staticTexts["Born: May 7, 1944"].exists)
但是,如果我只知道前缀,我该如何测试文本是否存在?
我想做这样的事情:
XCTAssertTrue(tablesQuery.staticTextWithPrefix["Born: "].exists)
甚至更好:
XCTAssertTrue(tablesQuery.staticTextWithRegex["Born: .+"].exists)
您可以使用谓词按前缀查找元素。例如:
let app = XCUIApplication()
let predicate = NSPredicate(format: "label BEGINSWITH 'Born: '")
let element = app.staticTexts.elementMatchingPredicate(predicate)
XCTAssert(element.exists)
请注意,如果有多个元素与谓词匹配,这可能会失败。可以在博客 post、Cheat Sheet for UI Testing.
中找到更多信息