Last active
September 8, 2016 22:34
-
-
Save chelseatroy/25e5be13f612c3778cb369e86d1c6daf to your computer and use it in GitHub Desktop.
Annotated iOS Feature Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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