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

时间:2016-10-23 03:13:15

标签: ios swift uitableview xcode-ui-testing

我正在尝试在基本购物列表应用中构建一个UI测试,确保滑动以删除表格视图单元格确实从表格视图中删除了该单元格。

我正在运行下面的测试代码,但是当需要向左滑动表格视图单元格(以显示删除按钮)时,它不会滑动。它似乎可能正在点击它,但它不会滑动。因此,尝试点击“删除”按钮时测试失败,因为“找不到匹配按钮。”

如何在表格视图中滑动删除以进行删除?

    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)
   }

2 个答案:

答案 0 :(得分:14)

请尝试使用这种新的Swift 3语法:

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

答案 1 :(得分:0)

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 或其 .default。

相关问题