Search code examples
uitestearlgrey

How to confirm system alert like allow notification


If the app is installed at the first time, need to allow notification, how can I confirm it? Is someone encountered?

enter image description here


Solution

  • you should typically be mocking notifications and other data requests in order to prevent the dialogs from coming up. You could also accept the notification manually and re-run your tests. We experimented with using the private UIAutomation framework for this and saw we could achieve this with it. For example, for pressing the left alert button.

    @interface SystemAlert : NSObject
    - (void)tapLeftButton;
    @end
    
    @interface SystemAlert (ForMethodCompletionOnly)
    + (id)localTarget;
    - (id)frontMostApp;
    - (id)alert;
    - (id)buttons;
    @end
    
    @implementation SystemAlert
    
    + (void)load {
      dlopen([@"/Developer/Library/PrivateFrameworks/UIAutomation.framework/UIAutomation" fileSystemRepresentation], RTLD_LOCAL);
    }
    
    - (void)tapLeftButton {
      id localTarget = [NSClassFromString(@"UIATarget") localTarget];
      id app = [localTarget frontMostApp];
      id alert = [app alert];
      id button = [[alert buttons] objectAtIndex:0];
      [button tap];
    }
    
    @end