代码之家  ›  专栏  ›  技术社区  ›  Real Lau

Espresso-自定义查找视图未捕获NoMatchingViewException-无IdlingResource

  •  1
  • Real Lau  · 技术社区  · 8 年前

    在这里,我想要一个自定义的查找视图函数:如果给定的匹配器在10秒内找不到视图,请返回布尔值false,否则返回视图。注意,我不想使用IdlingResource。我的代码如下:

    AiFinder。Java语言

    package BaseOpration;
    import android.support.test.espresso.NoMatchingViewException;
    import android.support.test.espresso.ViewAssertion;
    import android.support.test.espresso.ViewInteraction;
    import android.view.View;
    
    import org.hamcrest.Matcher;
    import static android.support.test.espresso.Espresso.onView;
    
    /**
     * Created by Real on 2018/3/14.
     */
    
    public class AiFinder {
    
        public static Object findElement(Matcher<View> matcher){
            int times = 0;
            while(times < Configer.timeOut){
                try{
                    return onView(matcher);
                }
                catch (NoMatchingViewException ex){
                    try {
                        System.out.printf("Retrying...");
                        Thread.sleep(1000);
                    } catch (Exception e) {
    //                    e.printStackTrace();
                        System.out.printf("111");
                    }
                    times += 1;
                }
            }
            return false;
        }
    
        public static void checkMatch(Object element, ViewAssertion assertion){
            if(element instanceof Boolean){
                try {
                    throw new Exception("Can not find the view");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            else{
                ViewInteraction e = (ViewInteraction) element;
                e.check(assertion);
            }
        }
    }
    

    和我的测试代码:

        @Test
        public void testLogin() throws Exception{
            onView(withId(R.id.tv_main_username)).perform(click());
            onView(withId(R.id.et_username)).perform(clearText(), replaceText("xxx"), closeSoftKeyboard());
            onView(withId(R.id.et_password)).perform(clearText(), replaceText("xxxx"), closeSoftKeyboard());
            onView(withId(R.id.bt_user_login)).perform(click());
            Object userName = AiFinder.findElement(withId(R.id.tv_main_username));
            AiFinder.checkMatch(userName, matches(withText("Hello")));
        }
    

    实际上,我看到它输入用户名和密码,然后单击登录按钮,它会给出输出:

    $ adb shell am instrument -w -r   -e debug false -e class **.**.AutomationEspressoTest#testLogin **.**.test/android.support.test.runner.AndroidJUnitRunner
    Client not ready yet..
    Started running tests
    
    android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: **.**:id/tv_main_username
    
    View Hierarchy:
    .............
    
    at dalvik.system.VMStack.getThreadStackTrace(Native Method)
    at java.lang.Thread.getStackTrace(Thread.java:595)
    at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:92)
    at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:56)
    at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184)
    at android.support.test.espresso.ViewInteraction.check(ViewInteraction.java:158)
    at BaseOpration.AiFinder.checkMatch(AiFinder.java:49)
    at com.hongfans.rearview.AutomationEspressoTest.testLogin(AutomationEspressoTest.java:63)
    at java.lang.reflect.Method.invoke(Native Method)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
    at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:27)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
    at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
    at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1904)
    
    Tests ran to completion.
    

    它不应该每隔1秒就找到视图吗?如果失败了(因为我关闭了网络),给我一个false标记,并引发异常(“找不到视图”)?希望有人能帮我(如果我把代码弄错了)。非常感谢。:)

    1 回复  |  直到 8 年前
        1
  •  1
  •   Anatolii    8 年前

    实际上,onView本身不会搜索视图,它只会准备一个ViewInteraction对象,可以由check和perform方法使用。在您的情况下,而不是:

    try {
        return onView(matcher);
    }
    ...
    

    可能是:

    try {
        onView(matcher).check(matches(isDisplayed());
        return true;
    }
    ...
    

    然而,更好的方法是使用空闲资源等待视图出现。请检查此项以了解更多信息- Android test with Espresso IdlingResource