Skip to content

Instantly share code, notes, and snippets.

@pedantix
Created October 28, 2017 20:11
Show Gist options
  • Save pedantix/ed69524fb57bd29607981063a2fc383a to your computer and use it in GitHub Desktop.
Save pedantix/ed69524fb57bd29607981063a2fc383a to your computer and use it in GitHub Desktop.
XCUIApplication testing with Ambasador in Swift 4

README!

This should provide the barebones of what is needed to use xcode 9 to get an ambassador tested app up and running.

There is an assumption that carthage is available for your project.

Standard Disclamir

This worked today, hope it works tommorow, there is probably a better way. Hope this helps you, and the author of course is not responsible for anything.

//
// BasicUsageExampleTests.swift
// ExampleUITests
//
// Created by Shaun Hubbard on 10/28/17.
// Copyright © 2017 Shaun Codes. All rights reserved.
//
import XCTest
import Ambassador
class BasicUsageExampleTests: UITestBase {
override func setUp() {
super.setUp()
app.launch()
}
func testMyRequest() {
var bar = "bar"
router["/api/thing"] = DelayResponse(JSONResponse(statusCode: 200, handler: { _ -> Any in
return ["foo" : bar]
}))
let elementsQuery = app.scrollViews.otherElements
let textLabel = elementsQuery.staticTexts[bar]
XCTAssert(bar.exists)
}
}
github "pedantix/Ambassador" "swift4-carthage"
//
// UITestBase.swift
// ExampleUITests
//
// Created by Shaun Hubbard on 10/28/17.
// Copyright © 2017 Shaun Codes. All rights reserved.
//
import Foundation
import XCTest
import Embassy
import Ambassador
class UITestBase: XCTestCase {
let port = 8080
var router: Router!
var eventLoop: EventLoop!
var server: HTTPServer!
var app: XCUIApplication!
var eventLoopThreadCondition: NSCondition!
var eventLoopThread: Thread!
override func setUp() {
super.setUp()
continueAfterFailure = false
setupWebApp()
setupApp()
}
// setup the Embassy web server for testing
private func setupWebApp() {
guard let kQueueSelector = try? KqueueSelector() else { fatalError() }
eventLoop = try? SelectorEventLoop(selector: kQueueSelector)
router = Router()
server = DefaultHTTPServer(eventLoop: eventLoop, port: port, app: router.app)
// Start HTTP server to listen on the port
do {
try server.start()
} catch let err {
fatalError("server failed to start \(err)")
}
eventLoopThreadCondition = NSCondition()
eventLoopThread = Thread(target: self, selector: #selector(runEventLoop), object: nil)
eventLoopThread.start()
}
// set up XCUIApplication
private func setupApp() {
app = XCUIApplication()
app.launchEnvironment["RESET_LOGIN"] = "1"
app.launchEnvironment["ENVOY_BASEURL"] = "http://localhost:\(port)"
}
override func tearDown() {
super.tearDown()
app.terminate()
server.stopAndWait()
eventLoopThreadCondition.lock()
eventLoop.stop()
while eventLoop.running {
if !eventLoopThreadCondition.wait(until: Date(timeIntervalSinceNow: 1)) {
fatalError("Join eventLoopThread timeout")
}
}
}
@objc private func runEventLoop() {
eventLoop.runForever()
eventLoopThreadCondition.lock()
eventLoopThreadCondition.signal()
eventLoopThreadCondition.unlock()
}
func notYetImplemented() {
XCTAssert(false, "This case has not been implemented")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment