【问题标题】:Is it possible to dismiss System Alerts using EarlGrey (iOS UI Testing)?是否可以使用 EarlGrey(iOS UI 测试)关闭系统警报?
【发布时间】:2016-06-06 00:04:04
【问题描述】:

我开始对 EarlGrey 进行一些试验,现在已经使用 XCUITest 进行了几个月的 UI 测试。我遇到了无法消除系统警报的经典问题,这很奇怪,因为看起来 Google 为系统警报实现了一个匹配器,称为 gray_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 UI Testing: how to dismiss a series of system alerts in code

这两种方法都不起作用。 GREYCondition 中的非错误情况不会触发断点,中断监视器也不会关闭我的警报。

有人知道 EarlGrey 是否支持关闭系统警报吗?

【问题讨论】:

    标签: ios ui-testing earlgrey


    【解决方案1】:

    可以使用 AppleSimulatorUtils 实用程序授予所有必需的权限。

    这种方法无需关闭警报并节省时间。

    通过在终端应用程序中输入下一个命令来安装实用程序

    brew tap wix/brew

    brew install applesimutils

    并授予权限

    applesimutils --byId <simulator UDID> --bundle <bundle identifier> --setPermissions "notifications=YES"


    更多信息和示例请参考

    https://github.com/wix/AppleSimulatorUtils

    【讨论】:

      【解决方案2】:

      EarlGreyImpl.invoked(fromFile: #file, lineNumber: #line).selectElement(with: gray_text("Click")).perform(grey_tap())

      //使用上面的代码可能会解决你的问题

      【讨论】:

        【解决方案3】:

        我们发现解决此问题的最佳方法是包含一个用于测试的启动参数,在该参数中我们不会为应用注册通知。

        类似:

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

        打电话之前

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

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

        【讨论】:

          【解决方案4】:

          作为 gray_systemAlertViewShown indicate 的文档,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())];
          

          在编写此系统时,EarlGrey 无法关闭警报视图。可以关闭应用程序启动的警报视图。 FAQ 有一个问题表明如果存在模式对话框,EarlGrey 测试将失败。

          【讨论】:

          • 感谢您的回复,@Gautam。我在已知问题部分读到:“我们计划在 EarlGrey API 中实现这一点,但同时我们建议您使用其他方式来关闭测试代码中的系统权限对话框。”你知道这指的是什么“其他手段”吗?作为一个部分相关的问题,您知道是否可以将 EarlGrey 与 XCode 的 XCUITest 框架集成?我可以通过这种方式消除系统警报。
          • 你可能应该在他们的 Google Group 上问这个问题,或者在 Github 页面上创建一个新问题。 groups.google.com/forum/#!forum/earlgrey-discuss
          • @Guatam - 您是否检查过“应用程序启动的警报视图如何被关闭”?我的意思是,除了使用 EarlGrey 测试的预设标志和自动解除警报(或根本不显示)?
          • @WladekSurala 请在 EarlGrey 测试应用程序测试中查看 this test,其中应用程序内警报 is shown 然后被解雇。
          猜你喜欢
          • 1970-01-01
          • 2016-08-13
          • 1970-01-01
          • 1970-01-01
          • 2016-03-23
          • 1970-01-01
          • 1970-01-01
          • 2022-06-23
          • 2012-06-09
          相关资源
          最近更新 更多