I am new to UI Automator.I have an application running as a service. It shows an icon on the notification bar
Is there any way to test this newly added icon on the top Notification bar ? I see no contol with uiautomatorviewer
or If there is any way to get icon information from NotificationMgr API please let me know it would be helpful
If you go to android SDK>tools>UIAutomator. Make sure device is connected, tap on green button on top left side. This will take the screenshot of your current screen on the device. This way you can point mouse on the screen to detect elements.
It is present under the tools folder in Android SDK: We use this tool to find out the application UI hierarchy and show the details of the elements present in the UI. We can inspect the attributes of an element by clicking on the element.
For notifications:
private UiObject getNotificationStackScroller()
{
/*
* access Notification Center through resource id, package name, class name.
* if you want to check resource id, package name or class name of the specific view in the screen,
* run 'uiautomatorviewer' from command.
*/
UiSelector notificationStackScroller = new UiSelector().packageName("com.android.systemui")
.className("android.view.ViewGroup")
.resourceId(
"com.android.systemui:id/notification_stack_scroller");
UiObject notificationStackScrollerUiObject = mDevice.findObject(notificationStackScroller);
assertTrue(notificationStackScrollerUiObject.exists());
return notificationStackScrollerUiObject;
}
and the access any "i-th" item through:
UiObject notificationUiObject = getNotificationStackScroller().getChild(new UiSelector().index(i));
assertTrue(notificationUiObject.exists());
and for example check if number of notifications is "3" exits:
UiObject numberOfNotifications = notificationUiObject.getChild(new UiSelector().text("3"));
assertTrue(numberOfNotifications.exists());
The UiDevice.openNotification method, added in API level 18, will programmatically open the notification drawer.
Usage Example
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
Context appContext = InstrumentationRegistry.getTargetContext();
assertEquals("com.mydomain.myuiautomatortests", appContext.getPackageName());
appContext.startActivity(new Intent(appContext, MainActivity.class));
UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
mDevice.openNotification();
}
}
Gradle Dependency
compile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2' // or higher
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With