ios - xCode 7.1 中警报的 UITesting

标签 ios xcode swift unit-testing alerts

我正在 xCode 7.1 中编写 UITest,但在测试警报时遇到问题(在我的案例中允许通知)。 在创建测试时,xCode 编写了这段代码:

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

这会立即导致错误:

Invalid escape sequence in literal

所以我将 xCode 的代码替换为:

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

但是当我运行 UITest 时它失败并显示消息:

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

正如人们所建议的那样here , 但同样的故事...

我相信很多人在 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("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()
    }

}

}

关于ios - xCode 7.1 中警报的 UITesting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33504748/

相关文章:

ios - 仅将 4 行代码从 objective-c 转换为 swift(指针)

ios - 如何使用 Google Maps SDK (Swift/iOS) 在 map 标记周围绘制半径?

iOS Swift PerformSegueWithIdentifier 用于发送数据

ios - 在 UIWebView 中使内容适合宽度

ios - 从 iTunes URL 字符串获取应用程序的 ID 号

ios - 如何解决在 Xcode 中使用 Google Maps SDK for iOS 时出现的错误 "Thread 1: SIGABRT error"

ios - React-Native ios App 崩溃无报告

ios - 导航回到另一个 View Controller 而不是它出现的地方

ios - AFNetworking 2.0 - 如何在成功时将响应传递给另一个类

swift - 我无法让我的代码使用过时的 Swift Core Data 教程