Android - 接收 GCM 推送通知的自动化测试
Android - automated test for receiving GCM push notifications
如何添加接收 GCM 通知的测试范围?测试场景:我的应用程序在后台,通知到达,检查它是否显示,点击通知打开应用程序。当我手动测试时,它在模拟器中工作。我遇到了 so need a test to detect breakages. I saw this How to detect headsup notification in 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>
解决方案涉及弄清楚一些事情。
- 在 AndroidManifest.xml 中,对于接收器元素,SDK 16 及更高版本将忽略类别标签。
- 显示的通知可能因应用程序处于前台还是后台而异。
- 用于查找通知的 UISelector 的类名与上面 link 中的示例不同。从仅使用 "textContains" 方法开始,然后优化您的选择器。
- 我们的 GcmListenerService 忽略了没有指定发件人的消息,因此必须在我的意图中添加 "from"。
- 我无法在 https://console.firebase.google.com 上找到查看现有 GCM 消息的方法,因此让我的 Intent 匹配服务器发送的内容(例如,from 字段未由服务器明确设置)是反复试验应用)
检查通知是否显示很棘手,因为无法使用 Android Studio Layout Inspector 检查通知抽屉。所以找到元素是反复试验。我这样做了:
UiSelector textSelector = new UiSelector().textContains("some text in my message");
UiObject notificationText = device.findObject(textSelector);
然后输出 UiObject 的属性,这样我就可以使选择器更具体(添加 packageName 和 className)。
我的测试意图代码和 receiver/service 元素在问题的更新部分。
如何添加接收 GCM 通知的测试范围?测试场景:我的应用程序在后台,通知到达,检查它是否显示,点击通知打开应用程序。当我手动测试时,它在模拟器中工作。我遇到了
更新 我想出了如何创建一个由接收者处理的意图。
// 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>
解决方案涉及弄清楚一些事情。
- 在 AndroidManifest.xml 中,对于接收器元素,SDK 16 及更高版本将忽略类别标签。
- 显示的通知可能因应用程序处于前台还是后台而异。
- 用于查找通知的 UISelector 的类名与上面 link 中的示例不同。从仅使用 "textContains" 方法开始,然后优化您的选择器。
- 我们的 GcmListenerService 忽略了没有指定发件人的消息,因此必须在我的意图中添加 "from"。
- 我无法在 https://console.firebase.google.com 上找到查看现有 GCM 消息的方法,因此让我的 Intent 匹配服务器发送的内容(例如,from 字段未由服务器明确设置)是反复试验应用)
检查通知是否显示很棘手,因为无法使用 Android Studio Layout Inspector 检查通知抽屉。所以找到元素是反复试验。我这样做了:
UiSelector textSelector = new UiSelector().textContains("some text in my message");
UiObject notificationText = device.findObject(textSelector);
然后输出 UiObject 的属性,这样我就可以使选择器更具体(添加 packageName 和 className)。
我的测试意图代码和 receiver/service 元素在问题的更新部分。