【问题标题】:UITesting of Alerts in xCode 7.1xCode 7.1 中警报的 UITesting
【发布时间】:2016-02-03 22:30:23
【问题描述】:

我在 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,但同样的故事......

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

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

【问题讨论】:

  • unicode-chars 的“错误”记录是 rdar://23493343 的主题。随意复制此问题。

标签: ios xcode swift unit-testing alerts


【解决方案1】:

这是一个示例,如何使用请求本地通知权限访问的应用来执行此操作:

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

【讨论】:

    【解决方案2】:

    见下例

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

    【讨论】:

    • 感谢您的回答。它有帮助,很好的方法! app.launchArguments[ ] 中的 TestingEnvironment 是什么? Xcode 无法识别(错误:使用未解析的标识符“TestingEnvironment”)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多