Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Annotated iOS Feature Test
import XCTest
import Hamcrest
import PactConsumerSwift
class CandiesMasterDetailFeature: XCTestCase {
override func setUp() {
super.setUp()
continueAfterFailure = false //So the moment that an assertion in the test fails, we will not continue with the test
XCUIApplication().launch() //Launches the test in the iOS simulator. We can access the app with XCUIApplication()
}
func testMasterDetail() {
let expectation = expectationWithDescription("Pact tests complete") //Setting up our Pact Contract expectations
let candiesService = MockService( //This mock service will validate our contract expectations
candy: "Service API",
consumer: "Our App",
done: { result in
assertThat(result, equalTo(PactVerificationResult.Passed)) //Here is where the actual contract assertion takes place
expectation.fulfill() //And this is where Pact provides our predetermined responses if our request fits the contract
})
candiesService
.given("a list of candies")
.uponReceiving("a request for candies")
.withRequest( //asserts the details of the request that we are making to the API
method:.GET,
path: "/candies",
query: ["search-text": "fruit"])
.willRespondWith( //Here is the predetermined response that we ask Pact to give to our app
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 //This block runs our assertions inside of our app.
let app = XCUIApplication() //Here we will give a variable name to our app for assertions
let searchBar = app //Look for a search field on the app's visible view
.otherElements["CandiesSearchTextBox"] //with the accessibilityIdentifier "CandiesSearchTextBox"
.childrenMatchingType(.SearchField)
.element
searchBar.tap() //tap that search bar
searchBar.typeText("fruit")
app.keyboards.buttons["Search"].tap() //Press a button on the keyboard that displays the "Search" text
let orangeSlicesRow = app.tables.cells.elementBoundByIndex(2) //Get the third row in the table visible on screen
assertThat(orangeSlicesRow.staticTexts["Orange Slices"].exists, equalTo(true)) //make sure it has the text "Orange Slices" in it
orangeSlicesRow.tap() //Tap that row.
assertThat(app.staticTexts["Orange Slices"].exists, equalTo(true)) //Check for the name of the candy on the detail page
let candyNameDetailRow = app.tables.cells.elementBoundByIndex(0) //Find the first element in our table of Orange Slices data
assertThat(candyNameDetailRow.images["OrangeSlices"].exists, equalTo(true)) //Make sure the orange slice image is visible
app.swipeUp() //Swipe upward on the screen (to scroll down)
let priceRow = app.tables.cells.elementBoundByIndex(1) //Find the second row in the table, which contains price data
assertThat(priceRow.staticTexts["$6.75"].exists, equalTo(true)) //Check that the price text is in that row
let flavorsRow = app.tables.cells.elementBoundByIndex(2) //Find the third row in the table, which contains flavors
assertThat(flavorsRow.staticTexts["orange, lemon, lime, cherry"].exists, equalTo(true)) //Check that the flavor text is in that row
testComplete() //if all the previous assertions pass and we arrive at this line, end the test.
}
waitForExpectationsWithTimeout(20, handler: nil) //If testComplete() is not called after 20 seconds of running, fail the test.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment