Skip to content

Instantly share code, notes, and snippets.

@chelseatroy
Created September 8, 2016 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chelseatroy/d5b3f474b5790f74f0a49dc3b72c724c to your computer and use it in GitHub Desktop.
Save chelseatroy/d5b3f474b5790f74f0a49dc3b72c724c to your computer and use it in GitHub Desktop.
Example iOS Feature Test
import XCTest
import Hamcrest
import PactConsumerSwift
class CandiesMasterDetailFeature: XCTestCase {
override func setUp() {
super.setUp()
continueAfterFailure = false
XCUIApplication().launch()
}
func testMasterDetail() {
let expectation = expectationWithDescription("Pact tests complete")
let candiesService = MockService(
candy: "Service API",
consumer: "Our App",
done: { result in
assertThat(result, equalTo(PactVerificationResult.Passed))
expectation.fulfill()
})
candiesService
.given("a list of candies")
.uponReceiving("a request for candies")
.withRequest(
method:.GET,
path: "/candies",
query: ["search-text": "fruit"])
.willRespondWith(
status:200,
headers: ["Content-Type": "application/json"],
body: ["candies": [
[
"id": "1",
"name": "Lemon Drops",
],
[
"id": "2",
"name": "Chocolate Covered Candied Ginger",
],
[
"id": "3",
"name": "Orange Slices",
],
]])
candiesService
.given("Orange slices")
.uponReceiving("a request for orange slice information")
.withRequest(
method:.GET,
path: "/candies/3")
.willRespondWith(
status:200,
headers: ["Content-Type": "application/json"],
body: ["candy": [
"name": "Orange Slices",
"price": 6.75,
"flavors": [
"orange",
"lemon",
"lime",
"cherry",
]
]])
candiesService.run { (testComplete) -> Void in
let app = XCUIApplication()
let searchBar = app
.otherElements["CandiesSearchTextBox"]
.childrenMatchingType(.SearchField)
.element
searchBar.tap()
searchBar.typeText("fruit")
app.keyboards.buttons["Search"].tap()
let orangeSlicesRow = app.tables.cells.elementBoundByIndex(2)
assertThat(orangeSlicesRow.staticTexts["Orange Slices"].exists, equalTo(true))
orangeSlicesRow.tap()
assertThat(app.staticTexts["Orange Slices"].exists, equalTo(true))
let candyNameDetailRow = app.tables.cells.elementBoundByIndex(0)
assertThat(candyNameDetailRow.images["OrangeSlices"].exists, equalTo(true))
app.swipeUp()
let priceRow = app.tables.cells.elementBoundByIndex(1)
assertThat(priceRow.staticTexts["$6.75"].exists, equalTo(true))
let flavorsRow = app.tables.cells.elementBoundByIndex(2)
assertThat(flavorsRow.staticTexts["orange, lemon, lime, cherry"].exists, equalTo(true))
testComplete()
}
waitForExpectationsWithTimeout(20, handler: nil)
}
}
@thiensubs
Copy link

This is out-of-date.
'childrenMatchingType' has been renamed to 'children(matching:)'
'SearchField' has been renamed to 'searchField'

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