Browse Source

Few test changes and more.

pull/92/head
Abhishek 5 years ago
parent
commit
34d4daf2db
  1. 19
      Clocker/ClockerUITests/NetworkDisconnectionTests.swift
  2. 107
      Clocker/ClockerUITests/PreferencesTest.swift
  3. 6
      Clocker/Overall App/NetworkManager.swift

19
Clocker/ClockerUITests/NetworkDisconnectionTests.swift

@ -20,25 +20,6 @@ class NetworkDisconnectionTests: XCTestCase {
}
}
// User should be still be able to add a timezone
func testAddingATimezone() {
app.launchArguments.append("mockNetworkDown")
precondition()
app.buttons["FloatingPreferences"].click()
if app.sheets.count == 0 {
app.windows["Clocker"].checkBoxes["AddTimezone"].click()
}
XCTAssertFalse(app.sheets.staticTexts["ErrorPlaceholder"].exists)
let searchField = app.searchFields["AvailableSearchField"]
searchField.reset(text: "Kolkata")
addAPlace(place: "Kolkata", to: app)
app.sheets.buttons["Close"].click()
}
func testAddingACity() {
app.launchArguments.append("mockNetworkDown")
precondition()

107
Clocker/ClockerUITests/PreferencesTest.swift

@ -45,42 +45,13 @@ class PreferencesTest: XCTestCase {
addAPlace(place: "UTC", to: app)
let matchPredicate = NSPredicate(format: "value == %@", "UTC")
let matchPredicate = NSPredicate(format: "value contains %@", "UTC")
let matchingFields = app.tables["TimezoneTableView"].textFields.matching(matchPredicate)
XCTAssertTrue(matchingFields.count > 0, "Matching Fields count was zero")
deleteAPlace(place: "UTC", for: app)
}
func testEditingLabel() {
let placeToAdd = "Auckland"
app.tapMenubarIcon()
app.tables["mainTableView"].typeKey(",", modifierFlags: .command)
if app.sheets.count == 0 {
app.windows["Clocker"].checkBoxes["AddTimezone"].click()
}
addAPlace(place: placeToAdd, to: app)
let matchPredicate = NSPredicate(format: "value == %@", placeToAdd)
let matchingFields = app.windows["Clocker"].textFields.matching(matchPredicate)
XCTAssertTrue(matchingFields.count > 1, "Matching Fields count was zero")
matchingFields.element(boundBy: 1).doubleClick()
matchingFields.element(boundBy: 1).typeText("NZ")
app.typeKey(XCUIKeyboardKey.return, modifierFlags: [])
app.tapMenubarIcon()
let labelPredicate = NSPredicate(format: "label == %@", "NZ")
let cells = app.tables["mainTableView"].cells.matching(labelPredicate)
XCTAssert(cells.count > 0)
app.tables["mainTableView"].typeKey(",", modifierFlags: .command)
deleteAPlace(place: placeToAdd, for: app)
}
func testSortingByTimezoneDifference() {
app.tapMenubarIcon()
app.tables["mainTableView"].typeKey(",", modifierFlags: .command)
@ -341,3 +312,79 @@ extension XCUIApplication {
statusItems.firstMatch.click()
}
}
extension XCTestCase {
func inverseWaiterFor(element: XCUIElement, time: TimeInterval = 25) {
let spinnerPredicate = NSPredicate(format: "exists == false")
let spinnerExpectation = expectation(for: spinnerPredicate, evaluatedWith: element, handler: nil)
let spinnerResult = XCTWaiter().wait(for: [spinnerExpectation], timeout: time)
if spinnerResult != .completed {
XCTFail("Still seeing Spinner after 25 seconds. Something's wrong")
}
}
func addAPlace(place: String, to app: XCUIApplication, shouldSleep: Bool = true) {
// Let's first check if the place is already present in the list
let matchPredicate = NSPredicate(format: "value contains %@", place)
let matchingFields = app.windows["Clocker"].tables["TimezoneTableView"].textFields.matching(matchPredicate)
if matchingFields.count > 0 {
return
}
if app.sheets.count == 0 {
app.windows["Clocker"].checkBoxes["AddTimezone"].click()
}
let searchField = app.searchFields["AvailableSearchField"]
searchField.reset(text: place)
let results = app.tables["AvailableTimezoneTableView"].cells.staticTexts.matching(matchPredicate)
let waiter = XCTWaiter()
let isHittable = NSPredicate(format: "exists == true", "")
let addExpectation = expectation(for: isHittable,
evaluatedWith: results.firstMatch) { () -> Bool in
print("Handler called")
return true
}
waiter.wait(for: [addExpectation], timeout: 5)
if results.count > 0 {
results.firstMatch.click()
}
app.buttons["AddAvailableTimezone"].click()
if shouldSleep {
sleep(2)
}
}
func deleteAllPlaces(app: XCUIApplication) {
var rowQueryCount = app.windows["Clocker"].tables["TimezoneTableView"].tableRows.count
if rowQueryCount == 0 {
return
}
let currentElement = app.windows["Clocker"].tableRows.firstMatch
currentElement.click()
while rowQueryCount > 0 {
app.windows["Clocker"].typeKey(XCUIKeyboardKey.delete, modifierFlags: XCUIElement.KeyModifierFlags())
rowQueryCount -= 1
}
}
func deleteAPlace(place: String, for app: XCUIApplication, shouldSleep: Bool = true) {
let matchPredicate = NSPredicate(format: "value contains %@", place)
let row = app.tables["TimezoneTableView"].textFields.matching(matchPredicate).firstMatch
row.click()
row.typeKey(XCUIKeyboardKey.delete, modifierFlags: XCUIElement.KeyModifierFlags())
if shouldSleep {
sleep(2)
}
}
}

6
Clocker/Overall App/NetworkManager.swift

@ -52,6 +52,12 @@ extension NetworkManager {
let dataTask = session.dataTask(with: request) { data, urlResponse, error in
// Check if we're running a network UI test
if ProcessInfo.processInfo.arguments.contains("mockTimezoneDown") {
completionHandler(nil, internalServerError)
return
}
guard error == nil, let httpURLResponse = urlResponse as? HTTPURLResponse, let json = data else {
completionHandler(nil, internalServerError)
return

Loading…
Cancel
Save