UI测试firstMatch

Alp*_*per 3 ios xcode-ui-testing

它是安全的,以取代在我的UI测试的代码,所有实例.element(boundBy: 0).firstMatch

我会这样认为,但是Apple的文档对此不太清楚(特别是在多次匹配的情况下,尽管我只关心第一个)。 https://developer.apple.com/documentation/xctest/xcuielementtypequeryprovider/2902250-firstmatch

Ole*_*tha 6

相反的交换element(boundBy: 0)firstMatch,你应该把它们连。

XCUIApplication().cells.element(boundBy:0).firstMatch
Run Code Online (Sandbox Code Playgroud)

如果屏幕上有多个单元格,这将使查询的解析速度更快。不使用firstMatch,测试将首先获取所有单元格的列表,然后使用第一个。使用firstMatch,测试将查找单元格并使用找到的第一个单元格。

仅在您明确知道单个元素将与查询匹配的情况下,才使用firstMatch。

您仍然应该element(boundBy:)在可能与查询匹配的元素可能超过1个的任何地方进行操作,以确保在使用时firstMatch,只有一个元素可以与该查询匹配。在有3个单元格并且要使用第一个单元格的情况下,您仍应使用element(boundBy: 0)。如果您知道屏幕上只有1个单元格,则可以跳过element(boundBy: 0)并使用firstMatch而不是element更快地解决查询。

// fine if there is only one cell
XCUIApplication().cells.element // also checks for multiple matches
// also fine if there is only one cell
XCUIApplication().cells.firstMatch // does not check for multiple matches
Run Code Online (Sandbox Code Playgroud)

  • 但是在没有 element(boundBy: 0) 的情况下使用 firstMatch 会有什么问题?它现在似乎可以工作。 (2认同)