我是单元测试tableview,我需要逻辑滑动? UITableView中的一行。是否可以在单元测试中执行滑动来调用处理程序?我查看了UITableViewDelegate,但没有滑动操作(didSelectRowAt存在并在单元测试中进行了测试)。
func createDeleteHandler(tableView : UITableView, indexPath : IndexPath) -> UIContextualAction.Handler {
let deleteHandler = { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
let noteToBeDeleted = self.notes[indexPath.row]
NoteManager.shared.deleteNote(note: noteToBeDeleted)
self.notes.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
success(true)
}
return deleteHandler
}
你可以像这样使用XCUITest测试:
import XCTest
class MoreUITests: XCTestCase {
override func setUp() {
continueAfterFailure = false
XCUIApplication().launch()
}
func testUI() {
let app = XCUIApplication()
let tablesQuery = app.tables
let addButton = app.navigationBars["Master"].buttons["Add"]
let masterButton = app.navigationBars["Detail"].buttons["Master"]
addButton.tap() // adds Item-0
addButton.tap() // adds Item-1
addButton.tap() // adds Item-2
addButton.tap() // adds Item-3
tablesQuery.staticTexts["Item-1"].tap()
// Go back
masterButton.tap()
// Swipe Left on item-2
tablesQuery.staticTexts["Item-2"].swipeLeft()
}
}
最简单的方法是使用Xcode UI Recorder记录它们。更多详情:
这是我用滑动记录的一个例子: