Is it possible to dismiss System Alerts using EarlGrey (iOS UI Testing)?

时光总嘲笑我的痴心妄想 提交于 2019-12-10 15:26:00

问题


I'm beginning to experiment with EarlGrey a little bit, having done UI Testing using the XCUITest for some months now. I'm running into the classic problem of being unable to dismiss system alerts, which is strange as it looks as though Google implemented a matcher for system alerts called grey_systemAlertViewShown(). I'm trying to detect system alerts using GREYCondition. Here's what I've tried:

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

I've also tried using addUIInterruptionMonitorWithDescription as described here (but using EarlGrey code to do basically what I am doing above in the interruption monitors): Xcode 7 UI Testing: how to dismiss a series of system alerts in code

Neither approach works. Breakpoints don't fire for the non-error case in my GREYCondition, and the interruption monitor doesn't dismiss my alert either.

Does anybody know if EarlGrey supports dismissing system alerts?


回答1:


As the docs for grey_systemAlertViewShown indicate, grey_systemAlertViewShown merely checks to see if system alert views are shown. A better usage of the API would be to assert that system alert is not shown (maybe because the test app has mocked out the code that causes system alerts).

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())];

As of this writing system alert views cannot be dismissed by EarlGrey. Alertviews that are launched by the app can be dismissed. The FAQ has a question that indicates that EarlGrey tests will fail if modal dialogs are present.




回答2:


The best way we found to get around this was to include a launch argument for testing, in which we wouldn't register the app for notifications.

Something like:

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

before you call

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

This way, the "Do you want to allow push notifications..." alert won't show.




回答3:


It is possible to grant all the required permissions using AppleSimulatorUtils util.

This approach eliminates the need to dismiss alerts and saves time.

Install an util by entering next commands in the Terminal app

brew tap wix/brew

brew install applesimutils

And grant permission(s) with

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


For more info and examples please refer to

https://github.com/wix/AppleSimulatorUtils




回答4:


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

//use above code it might work for your problem



来源:https://stackoverflow.com/questions/35589615/is-it-possible-to-dismiss-system-alerts-using-earlgrey-ios-ui-testing

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