Xcode 目前没有针对 iMessage 应用扩展的 UI 测试。但是您可以通过自己启动 Messages 并在 Messages 应用程序中查找元素来执行它。首先,您必须启动 Message 应用并打开对话:
let messageApp = XCUIApplication(bundleIdentifier: "com.apple.MobileSMS")
messageApp.terminate()
messageApp.activate()
messageApp.cells.firstMatch.tap()
然后,您可以通过以下方式访问您的 iMessage 应用程序:
// Replace appIndex by the position of your app in the iMessage bottom bar
let appIndex = 2
messageApp.collectionViews.descendants(matching: .cell).element(boundBy: appIndex).tap()
当您的 iMessage 应用程序以展开模式打开时,您可以访问关闭按钮:
let closeButton = messageApp.buttons.element(boundBy: 1)
如果您想在用户发送消息然后打开它时测试您的 iMessage 应用程序,您可以这样做:
// Send your message after it is inserted in the Messages app text field
let sendButton = messageApp.buttons["sendButton"]
waitForElementToExists(sendButton)
sendButton.tap()
// Tap on the iMessage first bubble
let firstBubble = messageApp.collectionViews["TranscriptCollectionView"].cells.element(boundBy: 2)
waitForElementToExists(firstBubble)
firstBubble.tap()
private func waitForElementToExists(_ element: XCUIElement) {
let exists = NSPredicate(format: "exists == 1")
expectation(for: exists, evaluatedWith: element, handler: nil)
waitForExpectations(timeout: 5, handler: nil)
}