在xCode 7.1中使用警报的UITesting [英] UITesting of Alerts in xCode 7.1
问题描述
我正在xCode 7.1中编写UITests并且在测试警报时遇到问题(在我的情况下允许通知)。
创建测试时,xCode会写入以下代码:
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()
立即导致错误:
文字中无效的转义序列
Invalid escape sequence in literal
所以我用以下代码替换了xCode的代码:
So I replaced xCode's code with:
app.alerts["\u{201c}AppName\u{201d} Would Like to Send You Notifications"].collectionViews.buttons["OK"].tap()
<但是,当我运行UITest时,它失败并显示消息:
But when I run UITest it fails with message:
UI测试失败 - 未找到警报匹配
UI Testing Failure - No matches found for Alert
代码相同
app.alerts[""AppName" Would Like to Send You Notifications"].collectionViews.buttons["OK"].tap()
我也试过了
app.alerts.collectionViews.buttons["OK"].tap()
正如人们所建议的那样这里,但同样的故事......
as people advised here, but same story...
我相信很多人在xCode 7.1中的UITesting期间遇到过这样的问题
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!
推荐答案
参见下面的示例
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()
}
}
}
这篇关于在xCode 7.1中使用警报的UITesting的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!