UITesting of Alerts in xCode 7.1

与世无争的帅哥 提交于 2019-12-03 07:13:42

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("trevistest@test.test")

    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()
    }

}

}

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!