【问题标题】:Xcode UI Testing allow system alerts seriesXcode UI 测试允许系统警报系列
【发布时间】:2016-08-13 17:20:54
【问题描述】:

我有问题,如果我尝试允许系列系统警报,只工作一次,下一个警报不是“允许” 我在谷歌上搜索了更多时间,并且知道那个帖子:(Xcode 7 UI Testing: how to dismiss a series of system alerts in code) 什么都没有。。不行。 这是我当前的代码,第一个警报“允许”成功,下一个警报未检测到..

XCUIApplication *app = [[XCUIApplication alloc] init];
app.launchEnvironment = @{
        @"isUITest" : @YES,
        @"withFakeData" : fakeData
};
[app launch];


for (int i = 1; i <= self.possibleSystemAlerts; i++) {
    NSLog(@"%d", i);
    XCTestExpectation *expectation = [self expectationWithDescription:@"High Expectations"];
    id monitor = [self addUIInterruptionMonitorWithDescription:@"Push notifications" handler:^BOOL(XCUIElement *_Nonnull interruptingElement) {
        XCUIElement *element = interruptingElement;
        XCUIElement *allow = element.buttons[@"Allow"];
        XCUIElement *ok = element.buttons[@"OK"];

        if ([ok exists]) {
            [ok tap];
            [expectation fulfill];
            return YES;
        }

        if ([allow exists]) {
            [allow forceTap];
            [expectation fulfill];
            return YES;
        }
        return NO;
    }];
    [app tap];
    [self waitForExpectationsWithTimeout:6.0 handler:^(NSError *error) {
        if (error) {
            NSLog(@"Timeout Error: %@", error);
        }
    }];
    [self removeUIInterruptionMonitor:monitor];
}

最好的问候, 伊万。

更新:

好的,我找到了解决方案,如何在第一次警报后尝试关闭第二次(感谢此站点:http://www.it1me.com/it-answers?id=32148965&s=Template:Viper&ttl=Xcode+7+UI+Testing%3A+how+to+dismiss+a+series+of+system+alerts+in+code)只需要始终返回 NO。

但是还有一个问题……

    t =    10.18s                     Find: Descendants matching type Alert
    t =    10.18s                     Find: Identity Binding
    t =    11.19s                     Find the "Allow “MyApp” to access your location while you use the app?" Alert (retry 1)
    t =    11.19s                         Snapshot accessibility hierarchy for com.apple.springboard
    t =    11.26s                         Find: Descendants matching type Alert
    t =    11.26s                         Find: Identity Binding
    t =    12.27s                     Find the "Allow “MyApp” to access your location while you use the app?" Alert (retry 2)
    t =    12.27s                         Snapshot accessibility hierarchy for com.apple.springboard
    t =    12.33s                         Find: Descendants matching type Alert
    t =    12.34s                         Find: Identity Binding
    t =    12.42s                     Assertion Failure: UI Testing Failure - No matches found for "Allow “MyApp” to access your location while you use the app?" Alert
Query input was {(
    Alert 0x7febe8731630: traits: 72057602627862528, {{25.0, 193.0}, {270.0, 182.0}}, label: '“MyApp” Would Like to Send You Notifications'
)}

他尝试关闭第三个通知,而不是第二个,当然他没有找到这个系统警报...

【问题讨论】:

  • 您在更新中链接到的网站不只是您在原始帖子中链接到的相同 Q/A 的抓取吗?

标签: ios objective-c xcode xcode-ui-testing ui-testing


【解决方案1】:

在应用启动之前,一一创建警报处理程序。此外,在与警报交互之前,请确保在应用程序的任何位置tap()。这是 Xcode 中的一个已知错误。

addUIInterruptionMonitor(withDescription:"First Dialog") { (alert) -> Bool in
    alert.buttons["Allow"].tap()
    return true
}

addUIInterruptionMonitor(withDescription:"Second Dialog") { (alert) -> Bool in
    alert.buttons["Allow"].tap()
    return true
}

addUIInterruptionMonitor(withDescription:"Third Dialog") { (alert) -> Bool in
    alert.buttons["Allow"].tap()
    return true
}

let app = XCUIApplication()
app.launch()

app.tap()
app.tap()
app.tap()

这三个点击将连续触发每个警报处理程序,而不会实际触发您应用中的任何事件。另请注意,每个中断处理程序都没有指定关于警报的任何内容,只有确认按钮。

【讨论】:

  • 问题被标记在目标 c 下。该解决方案在 Swift 上
  • 这对我们在 Xcode 9/iOS 11 下试图消除 SFAuthenticationSession 警报不起作用。
  • 但是这个解决方案对我们有用:stackoverflow.com/a/46951214/17294
  • ... 有时。
  • 我应该把什么作为描述添加到 addUIInterruptionMonitor ?我在 info.plist 中设置的描述?
【解决方案2】:

使用 For 循环遍历可能的系统警报的数量似乎是您的代码中最有可能失败的情况。将其转换为评估系统警报是否存在的 while 循环作为条件将在视觉上更清晰,涉及更少的总逻辑,并且不会出现 self.possibleSystemAlerts 不是正确值的失败条件。

您的整个监控逻辑也可能被删除。测试将等到应用程序空闲,届时您将收到警报或没有警报。评估它的存在与否、与之交互或结束循环。

【讨论】:

  • 在开始新测试之前,我从我的模拟器中删除了应用程序。我使用“waitForExpectationsWithTimeout”来不开始主要测试。
  • 更新问题,请查收。
猜你喜欢
  • 2015-11-15
  • 2022-06-23
  • 2016-03-23
  • 2016-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-28
  • 1970-01-01
相关资源
最近更新 更多