是否有可能使用EarlGrey(iOS UItesting)解除系统警报?

我现在已经开始尝试使用XCUITest几个月的UItesting了一下EarlGrey。 我遇到了无法解除系统警报的经典问题,这很奇怪,因为Google似乎为系统警报(称为grey_systemAlertViewShown())实施了匹配器。 我正在尝试使用GREYCondition检测系统警报。 以下是我所尝试的:

- (void)waitForAndDismissSystemAlertForSeconds:(NSInteger)seconds { GREYCondition *interactableCondition = [GREYCondition conditionWithName:@"isInteractable" block:^BOOL{ // Fails if element is not interactable NSError *error; [[EarlGrey selectElementWithMatcher:grey_systemAlertViewShown()] assertWithMatcher:grey_interactable() error:&error]; if (error) { return NO; } else { NSError *allowButtonError; [[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(@"Allow")] assertWithMatcher:grey_notNil() error:&allowButtonError]; if (!allowButtonError) { [[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(@"Allow")] performAction:grey_tap()]; } return YES; }]; [interactableCondition waitWithTimeout:seconds]; } 

我也尝试使用addUIInterruptionMonitorWithDescription(如下所示)(但使用EarlGrey代码基本上是在中断监视器上做的事情): Xcode 7 UItesting:如何解除代码中的一系列系统警报

这两种方法都不行。 在我的GREYCondition中,断点不会触发非错误情况,中断监视器也不会解除我的警报。

有谁知道EarlGrey是否支持解除系统警报?

正如文档grey_systemAlertViewShown 所示 ,grey_systemAlertViewShown仅检查是否显示系统警报视图。 更好地使用API​​将是声明不显示系统警报(可能是因为testing应用程序已经嘲笑导致系统警报的代码)。

 Code that taps a button that requests causes system alert to be shown (for ex: requests user's geo location) comes here... // Assert that in the test app system alert view is not shown because we have mocked out the part of code that requests user location. [[EarlGrey selectElementWithMatcher:grey_anything()] assertWithMatcher:grey_not(grey_systemAlertViewShown())]; 

在这个书写系统中,警报视图不能被EarlGrey解雇。 由应用程序启动的Alertviews可以被驳回。 常见问题有一个问题,表明如果存在modal dialog,EarlGreytesting将失败。

我们发现解决这个问题的最好方法是包含一个testing的启动参数,其中我们不会注册应用程序的通知。

就像是:

if [[[NSProcessInfo processInfo] arguments] containsObject:argument] { return; }

在你打电话之前

[[UIApplication sharedApplication] registerUserNotificationSettings:settings]; [[UIApplication sharedApplication] registerForRemoteNotifications];

这样,“您要允许推送通知…”警报将不会显示。