Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS UI Tests iMessage App/Extension

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.

like image 564
Charlie Fish Avatar asked May 24 '17 06:05

Charlie Fish


People also ask

How do I test my iOS UI?

Recording a UI TestFrom the debug bar, click the Record UI Test button. Xcode will launch the app and run it. You can interact with the element on-screen and perform a sequence of interactions for any UI test. Whenever you interact with an element, Xcode writes the corresponding code for it into your method.

What is Messages extension on iPhone?

An iMessage extension can include the same features as a standalone iMessage app, and allow users to access features of your iOS app within Messages. Your iMessage app will be listed on the App Store for iMessage in the same category and with the same description as your iOS app on the App Store.

How do you write test cases for iOS apps?

To create new unit case in iOS, go to File -> New -> File, and then select Unit Test Case Class. Doing so creates a template just like the one you got with your project.


1 Answers

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)
}
like image 55
Benjamin Pisano Avatar answered Sep 23 '22 00:09

Benjamin Pisano