Xcode UI测试允许系统警报系列



我有问题,如果我尝试允许系列系统警报,只工作一次,下一次警报不"允许"我在谷歌上搜索了更多的时间,并了解了这篇文章:(Xcode 7 UI测试:如何在代码中消除一系列系统警报)没有什么不起作用。这里是我当前的代码,第一个警报"允许"成功,下一个警报未检测到。。

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];
}

谨致问候,伊万。

UPD:

好吧,我找到了解决方案,如何在第一次警报后,尝试关闭第二次(感谢这个网站:http://www.it1me.com/it-answers?id=32148965&s=模板:Viper&ttl=Xcode+7+UI+测试%3A+如何+到+解除+a+系列+系统+警报+在+代码)只需要返回总是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'
)}

他试图关闭第三个通知,而不是第二个,当然他没有发现这个系统警报。。。

在应用程序启动之前逐个创建警报处理程序。此外,在与警报交互之前,请确保在应用程序的任何位置使用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()

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

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

您的整个监视器逻辑也可能被破坏。测试将等待应用程序空闲,届时您将收到警报或没有警报。评估它是否存在,与它互动或结束循环。

最新更新