Play with Automating Notifications

OG-Push-Release@2x-1

          We know that notifications are playing a major role in mobile devices for quick references, to take quick navigation and fast response. Nowadays retrieving the information from notification during automation is one of the important aspects. The latest version of Appium helps to retrieve the information from the notification in Android. In this article, I would like to share two interesting information about automating notifications. The first one is retrieving status bar notifications for Android devices and the second one is iOS push notification.

Retrieving status bar notification for Android

          For most of the history of Appium, it has not been possible to inspect notifications received on an Android device, especially the status bar notifications you can only look at by pulling them down from the status bar. To automate verification of notifications received, the brute-force approach meant navigating to this notifications view and hoping to get lucky reading the text elements on that page. Recently, the Appium implemented a way to read notifications via the Appium Settings helper app. Basically, the Appium Settings app can be granted permission to read any incoming notifications. It can then save these notifications for retrieval by the main Appium process using ADB. The below command helps to get the notification details as JSON response.

driver.executeScript(“mobile: getNotifications”);

      For this command to work, the Appium Settings app must, of course, be granted notification read permissions. This can be done manually in the Notification Access area of your Android system settings. In the Appium Java client, we will not get a JSON response, so we need to cast the response appropriately:

Map<String, Object> notifications = (Map<String, Object>)driver.executeScript(“mobile: getNotifications”);

        And we’ll need to keep casting as we navigate through the object. Basically, we have a main key statusBarNotifications, which returns an array of notification objects. Each of these notification objects itself has a lot of metadata associated with it (you can use this metadata to filter by the package ID of your app, for example). The notification key of this object has the main content we’re looking for, including the title and the text of the notification. Below is the sample code snippet:

Map<String, Object> response = (Map<String, Object>)driver.executeScript(“mobile: getNotifications“);
List<Map<String, Object>> notifications = (List<Map<String, Object>>)response .get(“statusBarNotifications“);
for (Map<String, Object> notification : notifications) {
Map<String, String> innerNotification = (Map<String, String>)notification.get(“notification“);
if (innerNotification.get(“title”) != null) {
System.out.println(innerNotification.get(“title”));

}

        This technique is especially powerful because, having granted the special permission to Appium, you can read notifications for any app on the device.

iOS push notification

      One of the common requirements for testers is to ensure that iOS Push Notifications are getting sent correctly from the backend service. The automation tools provided by Apple don’t make this easy. XCUITest has no built-in API methods for inspecting push notifications received. Nor is there any way to even simulate push notifications on a simulator, which makes real devices a hard requirement for automation. But Appium has the facility to automate the entire device UI from the perspective of a user interacting with the screen. This means we can do swipe to open the notifications shade and read the element hierarchy there to determine if our notification has arrived.

     Prior to starting testing the iOS push notification, your iPhone or iPad has the capability to receive the push notification messages. Also, a way to trigger the backend app service to send a push notification to a specific device, based on the device udid. This can be achieved by making API calls via code prior to start the actual retrieving of notification messages. Below is the test flows to achieve the iOS push notification testing:

  • Launch an Appium session with your application.
  • Closeout your app, to mimic the experience of a user receiving a push notification while doing something else.
  • Trigger the backend service to send a push notification to the device under test.
  • Swipe open the notification shade on the device.
  • Check the elements on the notification shade for one that matches the message that was sent via the backend service.
  • Swipe closed the notification shade.

       Once the Appium session gets started below is the sample code snippet for remaining test flow:

//Closeout your application:
driver.terminateApp(YOUR_APP_BUNDLE_ID);

//Swipe open the notification shade on the device:
int yMargin = 5;
int xMid = screenSize.width / 2;
PointOption top = PointOption.point(xMid, yMargin);
PointOption bottom = PointOption.point(xMid, screenSize.height – yMargin);
TouchAction action = new TouchAction(driver);
action.press(top);
action.waitAction(WaitOptions.waitOptions(Duration.ofSeconds(1)));
action.moveTo(bottom);
action.perform();

//Check the elements on the notification shade:
driver.findElement(By.xpath(“//XCUIElementTypeCell[contains(@label, ‘notification text’)]”));

//Swipe closed the notification shade:
int yMargin = 5;
int xMid = screenSize.width / 2;
PointOption top = PointOption.point(xMid, yMargin);
PointOption bottom = PointOption.point(xMid, screenSize.height – yMargin);
TouchAction action = new TouchAction(driver);
action.press(bottom);
action.waitAction(WaitOptions.waitOptions(Duration.ofSeconds(1)));
action.moveTo(top);
action.perform();

//Re-open your application:
driver.activateApp(YOUR_APP_BUNDLE_ID);

     You can use the above tricks to deal with notifications in Android and iOS devices to retrieve notification information and verify the push notification details on iOS.

Reference: Appium Pro

make it perfect!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: