【问题标题】:UI Testing swipe-to-delete table view cellUI测试滑动删除表格视图单元格
【发布时间】:2016-10-23 03:13:15
【问题描述】:

我正在尝试在一个基本的购物清单应用程序中构建一个 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)
   }

【问题讨论】:

  • 是否正确找到了 123 元素?如果你看模拟器,它真的被刷了吗?

标签: ios swift uitableview xcode-ui-testing


【解决方案1】:

试试这个新的Swift 3 语法:

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

【讨论】:

  • 这就是答案。谢谢,拉什万。
  • @Josh,很高兴为您提供帮助。
【解决方案2】:

Xcode 12.4 | 斯威夫特 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。

【讨论】:

  • 嘿塞巴斯蒂安,在编写我的测试时,我遇到了你的解决方案,因为我的失败很严重:while !cells.isEmpty { cells[0].swipeLeft() cells[0].buttons["Delete "].tap() } 未能获得匹配的快照:没有从输入 {( Application, pid: 1233 )} 中找到匹配类型表的后代匹配项您碰巧知道这是为什么吗?
猜你喜欢
  • 1970-01-01
  • 2016-01-20
  • 1970-01-01
  • 2018-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多