简体   繁体   English

在Android中测试通知

[英]Testing Notifications in Android

My android app has a service which sends notifications to user based on parameters like number of runs of the app. 我的Android应用程序有一个服务,根据应用程序的运行次数等参数向用户发送通知。 The notifications are sent at different times in different situations. 通知在不同情况下的不同时间发送。 I want to test whether notifications are sent at the right times in all the different cases. 我想测试在所有不同情况下是否在适当的时间发送通知。 Does android provide a way of such a testing ? android是否提供了这种测试方式?

Testing Notification using UIAutomator: 使用UIAutomator测试通知:

Just go through the below code. 只需浏览以下代码即可。 It will help you in testing the notification. 它将帮助您测试通知。

UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
device.openNotification();
device.wait(Until.hasObject(By.text(NOTIFICATION_TITLE)), TIMEOUT);
UiObject2 title = device.findObject(By.text(NOTIFICATION_TITLE));
UiObject2 text = device.findObject(By.text(NOTIFICATION_TEXT));
assertEquals(NOTIFICATION_TITLE, title.getText());
assertEquals(NOTIFICATION_TEXT, text.getText());
title.click();
device.wait(Until.hasObject(By.text(ESPRESSO.getName())), TIMEOUT);

Don't forget to add the UIAutomator dependencies in build.gradle . 不要忘记在build.gradle添加build.gradle依赖build.gradle

// UIAutomator dependency
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1' 

Please read this article 请阅读这篇文章

http://qathread.blogspot.com/2015/05/espresso-uiautomator-perfect-tandem.html http://qathread.blogspot.com/2015/05/espresso-uiautomator-perfect-tandem.html

Here is a nice explanation of this topic: 以下是对此主题的一个很好的解释:

Espresso for Android is perfect and fast test automation framework, but it has one important limitation - you are allowed to operate only inside your app under test context. 适用于Android的Espresso是完美且快速的测试自动化框架,但它有一个重要的限制 - 您只能在测试环境下的应用程序内部操作。

This means, it is not possible to automate tests for app features such as: 这意味着,无法自动执行应用功能的测试,例如:

  • application push notifications 应用推送通知
  • contact synchronization 联系同步
  • navigating from another app to your app under test, 从另一个应用导航到您测试的应用,

Since you have to deal with other apps from the mobile device - NotificationBar , Contacts or People app, etc. 由于您必须处理来自移动设备的其他应用程序 - NotificationBarContactsPeople app等。

In fact it wasn't possible until the release of UIAutomator 2.0 . 事实上,直到UIAutomator 2.0发布才有可能实现。 As stated in Android Developers blog post - "...Most importantly, UI Automator is now based on Android Instrumentation...". 正如Android开发者博客文章中所述 - “......最重要的是, UI Automator现在基于Android Instrumentation ......”。 And because of that we can run UIAutomator tests as well as Espresso tests using Instrumentation test runner . 因此,我们可以使用Instrumentation test runner运行器运行UIAutomator测试以及Espresso测试。

In addition to that we can combine UIAutomator tests together with Espresso tests and this gives us the real power and control over the phone and application under test. 除此之外,我们还可以将UIAutomator测试与Espresso测试结合起来,这使我们能够真正掌控手机和被测应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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