Push notification is a message pop up to any mobile device and can be sent from any app that is installed on device.
There are situation we need to test or verify if push notification is working as expected for the app we are currently testing.
Appium has the ability to handle push notifications, even we can
- count no. of notification we have received
- Read the notification message.
- verify if correct message content received.
Appium provides a method openNotifications() which will open the notification panel and reads the content, let’s see the implementation
I am taking “API Demos” application to create a push notification and then verify the message content
public class PushNoti { static DesiredCapabilities cap; static AndroidDriver<MobileElement> driver; AppiumDriverLocalService service; @BeforeTest public void setup() { cap = new DesiredCapabilities(); cap.setCapability(CapabilityType.PLATFORM, "Android"); cap.setCapability(CapabilityType.VERSION, "5.1.0"); cap.setCapability("deviceName", "mygeny510"); cap.setCapability("appPackage", "io.appium.android.apis"); cap.setCapability("appActivity", "ApiDemos"); } @Test public void LaunchDialer() throws Exception { driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), cap); driver.findElementByName("App").click(); driver.findElementByName("Notification").click(); driver.findElementByName("IncomingMessage").click(); driver.findElementByName("Show Interstitial Notification").click(); Thread.sleep(3000); driver.openNotifications(); Thread.sleep(2000); List<MobileElement> allnotifications = driver.findElements(By.id("android:id/title")); System.out.println("no of notifications " + allnotifications.size()); for (MobileElement webElement : allnotifications) { System.out.println(webElement.getText()); if(webElement.getText().contains("Dianne")){ System.out.println("Notification found"); break; } } driver.quit(); System.out.println("Finish......................................"); } }
You will notice the notification count will be 1 [in this case we have created one notification] and the content [basically the title of message]