我正在 xCode 7.1 中编写 UITests,并且在测试警报时遇到问题(在我的情况下允许通知)。
创建测试时,xCode 会写入以下代码:
app.alerts["\U201cAppName\U201d Would Like to Send You Notifications"].collectionViews.buttons["OK"].tap()
这会立即导致错误:
文字中的转义序列无效
所以我将 xCode 的代码替换为:
app.alerts["\u{201c}AppName\u{201d} Would Like to Send You Notifications"].collectionViews.buttons["OK"].tap()
但是当我运行 UITest 时它失败并显示消息:
UI 测试失败 - 未找到警报的匹配项
代码也一样
app.alerts["“AppName” Would Like to Send You Notifications"].collectionViews.buttons["OK"].tap()
我也尝试过
app.alerts.collectionViews.buttons["OK"].tap()
正如人们建议的那样here https://stackoverflow.com/questions/31746225/xcode7-xcode-ui-tests-how-to-handle-location-service-alert,但同样的故事...
相信很多人在 xCode 7.1 中进行 UITesting 时都遇到过这样的问题
请分享您的经验或任何解决建议。
提前致谢!
请参阅下面的示例
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] /cdn-cgi/l/email-protection")
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()
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)