手动在单元测试中向左滑动以调用处理程序以在Swift中进行测试

问题描述 投票:1回答:1

我是单元测试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
    }
ios swift uitableview handler
1个回答
1
投票

你可以像这样使用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记录它们。更多详情:

https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/testing_with_xcode/chapters/09-ui_testing.html

这是我用滑动记录的一个例子:

https://youtu.be/lHafMlIcoCY

© www.soinside.com 2019 - 2024. All rights reserved.