• <bdo id='DQn4m'></bdo><ul id='DQn4m'></ul>

    <small id='DQn4m'></small><noframes id='DQn4m'>

    <legend id='DQn4m'><style id='DQn4m'><dir id='DQn4m'><q id='DQn4m'></q></dir></style></legend>
    1. <i id='DQn4m'><tr id='DQn4m'><dt id='DQn4m'><q id='DQn4m'><span id='DQn4m'><b id='DQn4m'><form id='DQn4m'><ins id='DQn4m'></ins><ul id='DQn4m'></ul><sub id='DQn4m'></sub></form><legend id='DQn4m'></legend><bdo id='DQn4m'><pre id='DQn4m'><center id='DQn4m'></center></pre></bdo></b><th id='DQn4m'></th></span></q></dt></tr></i><div id='DQn4m'><tfoot id='DQn4m'></tfoot><dl id='DQn4m'><fieldset id='DQn4m'></fieldset></dl></div>

    2. <tfoot id='DQn4m'></tfoot>

        是否可以使用 Espresso 的 IdlingResource 等到某个视图出现?

        Is it possible to use Espresso#39;s IdlingResource to wait until a certain view appears?(是否可以使用 Espresso 的 IdlingResource 等到某个视图出现?)
          <bdo id='yFsTv'></bdo><ul id='yFsTv'></ul>

            <legend id='yFsTv'><style id='yFsTv'><dir id='yFsTv'><q id='yFsTv'></q></dir></style></legend>
              <tbody id='yFsTv'></tbody>

            <tfoot id='yFsTv'></tfoot>

            1. <small id='yFsTv'></small><noframes id='yFsTv'>

              • <i id='yFsTv'><tr id='yFsTv'><dt id='yFsTv'><q id='yFsTv'><span id='yFsTv'><b id='yFsTv'><form id='yFsTv'><ins id='yFsTv'></ins><ul id='yFsTv'></ul><sub id='yFsTv'></sub></form><legend id='yFsTv'></legend><bdo id='yFsTv'><pre id='yFsTv'><center id='yFsTv'></center></pre></bdo></b><th id='yFsTv'></th></span></q></dt></tr></i><div id='yFsTv'><tfoot id='yFsTv'></tfoot><dl id='yFsTv'><fieldset id='yFsTv'></fieldset></dl></div>

                  本文介绍了是否可以使用 Espresso 的 IdlingResource 等到某个视图出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在我的测试中,我有一个阶段,在按下按钮后,应用程序会执行大量异步计算并向云服务发出请求,之后它会显示某个视图.

                  In my test I have a stage where after pressing a button application does a lot of asynchronous calculations and requests to the cloud service, after which it displays a certain view.

                  是否可以使用 Espresso 的 IdlingResource 实现等到某个视图出现?

                  Is it possible to use Espresso's IdlingResource implementation to wait until a certain view appears?

                  我已阅读此处的答案,并且评论似乎表明您可以使用 IdlingResource 代替,但我不明白如何.Espresso 似乎没有任何内置方法来处理长操作,但必须编写自己的等待循环感觉就像是 hack.

                  I've read an answers here and comments seems to suggest that you can use IdlingResource instead, but I don't understand how. Espresso does not seem to have any built-in way to handle long operations, but having to write your own waiting loops feels like a hack.

                  有什么方法可以解决这个问题,或者我应该按照链接线程中的答案建议做吗?

                  Any way to solve this or should I just do as the answer in the linked thread suggests?

                  推荐答案

                  您的 IdlingResource 可能如下所示:

                  Your IdlingResource could look like this:

                  import android.support.test.espresso.IdlingResource;
                  import android.support.test.espresso.ViewFinder;
                  import android.support.test.espresso.ViewInteraction;
                  import android.view.View;
                  
                  import org.hamcrest.Matcher;
                  
                  import java.lang.reflect.Field;
                  
                  import static android.support.test.espresso.Espresso.onView;
                  
                  public class ViewShownIdlingResource implements IdlingResource {
                  
                      private static final String TAG = ViewShownIdlingResource.class.getSimpleName();
                  
                      private final Matcher<View> viewMatcher;
                      private ResourceCallback resourceCallback;
                  
                      public ViewShownIdlingResource(final Matcher<View> viewMatcher) {
                          this.viewMatcher = viewMatcher;
                      }
                  
                      @Override
                      public boolean isIdleNow() {
                          View view = getView(viewMatcher);
                          boolean idle = view == null || view.isShown();
                  
                          if (idle && resourceCallback != null) {
                              resourceCallback.onTransitionToIdle();
                          }
                  
                          return idle;
                      }
                  
                      @Override
                      public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {
                          this.resourceCallback = resourceCallback;
                      }
                  
                      @Override
                      public String getName() {
                          return this + viewMatcher.toString();
                      }
                  
                      private static View getView(Matcher<View> viewMatcher) {
                          try {
                              ViewInteraction viewInteraction = onView(viewMatcher);
                              Field finderField = viewInteraction.getClass().getDeclaredField("viewFinder");
                              finderField.setAccessible(true);
                              ViewFinder finder = (ViewFinder) finderField.get(viewInteraction);
                              return finder.getView();
                          } catch (Exception e) {
                              return null;
                          }
                      }
                  }
                  

                  然后,您可以创建一个等待您的视图的辅助方法:

                  Then, you could create a helper method waiting for your view:

                  public void waitViewShown(Matcher<View> matcher) {
                      IdlingResource idlingResource = new ViewShownIdlingResource(matcher);///
                      try {
                          IdlingRegistry.getInstance().register(idlingResource);
                          onView(matcher).check(matches(isDisplayed()));  
                      } finally {
                          IdlingRegistry.getInstance().unregister(idlingResource);
                      }    
                  }
                  

                  最后,在你的测试中:

                  @Test
                  public void someTest() {
                      waitViewShown(withId(R.id.<some>));
                  
                      //do whatever verification needed afterwards    
                  } 
                  

                  您可以通过让 IdlingResource 等待任何条件来改进此示例,而不仅仅是等待可见性条件.

                  You could improve this example by making IdlingResource wait for any condition, not just for the visibility one.

                  这篇关于是否可以使用 Espresso 的 IdlingResource 等到某个视图出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)
                    <tbody id='1tcze'></tbody>

                  <small id='1tcze'></small><noframes id='1tcze'>

                          <legend id='1tcze'><style id='1tcze'><dir id='1tcze'><q id='1tcze'></q></dir></style></legend>

                            <bdo id='1tcze'></bdo><ul id='1tcze'></ul>
                            <tfoot id='1tcze'></tfoot>

                            <i id='1tcze'><tr id='1tcze'><dt id='1tcze'><q id='1tcze'><span id='1tcze'><b id='1tcze'><form id='1tcze'><ins id='1tcze'></ins><ul id='1tcze'></ul><sub id='1tcze'></sub></form><legend id='1tcze'></legend><bdo id='1tcze'><pre id='1tcze'><center id='1tcze'></center></pre></bdo></b><th id='1tcze'></th></span></q></dt></tr></i><div id='1tcze'><tfoot id='1tcze'></tfoot><dl id='1tcze'><fieldset id='1tcze'></fieldset></dl></div>