UITesting of Alerts in xCode 7.1

后端 未结 2 2117
孤街浪徒
孤街浪徒 2021-02-09 05:04

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         


        
2条回答
  •  爱一瞬间的悲伤
    2021-02-09 05:57

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

提交回复
热议问题