UI 测试滑动删除 table 视图单元格

UI Testing swipe-to-delete table view cell

我正在尝试在一个基本的购物清单应用程序中构建一个 UI 测试,以确保滑动以删除 table 视图单元格实际上会从 table 查看。

我是下面的测试代码运行,但是当要向左滑动table视图单元格(显示删除按钮)时,它不会滑动。看起来它可能正在点击它,但它不会滑动。因此,尝试点击删除按钮时测试失败,因为 "No matches found for button."

如何在 table 视图中进行滑动删除测试?

    func testDeletingCell() {
       let app = XCUIApplication()
       app.navigationBars["ShoppingList.ShoppingListView"].buttons["Add"].tap()

       let element = app.otherElements.containing(.navigationBar, identifier:"ShoppingList.AddShoppingListItemView").children(matching: .other).element.children(matching: .other).element.children(matching: .other).element
       let textField = element.children(matching: .textField).element(boundBy: 0)
       textField.tap()
       textField.typeText("abc")

       let textField2 = element.children(matching: .textField).element(boundBy: 1)
       textField2.tap()
       textField2.typeText("123")
       app.navigationBars["ShoppingList.AddShoppingListItemView"].buttons["Save"].tap()

       let tablesQuery = app.tables
       tablesQuery.children(matching: .cell).element(boundBy: 0).staticTexts["123"].swipeLeft()
       tablesQuery.buttons["Delete"].tap()

       XCTAssert(app.tables.cells.count == 0)
   }

试试这个新的 Swift 3 语法:

let tablesQuery = app.tables.cells
tablesQuery.element(boundBy: 0).swipeLeft()
tablesQuery.element(boundBy: 0).buttons["Delete"].tap()

Xcode 12.4 | Swift 5

删除 XCTestCase 中 UITableView 内的所有单元格:

let app = XCUIApplication()
app.launch()

//Ensure your UIViewController loaded with data
sleep(5)

let tablesQuery = app.tables["<YourTableViewIdentifier>"].cells         
for i in 0..<tablesQuery.allElementsBoundByAccessibilityElement.count{
    tablesQuery.element(boundBy: 0).swipeLeft()
    //Sometimes the swipeLeft() itself is enough to delete the cell,
    //but if it is not use what @Rashwan L suggests
    //tablesQuery.element(boundBy: 0).buttons["Delete"].tap()
}         
XCTAssertEqual(tablesQuery.allElementsBoundByAccessibilityElement.count, 0)

我没有向左滑动然后点击删除的原因是因为 swipeLeft() 已经足以从 UITableView 中删除我的单元格。

如果您想在单元格上向左滑动并确保它不会被删除,请使用:

swipeLeft(velocity: .slow)

此速度是 XCUIGestureVelocity,您可以将其设置为 .fast、.slow 或其默认值。