如何在 UITests 下重新排序单元格?

How to reorder cells under UITests?

连同 UITestsUITableView 我需要重新排序单元格。可能吗?我试过 myTable.swipeDown() 但它是在不响应重新排序的单元格位置调用的。我怎样才能做到这一点?有可能吗?

如果您正确设置了自定义单元格的辅助功能属性,那么重新排序控件应该可以访问。让我们假设单元格的可访问性 labels/identifiers 分别设置为 "LondonStreet" 和 "BakerStreet"。

您可以通过点击并从一个重新排序控件拖动到下一个重新排序单元格。这些控件的辅助功能标识符是根据单元格的信息自动设置的。

let app = XCUIApplication()
app.launch()

let topButton = app.buttons["Reorder LondonStreet"]
let bottomButton = app.buttons["Reorder BakerStreet"]
bottomButton.pressForDuration(0.5, thenDragToElement: topButton)

"Reorder " 前缀由 OS 设置。尝试使用辅助功能检查器查看重新排序控件的值。

如果您想在您的机器上试用,我已经在我的 UI Testing Cheat Sheet with some working sample code 中添加了一个示例。

要启用 Accessibility Inspector,您首先必须转到:

系统偏好设置 > 安全与隐私:

然后Xcode > 打开开发者工具 > Accessibility Inspector

更通用的解决方案:(如果您不知道标题)

guard let firstCell = app.cells.allElementsBoundByIndex.first, let topButton = firstCell.buttons.allElementsBoundByIndex.last else { XCTFail("no reoder btn for first cell"); return }
guard let bottomCell = app.cells.allElementsBoundByIndex.last, let bottomButton = bottomCell.buttons.allElementsBoundByIndex.last else { XCTFail("no reoder btn for last cell"); return }
topButton.press(forDuration: 0.5, thenDragTo: bottomButton)