Skip to content

Instantly share code, notes, and snippets.

@levantAJ
Created September 21, 2019 09:44
Show Gist options
  • Save levantAJ/b24c96846ed92ecdc4f5160b0e843966 to your computer and use it in GitHub Desktop.
Save levantAJ/b24c96846ed92ecdc4f5160b0e843966 to your computer and use it in GitHub Desktop.
UITest open deep link URLs
//
// XCTestCase+Extension.swift
// UITests
//
// Created by levantAJ on 3/16/19.
// Copyright © 2019 levantAJ. All rights reserved.
//
import XCTest
extension XCTestCase {
func openDeepLink(urlString: String, file: StaticString = #file, line: UInt = #line, assertion: (XCUIApplication) -> Void) {
let contactsApp = XCUIApplication(bundleIdentifier: "com.apple.MobileAddressBook")
contactsApp.launch()
let kateBellCell = contactsApp.cells.staticTexts["Kate Bell"]
if kateBellCell.waitForExistence(timeout: ShortTimeout) {
kateBellCell.tap()
}
let urlCell = contactsApp.tables.cells.element(contains: urlString)
if urlCell.waitForExistence(timeout: ShortTimeout) {
urlCell.tap()
} else {
contactsApp.buttonTap(name: "Edit")
let editTableView = contactsApp.tables.firstMatch
expectForExistance(editTableView, "No 'Edit' table view found!", timeout: ShortTimeout)
editTableView.swipeUp()
let addURLCell = editTableView.cells.element(contains: "add url")
expectForExistance(addURLCell, "No 'add url' cell found!", timeout: ShortTimeout)
addURLCell.tap()
let urlTextField = editTableView.cells.textFields["URL"]
expectForExistance(urlTextField, "No 'URL' text field found!", timeout: ShortTimeout)
urlTextField.tap()
urlTextField.typeText(urlString)
editTableView.swipeDown()
contactsApp.buttonTap(name: "Done")
let urlCell = contactsApp.tables.cells.element(contains: urlString)
expectForExistance(urlCell, "No '\(urlString)' cell found!", timeout: ShortTimeout)
urlCell.tap()
}
let alertHandler = addUIInterruptionMonitor(withDescription: "\"Contacts\" wants to open \"YourApp\"") { alert -> Bool in
alert.buttons["Open"].tap()
return true
}
XCUIApplication().tap()
sleep(3)
assertion(XCUIApplication())
contactsApp.activate()
contactsApp.buttonTap(name: "Edit")
let editTableView = contactsApp.tables.firstMatch
expectForExistance(editTableView, "No 'Edit' table view found!", timeout: ShortTimeout)
editTableView.swipeUp()
let urlTextField = editTableView.cells.textFields[urlString].firstMatch
expectForExistance(urlTextField, "No '\(urlString)' text field found!", timeout: ShortTimeout)
urlTextField.tap()
urlTextField.buttons["Clear text"].tap()
contactsApp.buttonTap(name: "Done")
contactsApp.terminate()
removeUIInterruptionMonitor(alertHandler)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment