Skip to content

Instantly share code, notes, and snippets.

@cornr
Last active December 25, 2023 04:01
Show Gist options
  • Save cornr/46e14d1e8b160f981dc0fae53ba68b09 to your computer and use it in GitHub Desktop.
Save cornr/46e14d1e8b160f981dc0fae53ba68b09 to your computer and use it in GitHub Desktop.
UI testing openURL and universal links via iMessage
import Foundation
import XCTest
//This is based on https://blog.branch.io/ui-testing-universal-links-in-xcode-9/
final class iMessage {
static func launch() -> XCUIApplication {
// Open iMessage App
let messageApp = XCUIApplication(bundleIdentifier: "com.apple.MobileSMS")
// Launch iMessage app
messageApp.launch()
// Wait some seconds for launch
XCTAssertTrue(messageApp.waitForExistence(timeout: 10))
// Continues "Whats new" if present
let continueButton = messageApp.buttons["Continue"]
if (continueButton.exists) {
continueButton.tap()
}
// Removes New Messages Sheet on iOS 13
let cancelButton = messageApp.navigationBars.buttons["Cancel"]
if cancelButton.exists {
cancelButton.tap()
}
// Return application handle
return messageApp
}
static func open(URLString urlString: String, inMessageApp app: XCUIApplication) {
XCTContext.runActivity(named: "Open URL \(urlString) in iMessage") { _ in
// Find Simulator Message
let kateBell = app.cells.staticTexts["Kate Bell"]
XCTAssertTrue(kateBell.waitForExistence(timeout: 10))
kateBell.tap()
// Tap message field
app.textFields["iMessage"].tap()
// Continues "Swipe to Text" Sheet
let continueButton = app.buttons["Continue"]
if continueButton.exists {
continueButton.tap()
}
// Enter the URL string
app.typeText("Open Link:\n")
app.typeText(urlString)
// Simulate sending link
app.buttons["sendButton"].tap()
//Wait for Main App to finish launching
sleep(2)
// The first link on the page
let messageBubble = app.cells.links["com.apple.messages.URLBalloonProvider"]
XCTAssertTrue(messageBubble.waitForExistence(timeout: 10))
messageBubble.tap()
}
}
static func openFromPasteboard(inMessageApp app: XCUIApplication) {
XCTContext.runActivity(named: "Open URL from Pasetboard in iMessage") { _ in
// Find Simulator Message
let kateBell = app.cells.staticTexts["Kate Bell"]
XCTAssertTrue(kateBell.waitForExistence(timeout: 10))
kateBell.tap()
// Add dummy text to workaround issue with link-only bubbles in iOS 12.4+
app.textFields["iMessage"].tap()
app.typeText("Open Link:\n")
// Tap message field to open paste menu
app.textFields["iMessage"].tap()
sleep(1)
app.menuItems["Paste"].tap()
// Simulate sending link
sleep(1)
app.buttons["sendButton"].tap()
//Wait for Main App to finish launching
sleep(3)
// The first link on the page
let messageBubble = app.cells.links["com.apple.messages.URLBalloonProvider"]
XCTAssertTrue(messageBubble.waitForExistence(timeout: 10))
messageBubble.tap()
}
}
}
import XCTest
class UniversialLinktTest: XCTestCase {
var app: XCUIApplication!
override func setUp() {
super.setUp()
continueAfterFailure = false
app = XCUIApplication()
app.launch()
}
func testItDeepLinksIntoApp() {
// Open App via iMessage
let messageApp = iMessage.launch()
iMessage.open(URLString: "https://mydomain.com/verify/12345", inMessageApp: messageApp)
// Do test here...
let isInVerifyScreen = app.staticTexts["12345"].waitForExistence(timeout: 10)
XCTAssertTrue(isInVerifyScreen)
// For reliability terminate iMessage after test
messageApp.terminate()
}
}
@standinga
Copy link

Yeah, I saw this thread just after I posted here. Seems we need to wait some more for a non-workaround (native) solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment