当我在运行一些espresso测试时显示的布局中有一个ProgressBar – 然后我碰到:
- Caused by: android.support.test.espresso.AppNotIdleException: Looped for 1670 iterations over 60 SECONDS. The following Idle Conditions Failed .
什么是一个很好的方式来解决这个问题?发现一些黑客,但寻找一个很好的方式
解决方法
我有同样的问题.我找不出一个完美的解决方案,但我也会发布我的方法.
我试图做的是覆盖ProgressBar上的indeterminateDrawable.当有一个简单的drawable没有动画发生和Espresso测试没有遇到空闲问题.
不幸的是,main和androidTest都是一样的.我没有找到一种方法来覆盖我的ProgressBar的样式.
现在最终结合了https://gist.github.com/Mauin/62c24c8a53593c0a605e#file-progressbar-java和How to detect whether android app is running UI test with Espresso的一些想法.
起初我创建了自定义的ProgressBar类,一个用于调试,一个用于发布.发布版本只调用超级构造函数,不做任何其他操作.调试版本覆盖方法setIndeterminateDrawable.有了这个,我可以设置一个简单的drawable而不是动画的.
发行代码:
- public class ProgressBar extends android.widget.ProgressBar {
- public ProgressBar(Context context) {
- super(context);
- }
- public ProgressBar(Context context,AttributeSet attrs) {
- super(context,attrs);
- }
- public ProgressBar(Context context,AttributeSet attrs,int defStyleAttr) {
- super(context,attrs,defStyleAttr);
- }
- @TargetApi(Build.VERSION_CODES.LOLLIPOP)
- public ProgressBar(Context context,int defStyleAttr,int defStyleRes) {
- super(context,defStyleAttr,defStyleRes);
- }
- }
调试代码:
- public class ProgressBar extends android.widget.ProgressBar {
- public ProgressBar(Context context) {
- super(context);
- }
- public ProgressBar(Context context,defStyleRes);
- }
- @SuppressWarnings("deprecation")
- @Override
- public void setIndeterminateDrawable(Drawable d) {
- if (isRunningTest()) {
- d = getResources().getDrawable(R.drawable.ic_replay);
- }
- super.setIndeterminateDrawable(d);
- }
- private boolean isRunningTest() {
- try {
- Class.forName("base.EspressoTestBase");
- return true;
- } catch (ClassNotFoundException e) {
- /* no-op */
- }
- return false;
- }
- }
如您所见,我还添加了一个支票,如果我的应用程序正在运行Espresso测试,而我正在搜索的类是我的Espresso测试的基础.
不好的是,你必须更新所有的代码才能使用自定义的ProgressBar.但是好的是,您的发行代码对此解决方案没有重大影响.