Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test Notification bar icons using uiautomator

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

like image 325
user3013582 Avatar asked Nov 20 '13 15:11

user3013582


People also ask

How do you use the UIAutomator in Appium?

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.

Where is UIAutomator located?

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.


2 Answers

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());
like image 55
Tomasz Jarosik Avatar answered Sep 22 '22 02:09

Tomasz Jarosik


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 
like image 24
albert c braun Avatar answered Sep 20 '22 02:09

albert c braun