1

I am writing my XCUITest's for my app. I am declaring the alert in order to use waitForExpectationsWithTimeout to make my test asynchronous....However it is throwing the error Variable used within its own initial value on the declaration of alert on line 5.

    let timeout = NSTimeInterval()
    let app = XCUIApplication()

    let exists = NSPredicate(format: "exists == 1")
    let alert = alert.buttons["OK"]


    testCase.addUIInterruptionMonitorWithDescription("Enable Notifications") { (alert) -> Bool in
            alert.buttons["OK"].tap()
        return true
    }

    self.buttons["Enable notifications"].tap()
    testCase.expectationForPredicate(exists, evaluatedWithObject: alert, handler: nil)
    testCase.waitForExpectationsWithTimeout(timeout, handler: nil)
    app.tap()

Can someone tell me why it is throwing this error and what I can do to fix this. Thanks in advance.

Billy Boyo
  • 819
  • 2
  • 12
  • 22

1 Answers1

5

It is because in your line no. 5, you have written

let alert = alert.buttons["OK"]

alert was never declared before this line, so you cannot write this.

For example, take this case,

let a = a+5

Now compiler will throw the same error as it does not know the value of 'a' as it was not declared before.

Aakash
  • 2,239
  • 15
  • 23
  • Okay, so how must I declare alert before? – Billy Boyo Dec 05 '16 at 12:50
  • I am not familiar with Xcode Tests, just answered for the your question. Found this on the internet after searching a bit - let alert = app.alerts.elementBoundByIndex(0).buttons["OK"] - try once – Aakash Dec 05 '16 at 13:40
  • If not helpful try this link http://stackoverflow.com/questions/32148965/xcode-7-ui-testing-how-to-dismiss-a-series-of-system-alerts-in-code or wait someone having knowledge on this to answer – Aakash Dec 05 '16 at 13:43