[英]Android - automated test for receiving GCM push notifications
如何增加测试范围以接收GCM通知? 测试场景:我的应用程序在后台,通知到达,检查它是否已显示,单击通知以打开应用程序。 当我手动测试时,它可以在模拟器中工作。 我对Android通知未显示在API 26上有些不满,因此需要进行测试以检测破损。 我看到了如何在uiautomator中检测抬头通知? 但未显示如何为我的应用创建通知。
更新我想出了如何创建由接收者处理的意图。
// app already started and in background
Intent intent = new Intent();
intent.setAction("com.google.android.c2dm.intent.RECEIVE");
intent.putExtra("from", "1234567890");
intent.putExtra("message", "Android may be hard, but iOS is even harder");
Context context = InstrumentationRegistry.getInstrumentation().getContext();
context.sendBroadcast(intent);
我的接收者和收听者:
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!--category tag not required if min SDK 16 or higher-->
</intent-filter>
</receiver>
<service
android:name=".gcm.MyGcmListenerService"
android:permission="com.google.android.c2dm.permission.RECEIVE"
android:exported="true" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
解决方案涉及弄清楚一些事情。
检查通知是否显示很棘手,因为无法使用Android Studio布局检查器检查通知抽屉。 因此,找到这些元素是反复试验的。 我这样做:
UiSelector textSelector = new UiSelector().textContains("some text in my message");
UiObject notificationText = device.findObject(textSelector);
然后输出UiObjects的属性,这样我可以使选择器更具体(添加packageName和className)。
我的测试意图和接收器/服务元素的代码在问题的“更新”部分中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.