Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing Notifications in Android

My android app has a service which sends notifications to user based on parameters like number of runs of the app. The notifications are sent at different times in different situations. I want to test whether notifications are sent at the right times in all the different cases. Does android provide a way of such a testing ?

like image 264
Gaganpreet Avatar asked Nov 03 '15 09:11

Gaganpreet


People also ask

How do I test my notifications?

Sending a test Android push notificationClick on Settings and open Mobile Apps. Click on the Android App and make sure the Firebase API key has been configured. Click on Test Push and enter the device token for your test device. Add a test payload and send the test.

What is test push notification?

It allows you to have a small group of test users on both Android and iOS to send your test notification out to without bugging the other users of your app. Using Notification Groups allows you to test more targeted notifications.

Can we test push notifications on Android emulator?

To use push notifications, you must install a compatible version of the Google APIs platform. To test your app on the emulator, expand the directory for Android 4.2. 2 (API 17) or a higher version, select Google APIs, and install it. Then create a new AVD with Google APIs as the platform target.

What is testing in Android?

Testing is an integral part of the app development process. By running tests against your app consistently, you can verify your app's correctness, functional behavior, and usability before you release it publicly. You can manually test your app by navigating through it.


2 Answers

Testing Notification using UIAutomator:

Just go through the below code. It will help you in testing the notification.

UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
device.openNotification();
device.wait(Until.hasObject(By.text(NOTIFICATION_TITLE)), TIMEOUT);
UiObject2 title = device.findObject(By.text(NOTIFICATION_TITLE));
UiObject2 text = device.findObject(By.text(NOTIFICATION_TEXT));
assertEquals(NOTIFICATION_TITLE, title.getText());
assertEquals(NOTIFICATION_TEXT, text.getText());
title.click();
device.wait(Until.hasObject(By.text(ESPRESSO.getName())), TIMEOUT);

Don't forget to add the UIAutomator dependencies in build.gradle.

// UIAutomator dependency
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1' 
like image 171
Prem Choudhary Avatar answered Nov 15 '22 20:11

Prem Choudhary


Please read this article

http://qathread.blogspot.com/2015/05/espresso-uiautomator-perfect-tandem.html

Here is a nice explanation of this topic:

Espresso for Android is perfect and fast test automation framework, but it has one important limitation - you are allowed to operate only inside your app under test context.

This means, it is not possible to automate tests for app features such as:

  • application push notifications
  • contact synchronization
  • navigating from another app to your app under test,

Since you have to deal with other apps from the mobile device - NotificationBar, Contacts or People app, etc.

In fact it wasn't possible until the release of UIAutomator 2.0. As stated in Android Developers blog post - "...Most importantly, UI Automator is now based on Android Instrumentation...". And because of that we can run UIAutomator tests as well as Espresso tests using Instrumentation test runner.

In addition to that we can combine UIAutomator tests together with Espresso tests and this gives us the real power and control over the phone and application under test.

like image 28
piotrek1543 Avatar answered Nov 15 '22 19:11

piotrek1543