我的应用程序显示启动画面。如何让我的测试等待主屏幕?

My app shows a splash screen. How can I make my test wait for the main screen?

我的应用显示启动画面。如何让我的测试等待主屏幕出现?没有等待,我的测试在应用程序启动后立即失败。

// in application:didFinishLaunchingWithOptions: of my AppDelegate...
SplashViewController *splashVC = [[SplashViewController alloc] init];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = splashVC;
[self.window makeKeyAndVisible];

NSTimeInterval splashScreenDuration = 0.5;
[NSTimer scheduledTimerWithTimeInterval:splashScreenDuration
                               target:self
                             selector:@selector(hideSpashScreenAndDisplayMainViewController)
                             userInfo:nil
                              repeats:NO];

// hideSpashScreenAndDisplayMainViewController method simply sets self.window.rootViewController to the main view controller.

EarlGrey 提供 GREYCondition API that you can use in your setup method or the beginning of your specific test to make EarlGrey wait while you wait for the splash screen to disappear. You can do this using code similar to what they have in their faq page.

// Wait for the main view controller to become the root view controller.
  BOOL success = [[GREYCondition conditionWithName:@"Wait for main root view controller"
                                             block:^{
    id<UIApplicationDelegate> appDelegate = [UIApplication sharedApplication].delegate;
    UIViewController *rootViewController = appDelegate.window.rootViewController;
    return [rootViewController isKindOfClass:[MainViewController class]];
  }] waitWithTimeout:5];

  GREYAssertTrue(success, @"Main view controller should appear within 5 seconds.");