在xCode 7.1中使用警报

我在xCode 7.1中编写UITests,在testing警报时遇到问题(在我的情况下允许通知)。 当创build一个testingxCode写这个代码:

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

立即导致错误:

文字中的转义序列无效

所以我用下面的代码replace了xCode的代码:

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

但是,当我运行UITest失败,消息:

UItesting失败 – 找不到警报的匹配项

代码也一样

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

我也试过了

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

正如人们在这里所build议的,但同样的故事

我相信很多人在xCode 7.1的UITesting期间遇到这样的问题

请分享您的经验或任何解决build议。 提前致谢!

看下面的例子

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

以下是一个示例,说明如何使用请求本地通知权限的应用程序执行此操作:

 addUIInterruptionMonitorWithDescription("Local Dialog") { (alert) -> Bool in if alert.collectionViews.buttons["OK"].exists { alert.collectionViews.buttons["OK"].tap() return true } return false }