1

I am using swift for an Xcode UI Test app. Our application under test sometimes pops up an "Alert Box" that affects normal work flow of test case. There is no way of predicting when the popup would appear. It might appear at test case 1 or test case number x.

I wanted to dismiss the "Alert Box" and continue with rest of the test case. How do I handle similar event of ASYNC nature with swift XCUITest framework, without affecting normal flow of test case?

So far I have found:

expectationForPredicate(exists, evaluatedWithObject: alertbox, handler: nil)
waitForExpectationsWithTimeout(300, handler: nil)

This is not feasible because of two reasons.

  1. timeout cannot be predicted
  2. Is blocking test case flow

    func testTestCase1 {
        let expectation = expectationWithDescription("Alert Found! Dismissing")
        do {
            // ...
            // test steps
            // ...
            expectation.fulfill()
        }
        waitForExpectationsWithTimeout(300) {
            dimissAlert()
        }
    }
    

Ref1: https://www.bignerdranch.com/blog/asynchronous-testing-with-xcode-6/
Ref2: XCTest and asynchronous testing in Xcode 6
Ref3: https://adoptioncurve.net/archives/2015/10/testing-asynchronous-code-in-swift/

Is there a generic way to handle async events across the suite? How do I continue with testing while another thread waits for "Alert Box" appear event?

Cheers!

Community
  • 1
  • 1
nixtalker
  • 48
  • 5

2 Answers2

3

Stephen is right, the UI interruption monitor should work nicely. I recommend adding it to your test set up so it runs for all of your tests.

class UITests: XCTestCase {
    let app = XCUIApplication()

    override func setUp() {
        super.setUp()
        addUIInterruptionMonitorWithDescription("Alert") { (alert) -> Bool in
            alert.buttons["OK"].tap()
            return true
        }
        app.launch()
    }

    func testFoo() {
        // example test
    }
}
Joe Masilotti
  • 16,815
  • 6
  • 77
  • 87
1

Xcode 7.1 has added addUIInterruptionMonitorWithDescription which seems to be what you are looking for. Documentation here: https://developer.apple.com/reference/xctest/xctestcase/1496273-adduiinterruptionmonitorwithdesc?language=objc

Stephen
  • 1,427
  • 1
  • 17
  • 42