android – ProgressBars和Espresso

前端之家收集整理的这篇文章主要介绍了android – ProgressBars和Espresso前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我在运行一些espresso测试时显示的布局中有一个ProgressBar – 然后我碰到:
  1. 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-javaHow to detect whether android app is running UI test with Espresso的一些想法.

起初我创建了自定义的ProgressBar类,一个用于调试,一个用于发布.发布版本只调用超级构造函数,不做任何其他操作.调试版本覆盖方法setIndeterminateDrawable.有了这个,我可以设置一个简单的drawable而不是动画的.

发行代码

  1. public class ProgressBar extends android.widget.ProgressBar {
  2.  
  3. public ProgressBar(Context context) {
  4. super(context);
  5. }
  6.  
  7. public ProgressBar(Context context,AttributeSet attrs) {
  8. super(context,attrs);
  9. }
  10.  
  11. public ProgressBar(Context context,AttributeSet attrs,int defStyleAttr) {
  12. super(context,attrs,defStyleAttr);
  13. }
  14.  
  15. @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  16. public ProgressBar(Context context,int defStyleAttr,int defStyleRes) {
  17. super(context,defStyleAttr,defStyleRes);
  18. }
  19.  
  20. }

调试代码

  1. public class ProgressBar extends android.widget.ProgressBar {
  2.  
  3. public ProgressBar(Context context) {
  4. super(context);
  5. }
  6.  
  7. public ProgressBar(Context context,defStyleRes);
  8. }
  9.  
  10. @SuppressWarnings("deprecation")
  11. @Override
  12. public void setIndeterminateDrawable(Drawable d) {
  13. if (isRunningTest()) {
  14. d = getResources().getDrawable(R.drawable.ic_replay);
  15. }
  16. super.setIndeterminateDrawable(d);
  17. }
  18.  
  19. private boolean isRunningTest() {
  20. try {
  21. Class.forName("base.EspressoTestBase");
  22. return true;
  23. } catch (ClassNotFoundException e) {
  24. /* no-op */
  25. }
  26. return false;
  27. }
  28.  
  29. }

如您所见,我还添加了一个支票,如果我的应用程序正在运行Espresso测试,而我正在搜索的类是我的Espresso测试的基础.

不好的是,你必须更新所有的代码才能使用自定义的ProgressBar.但是好的是,您的发行代码对此解决方案没有重大影响.

猜你在找的Android相关文章