如果您可以使用firstMatch 而不是element,并且还可以避免使用count,您或许可以避免渲染整个表格。
我进行了一项测试,检查表格前两个单元格中的预期标签。起初,我使用app.table.cells.element(boundBy: 0) 和app.table.cells.element(boundBy: 1) 来查找第一个和第二个单元格。这导致在我可以访问单元格之前呈现整个表格。
我调整了我的测试,使其不太精确,但对我来说仍然足够好(考虑到否则会花费大量时间)。相反,我将matching 与预期标签值的谓词一起使用firstMatch,以查找符合我想要的条件的第一个单元格。这样,一旦找到它们,遍历就会停止(并且由于它们位于表的顶部,因此很快)。
这是之前和之后的代码。
之前(缓慢,但更精确):
private func checkRhymes(query: String, expectedFirstRhyme: String, expectedSecondRhyme: String) {
let table = app.tables.element
let cell0 = table.cells.element(boundBy: 0)
let cell1 = table.cells.element(boundBy: 1)
let actualRhyme0 = cell0.staticTexts.matching(identifier: "RhymerCellWordLabel").firstMatch.label
let actualRhyme1 = cell1.staticTexts.matching(identifier: "RhymerCellWordLabel").firstMatch.label
XCTAssertEqual(expectedFirstRhyme, actualRhyme0, "Expected first rhyme for \(query) to be \(expectedFirstRhyme) but found \(actualRhyme0)")
XCTAssertEqual(expectedSecondRhyme, actualRhyme1, "Expected first rhyme for \(query) to be \(expectedSecondRhyme) but found \(actualRhyme1)")
}
更快,但不太精确(但足够好):
private func checkRhymes(query: String, expectedFirstRhyme: String, expectedSecondRhyme: String) {
let table = app.tables.firstMatch
let label0 = table.cells.staticTexts.matching(NSPredicate(format: "label = %@", expectedFirstRhyme)).firstMatch
let label1 = table.cells.staticTexts.matching(NSPredicate(format: "label = %@", expectedSecondRhyme)).firstMatch
// We query for the first cells that we find with the expected rhymes,
// instead of directly accessing the 1st and 2nd cells in the table,
// for performance issues.
// So we can't add assertions for the "first" and "second" rhymes.
// But we can at least add assertions that both rhymes are visible,
// and the first one is above the second one.
XCTAssertTrue(label0.frame.minY < label1.frame.minY)
XCTAssertTrue(label0.isHittable)
XCTAssertTrue(label1.isHittable)
}
参考:
https://developer.apple.com/documentation/xctest/xcuielementquery/1500515-element
当您期望查询结果时,使用元素属性访问查询结果
查询的单个匹配元素,但要检查多个
访问结果之前的模棱两可的匹配。元素属性
遍历您应用的可访问性树以检查多个匹配项
返回之前的元素,如果没有则当前测试失败
单个匹配元素。
如果您明确知道会有一个
匹配元素,使用 XCUIElementTypeQueryProvider firstMatch
而是财产。 firstMatch 停止遍历您应用的可访问性
层次结构一旦找到匹配的元素,加速元素
查询解析。