UITesting of Alerts in xCode 7.1
Asked Answered
L

2

7

I'm writing UITests in xCode 7.1 and have a problem in testing alerts (Allow notifications in my case). While creating a test xCode writes this code:

app.alerts["\U201cAppName\U201d Would Like to Send You Notifications"].collectionViews.buttons["OK"].tap()

Which immediately causes error:

Invalid escape sequence in literal

So I replaced xCode's code with:

app.alerts["\u{201c}AppName\u{201d} Would Like to Send You Notifications"].collectionViews.buttons["OK"].tap()

But when I run UITest it fails with message:

UI Testing Failure - No matches found for Alert

The same for code

app.alerts["“AppName” Would Like to Send You Notifications"].collectionViews.buttons["OK"].tap()

I also tried

app.alerts.collectionViews.buttons["OK"].tap()

as people advised here, but same story...

I believe many people faced with such an issue during UITesting in xCode 7.1

Please, share your experience or any suggestions for solving. Thanks in advance!

Leopoldeen answered 3/11, 2015 at 16:50 Comment(1)
The "wrong" recording of the unicode-chars is subject of rdar://23493343. Feel free to duplicate this issue.Fifth
B
6

See example below

import XCTest

let systemAlertHandlerDescription = "systemAlertHandlerDescription"

class LoginPerformingTestCase: XCTestCase {

var systemAlertMonitorToken: NSObjectProtocol? = nil

override func setUp() {
    continueAfterFailure = false

    let app = XCUIApplication()
    app.launchArguments = [TestingEnvironment.resetLaunchArgument, TestingEnvironment.testingEnvironmentArgument]
    app.launch()

    systemAlertMonitorToken = addUIInterruptionMonitorWithDescription(systemAlertHandlerDescription) { (alert) -> Bool in
        if alert.buttons.matchingIdentifier("OK").count > 0 {
            alert.buttons["OK"].tap()
            return true
        } else {
            return false
        }
    }
}

override func tearDown() {
    if let systemAlertMonitorToken = self.systemAlertMonitorToken {
        removeUIInterruptionMonitor(systemAlertMonitorToken)
    }

    super.tearDown()
}

func loginWithApp(app: XCUIApplication) {
    let signInButton = app.buttons["SIGN IN"]
    signInButton.tap()
    let emailAdressTextField = app.textFields.matchingIdentifier("EmailAddress").elementBoundByIndex(0)
    emailAdressTextField.tap()
    emailAdressTextField.typeText("[email protected]")

    let passwordSecureTextField = app.secureTextFields["Password"]
    passwordSecureTextField.tap()
    passwordSecureTextField.typeText("1111")
    signInButton.tap()

    let exists = NSPredicate(format: "exists == 1")
    let iconAlarmButton = app.buttons["icon alarm"]

    let expectation = expectationForPredicate(exists, evaluatedWithObject: iconAlarmButton, handler: nil)
    waitForExpectationsWithTimeout(60) { (error) -> Void in
        if let _ = error {
            expectation.fulfill()
        }
    }

    app.tap()//workaround to hide system alert
    let darkNavigaitonBar = app.otherElements.matchingIdentifier("darkNavigationView").elementBoundByIndex(0)
    if darkNavigaitonBar.hittable == true {
        app.tap()
    }

}

}
Bailey answered 3/11, 2015 at 17:24 Comment(1)
Thanks for your answer. It helped, nice approach! What is TestingEnvironment in app.launchArguments[ ]? Xcode doesn't recognize it (error: Use of unresolved identifier 'TestingEnvironment').Leopoldeen
W
2

Here is an example how to do it with an app requesting local notification permission access:

addUIInterruptionMonitorWithDescription("Local Dialog") { (alert) -> Bool in
     if alert.collectionViews.buttons["OK"].exists {
          alert.collectionViews.buttons["OK"].tap()
          return true
     }
     return false
}
Wommera answered 28/6, 2016 at 20:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.