     }
 
     func testTabTrayDisposed() {
-        let browserViewController = UIApplication.sharedApplication().keyWindow!.rootViewController!
-
         // Enter the tab tray.
         tester().tapViewWithAccessibilityLabel("Show Tabs")
         tester().waitForViewWithAccessibilityLabel("Tabs Tray")
-        weak var tabTrayController = browserViewController.presentedViewController
+        weak var tabTrayController = getTopViewController()
         weak var tabCell = tester().waitForTappableViewWithAccessibilityLabel("home")
         XCTAssertNotNil(tabTrayController, "Got tab tray reference")
         XCTAssertNotNil(tabCell, "Got tab cell reference")

        XCTAssertNil(webView, "webView disposed")
    }


    private func getChildViewController(parent: UIViewController, childClass: String) -> UIViewController {
        let childControllers = parent.childViewControllers.filter { child in

        // Type the URL to make the search controller appear.
        tester().tapViewWithAccessibilityIdentifier("url")
        tester().clearTextFromAndThenEnterTextIntoCurrentFirstResponder("foobar")
        tester().waitForTimeInterval(0.1) // Wait to make sure that input has propagated through delegate methods
        let browserViewController = getTopViewController()
        weak var searchViewController = getChildViewController(browserViewController, childClass: "SearchViewController")
        XCTAssertNotNil(searchViewController, "Got search controller reference")

