Xcode UI Testing allow system alerts series

血红的双手。 提交于 2019-12-23 12:33:32

问题


I have problem, if i try allow series system alert, work only one time, and next alert not "allowing" I googling more time, and know about that post: (Xcode 7 UI Testing: how to dismiss a series of system alerts in code) Nothing.. not work. Here my current code, first alert "allowed" successfully, next alert not detected..

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

Best regards, Ivan.

UPD:

Okay, i found solution, how after first alert, try close second (thanks for this site: 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) Just need return always NO.

But another problem...

    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'
)}

He try close third notification, not second, of course he not found this system alert...


回答1:


Create the alert handlers one by one, before the app launches. Also, make sure to tap() anywhere on the app before interacting with the alert. This is a known bug in 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()

These three taps will fir each alert handler in succession without actually triggering any events in your app. Also note that each interruption handler doesn't specify anything about the alert, only the confirmation button.




回答2:


Using a For loop iterating through the number of possible system alerts seems like the most likely failure case in your code. Turning it into a while loop that evaluates the existence of a system alert as the conditional would be a lot cleaner visually, involve less total logic, and not have the failure condition where self.possibleSystemAlerts isn't the correct value.

Your entire monitor logic can also be ripped out. The test will wait until the app idles, by which time you'll have an alert or not have an alert. Evaluate its presence or lack thereof, interact with it or end the loop.



来源:https://stackoverflow.com/questions/36753813/xcode-ui-testing-allow-system-alerts-series

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!