1

I've been having a hard time trying to dismiss UIAlertControllers in Earl Grey. I followed the example in this example, but I cannot dismiss the alerts by using grey_text. I'm using Swift to write the tests where I need to dismiss the alerts, and the app is written in Objective-C.

So how can I dismiss the alerts via the Earl Grey framework? I've also used grey_accessibilityLabel with no luck, and I can't add an accessibility ID to a UIAlertController, to my knowledge.

Justin C
  • 11
  • 2
  • Is this a simple UIAlertController or a System Alert? We have tests for this in - https://github.com/google/EarlGrey/blob/master/Tests/FunctionalTests/Sources/FTRAlertViewTest.m – gran_profaci Jun 01 '17 at 00:31
  • This is a UIAlertController. I followed the test example, (which used grey_text to find the button and press it) but I could not use grey_text in my test to find the "OK" button to dismiss it. I got a noMatchingElements exception. – Justin C Jun 01 '17 at 00:33
  • If it's a system generated alert like asking for permission to use location or send notification or access photos on a **simulator** then you can dismiss some of these alerts using this trick: https://stackoverflow.com/questions/28443578/ios-permission-alerts-removing-or-surpressing (it only works on simulators). If it is on **device** then your best bet is to mock out the API that's triggering the system alert because the trick above doesn't work on devices. – khandpur Jun 01 '17 at 00:38
  • fyi, there's also an issue tracking it: https://github.com/google/EarlGrey/issues/55 – khandpur Jun 01 '17 at 00:44
  • Thanks for the info! My issue is with the app-generated alerts, however, not the system ones. – Justin C Jun 01 '17 at 00:45

1 Answers1

0

I was able to assert a UIAlertController and perform an action on the same using the following code:

EarlGrey.select(elementWithMatcher: grey_text("You are about to exit")).assert(grey_sufficientlyVisible())
EarlGrey.select(elementWithMatcher: grey_text("The information you have entered will not be saved.")).assert(grey_sufficientlyVisible())
EarlGrey.select(elementWithMatcher: grey_text("Confirm")).assert(grey_sufficientlyVisible())
EarlGrey.select(elementWithMatcher: grey_text("Cancel")).perform(grey_tap())

Using the matcher grey_buttonTitle("Cancel") did not seem to be working.

ZeMoon
  • 20,054
  • 5
  • 57
  • 98