How to test if the app is presenting a certain view controller?

Viewed 496

I'm pretty new to XCode UI tests and I'm trying to run a test where I fill two text labels and then I press a button. After the button is pressed the app should make an URL call and be redirected to another view controller. I want to check if at the end of this operation the second view controller is displayed.

To test this, I have written the following test:

let app = XCUIApplication()
app.launch()
    
let ownerTextField = app.textFields["ownerTextField"]
ownerTextField.tap()
ownerTextField.typeText("UserA")
let repositoryTextField = app.textFields["repositoryTextField"]
repositoryTextField.tap()
repositoryTextField.typeText("AppB")
app.buttons["SearchButton"].tap()
XCTAssertTrue(app.isDisplayingResults)

Where isDisplayingResults is

extension XCUIApplication {
    var isDisplayingResults: Bool {
        return otherElements["resultView"].exists
    }
}

I have set up the identifier of the View controller inside its swift file class:

override func viewDidLoad() {
    super.viewDidLoad()
    view.accessibilityIdentifier = "resultView"
    ...

Nonetheless to say, the test fails. How can I get a success?

1 Answers

It's so simple. If after clicking the URL, Viewcontroller is presenting means your previous VC button doesn't exist on screen. So Just check for previous VC button exists or not.

  If app.buttons["SearchButton"].esists() 
 { //write if code
 } else {
// Write else code
        }
Related