防止从失败的测试中访问不存在的元素

时间:2017-07-18 04:22:13

标签: swift xctest xcode-ui-testing

在一个只包含20行表视图的应用程序中,所有这些都包含文本“Cell X”,X是行的索引,我有以下测试:

func testTwentyFirstRow()
{
    let cell = XCUIApplication().staticTexts["Cell 20"]
    let label = cell.label  
    XCTAssert(label == "Cell 20", "Label is not equal to Cell 20")
}

此测试在第二行失败并出现以下错误

No matches found for Find: Elements matching predicate '"Cell 20" IN identifiers' from input {(
    StaticText, 0x60800019b930, traits: 8589934656, label: 'Cell 0',
    ...
    StaticText, 0x60800019d740, traits: 8589934656, label: 'Cell 19'
)}

是否有任何可以阻止它在那里失败但是在下一行失败而不是我断言标签的值?

1 个答案:

答案 0 :(得分:0)

如果您访问除exists以外的XCUIElement的属性,框架将尝试解析导致该元素的查询,然后检查它以查找该属性的值。

如果在检查其他任何内容或与之交互之前检查元素exists,则可以处理它尚不存在的情况。

if cell.exists {
    let label = cell.label
    XCTAssertEqual(label, "text")
} else {
    XCTFail("Cell did not exist")
}

此时您不必通过测试,您可以采取措施使用循环来保持单元格的存在,直到它确实存在,或者您喜欢的任何内容。

相关问题