iOS UI Tests iMessage App/Extension
Asked Answered
D

2

9

I'm currently using Fastlane Snapshot to automate taking screenshots for my application. It's all based on UI Tests.

I'm trying to add this same functionality to an iMessage App/Extension.

So currently I have a test that goes through taps buttons, fills in text fields, takes the screenshots, etc.

After all that is done I'd like it to close the application (click the home button), open iMessage, interact with my iMessage application and take some screenshots there as well.

Is this possible? If so how can I achieve this? Automating screenshots for this one application has been amazing and I'd love to be able to do that for the iMessage App as well.

Dwelling answered 24/5, 2017 at 6:40 Comment(0)
C
1

There is no UI Tests for iMessage app extension in Xcode currently. But you can perform it by launching Messages by yourself and find elements in the Messages app. At first, you'll have to launch the Message app and open a conversation :

let messageApp = XCUIApplication(bundleIdentifier: "com.apple.MobileSMS")
messageApp.terminate()
messageApp.activate()
messageApp.cells.firstMatch.tap()

Then, you can access your iMessage app by doing so :

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

When your iMessage app is opened in the expanded mode, you can access the close button :

let closeButton = messageApp.buttons.element(boundBy: 1)

If you want to test your iMessage app when the user send a message and then open it, you can do it this way :

// 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)
}
Cora answered 21/8, 2019 at 10:47 Comment(0)
M
0

With Xcode 9 you can easily switch to the other applications like Messages. The following code switches to Messages, interacts with elements within the app and then switches back to your own app.

let messageApp = XCUIApplication(bundleIdentifier: "com.apple.MobileSMS")
messageApp.terminate()
messageApp.activate()

messageApp.cells.staticTexts["Kate Bell"].tap()

XCUIApplication().activate()
Murr answered 16/7, 2017 at 3:12 Comment(1)
No luck using this with fastlane at the moment. github.com/fastlane/fastlane/issues/9579Admonition

© 2022 - 2024 — McMap. All rights reserved.