Josh Josh - 9 months ago 104
Swift Question

UI Testing swipe-to-delete table view cell

I'm trying to build a UI test in a basic shopping list app that makes sure swiping to delete a table view cell does in fact delete the cell from the table view.

I am running the test code below, but when it comes time to swipe the table view cell left (to display the delete button), it doesn't swipe. It appears that it may be tapping it, but it doesn't swipe. Because of this, the test fails when trying to tap the Delete button because "No matches found for button."

How does one test swipe to delete in a table view?

func testDeletingCell() {
let app = XCUIApplication()

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)

let textField2 = element.children(matching: .textField).element(boundBy: 1)

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

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

Answer Source

Try this new Swift 3 syntax instead:

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