UITesting, XCTest current ViewController Class

后端 未结 2 1093
半阙折子戏
半阙折子戏 2021-02-07 00:34

Simple problem. I got button which perform segue to next view controller. I want to write UI XCTest to tell me did it open view controller i wanted.

2条回答
  •  别那么骄傲
    2021-02-07 01:13

    The UI Testing framework doesn't have access to your applications code which makes class assertions on instances impossible. You are not able to directly tell the class of the controller which is on screen.

    However, if you think about your test a little differently you can make a very similar assertion. Write your tests as if you are the user. Your user doesn't care if he/she is looking at a ItemDetailViewController or a ItemListTableViewController so neither should your tests.

    The user cares what's on the screen. What's the title? Or, what are the names of these buttons? Following that logic you are rewrite your test to assert based on those items, not the name of the coded class.

    For example, if you are presenting your controller in a navigation stack you can assert the title.

    let app = XCUIApplication()
    app.buttons["View Item"].tap()
    
    XCTAssert(app.navigationBars["Some Item"].exists)
    

    Or, if the screen is presented modally but you know some static text or buttons, use those.

    let app = XCUIApplication()
    app.buttons["View Item"].tap()
    
    XCTAssert(app.staticTexts["Item Detail"].exists)
    XCTAssert(app.buttons["Remove Item"].exists)
    

提交回复
热议问题