是否可以使用EarlGrey(iOS UI测试)解除系统警报?

Jon*_*hen 4 ui-testing ios earlgrey

我开始尝试使用EarlGrey一点点,现在已经使用XCUITest进行了UI测试.我遇到了无法解除系统警报的经典问题,这很奇怪,因为看起来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];
}
Run Code Online (Sandbox Code Playgroud)

我也尝试过使用这里描述的addUIInterruptionMonitorWithDescription(但是使用EarlGrey代码来完成我在上面的中断监视器中所做的事情):Xcode 7 UI测试:如何在代码中解除一系列系统警报

两种方法都不奏效.我的GREYCondition中的非错误情况不会触发断点,并且中断监视器也不会解除我的警报.

有人知道EarlGrey是否支持解雇系统警报?

小智 5

正如grey_systemAlertViewShown的文档所示,grey_systemAlertViewShown仅检查是否显示系统警报视图.更好地使用API​​将断言未显示系统警报(可能是因为测试应用程序模拟了导致系统警报的代码).

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())];
Run Code Online (Sandbox Code Playgroud)

在撰写此系统时,EarlGrey不会忽视警报视图.应用程序启动的警报视图可以被取消.该FAQ有一个问题,表示如果模态对话框存在EarlGrey测试将失败.