Testing Notifications in Android

孤街浪徒 提交于 2019-12-04 07:26:36
Prem Choudhary

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' 
piotrek1543

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!