繁体   English   中英

Android-接收GCM推送通知的自动测试

[英]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>

解决方案涉及弄清楚一些事情。

  1. 在AndroidManifest.xml中,对于接收器元素,SDK 16及更高版本将忽略类别标签。
  2. 根据应用是在前台还是在后台,显示的通知可能会有所不同。
  3. 与上面的链接中的示例相比,用于查找通知的UISelector具有不同的className。 首先使用“ textContains”方法,然后优化选择器。
  4. 我们的GcmListenerService忽略了没有指定from的消息,因此必须在我的意图中添加“ from”。
  5. 我找不到在https://console.firebase.google.com上查看现有GCM消息的方法,因此让我的意图与服务器发送的内容相匹配(例如,来自字段未由明确设置)是一次反复的尝试服务器应用)

检查通知是否显示很棘手,因为无法使用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.

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