• <tfoot id='PDlBT'></tfoot>

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

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

        <bdo id='PDlBT'></bdo><ul id='PDlBT'></ul>
      1. 是否可以使用 Espresso 的 IdlingResource 等到某个视图

        时间:2023-10-01
        • <bdo id='xptSv'></bdo><ul id='xptSv'></ul>

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

                  <legend id='xptSv'><style id='xptSv'><dir id='xptSv'><q id='xptSv'></q></dir></style></legend>
                • 本文介绍了是否可以使用 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 等到某个视图出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何正确设置 Java/Selenium 配置以运行自动化测试 下一篇:如何从 Selenium 的 gmail 收件箱中单击特定的电子邮

                  相关文章

                  最新文章

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

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

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