简体   繁体   中英

How to send a broadcast using adb with arguments in an Appium test case?

My test case is simple. I have a condition in my app, whenever a push notification has been received I need to check for a parameter in the extras against a parameter I have stored locally on the Android device. If these parameter values do not match I should ignore the push notification, else I will show it on the device.

So now that I have implemented everything and tested to be working fine I want to write a test case so that in the future I can automate this test. I use eclipse JUNIT test cases with Appium for automating tests. When testing I use adb to trigger push notifications since it's easier. This is the command I use to trigger it from the terminal..

adb shell am broadcast -a com.google.android.c2dm.intent.RECEIVE -n package.name/com.google.android.gms.gcm.GcmReceiver --ei "pw_msg" 1

.. which works as expected. In my Appium test cases I have it like this..

Runtime.getRuntime().exec("adb shell am broadcast -a com.google.android.c2dm.intent.RECEIVE -n package.name/com.google.android.gms.gcm.GcmReceiver --ei \"pw_msg\" 1");

.. which does not seem to trigger the push notification. I can confirm that I can execute adb commands from the Appium test case fine because commands such as

Runtime.getRuntime().exec("adb shell svc data disable")
Runtime.getRuntime().exec("adb shell am start -n package.name/package.name.MainActivity")

works fine. What's wrong with the broadcast adb command? Am I writing it wrong? or running into some limitation or something else?

After a bit of work I have fixed the problem. First, I read the output of the terminal like this..

final Process exec = Runtime.getRuntime().exec("adb shell am broadcast -a com.google.android.c2dm.intent.RECEIVE "
                + "-n facilit.net.mobile.debug/com.google.android.gms.gcm.GcmReceiver "
                + "--ei \"pw_msg\" 1");

        final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(exec.getInputStream()));

        String s;
        while((s = bufferedReader.readLine()) != null) {
            System.out.println(s);
        }

This allowed me to see the output from the terminal such as..

Broadcasting: Intent { act=com.google.android.c2dm.intent.RECEIVE cmp=package.name/com.google.android.gms.gcm.GcmReceiver (has extras) } Broadcast completed: result=404

Usually 404 means there was a formatting error in the command. All I had to do was correct the formatting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM