 
 class FTRSwiftTests: XCTestCase {
 
+  var navigationController: UINavigationController? {
+    let rootVC = UIApplication.shared.delegate?.window??.rootViewController
+    return (rootVC as? UINavigationController) ?? (rootVC?.navigationController)
+  }
+
   override func tearDown() {
     super.tearDown()
-    let delegateWindow:UIWindow! = UIApplication.shared.delegate!.window!
-    var navController:UINavigationController?
-    if ((delegateWindow.rootViewController?.isKind(of: UINavigationController.self)) != nil) {
-      navController = delegateWindow.rootViewController as? UINavigationController
-    } else {
-      navController = delegateWindow.rootViewController!.navigationController
-    }
-    _ = navController?.popToRootViewController(animated: true)
+    _ = navigationController?.popToRootViewController(animated: true)
   }
 
   func testOpeningView() {
-    self.openTestView("Typing Views")
+    openTestView("Typing Views")
   }
 
   func testRotation() {
-    EarlGrey.rotateDeviceTo(orientation: UIDeviceOrientation.landscapeLeft, errorOrNil: nil)
-    EarlGrey.rotateDeviceTo(orientation: UIDeviceOrientation.portrait, errorOrNil: nil)
+    EarlGrey.rotateDeviceTo(orientation: .landscapeLeft, errorOrNil: nil)
+    EarlGrey.rotateDeviceTo(orientation: .portrait, errorOrNil: nil)
   }
 
   func testTyping() {
-    self.openTestView("Typing Views")
+    openTestView("Typing Views")
     let matcher = grey_accessibilityID("TypingTextField")
     let action = grey_typeText("Sample Swift Test")
     let assertionMatcher = grey_text("Sample Swift Test")

  }

  func testStepperActionWithCondition() {
    self.openTestView("Basic Views")
    var stepperValue = 51.0
    // Without the parameter using the value of the wait action, a warning should be seen.
    _ = GREYCondition.init(name: "conditionWithAction", block: {
      stepperValue += 1
      EarlGrey.select(elementWithMatcher: grey_kindOfClass(UIStepper.self))
        .perform(grey_setStepperValue(stepperValue))
      return stepperValue == 55
    }).waitWithTimeout(seconds: 10.0)
    EarlGrey.select(elementWithMatcher: grey_kindOfClass(UIStepper.self))
      .assert(with: grey_stepperValue(55))
  }

  func openTestView(_ name: String) {
    var errorOrNil : NSError?
    EarlGrey.select(elementWithMatcher: grey_accessibilityLabel(name))
      .perform(grey_tap(), error: &errorOrNil)
    if ((errorOrNil == nil)) {
      return
    }
    EarlGrey.select(elementWithMatcher: grey_kindOfClass(UITableView.self))


  func grey_firstElement() -> GREYMatcher {
    var firstMatch = true
    return GREYElementMatcherBlock(matchesBlock: { element in
      if firstMatch {
        firstMatch = false
        return true
      }
      return false
    }, descriptionBlock: { description in
      _ = description?.appendText("first match")
    })
  }
}
