2017-03-16 330 views
1

我是使用espresso的新手,現在我正在爲我的應用程序開發自動化測試。執行編輯文本創建的編輯文本

我有佈局按下面,用ID(R.id.parameter_layout)線性佈局內部,創建和膨脹編程編輯文本作爲子圖。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:orientation="vertical"> 

     <ScrollView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <LinearLayout 
       android:id="@+id/parameter_layout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="5dp" 
       android:layout_marginTop="5dp" 
       android:orientation="vertical"/> 
     </ScrollView> 

    </LinearLayout> 

我可以用下面的代碼獲取子視圖:

onView(withId(R.id.parameter_layout)).check(matches(withChildHint("areaPath"))); 

但是,當我試圖綁執行類型的文字像下面,我得到了錯誤。

onView(allOf(withId(R.id.parameter_layout),withChildHint("areaPath"))).perform(typeText("aaa"), closeSoftKeyboard()); 

,這是我withChildHint方法:

static Matcher<View> withChildHint(final String string) { 
     return new BoundedMatcher<View, LinearLayout>(LinearLayout.class) { 
      @Override 
      public boolean matchesSafely(LinearLayout view) { 
       View child = view.getChildAt(0); 
       if (child != null && child instanceof EditText) { 
        return ((EditText) child).getHint().toString().equals(string); 
       }` 
       return false; 
      } 

      @Override 
      public void describeTo(Description description) { 
       description.appendText("with child text: "); 
      } 
     }; 
    } 

螞蟻,這是錯誤,我得到:

> android.support.test.espresso.PerformException: Error performing 'type text(/axon-v3/portal/areas/1)' on view '(with id: com.greenwavesystems.axon.lib.testapp:id/parameter_layout and with child text:)'. 
       at android.support.test.espresso.PerformException$Builder.build(PerformException.java:83) 
       at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:70) 
       at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:53) 
       at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184) 
       at android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:115) 
       at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:87) 
       at com.greenwavesystems.axon.lib.testapp.MethodActivityTest.testMethod(MethodActivityTest.java:95) 
       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:1959) 
       Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints: 
       ((is displayed on the screen to the user) and (supports input methods or is assignable from class: class android.widget.SearchView)) 
       Target view: "LinearLayout{id=2131558549, res-name=parameter_layout, visibility=VISIBLE, width=1208, height=378, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=20.0, child-count=2}" 
       at android.support.test.espresso.ViewInteraction$1.run(ViewInteraction.java:138) 
       at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423) 
       at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
       at android.os.Handler.handleCallback(Handler.java:739) 
       at android.os.Handler.dispatchMessage(Handler.java:95) 
       at android.os.Looper.loop(Looper.java:158) 
       at android.app.ActivityThread.main(ActivityThread.java:7229) 
       at java.lang.reflect.Method.invoke(Native Method) 
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

請告知我缺少什麼,真的很感激任何一種幫助。

回答

1

在你logcat的,它說:了java.lang.RuntimeException:

所致行動將不會被執行,因爲目標視圖不匹配的 以下約束的一種或多種: ((被顯示在屏幕上給用戶)和(支持輸入方法或可分配從類: 類android.widget.SearchView))

目標的觀點:「的LinearLayout {ID = 2131558549,RES-NAME = parameter_layout,能見度= VISIBLE,width = 1208,height = 378, has-focus = true,has-focusable = =真, 被點擊的= false,則啓用真實的,具有窗口聚焦=真,則聚焦=假, 是-可聚焦= false,則佈局請求= false,則選擇=假, 根被佈局請求=假,具有輸入連接=假,X = 0.0, Y = 20.0,對兒童計數= 2}」

好象您正在作爲LinearLayout執行typeTexttarget view

嘗試this solution想法,讓您的EditText然後執行typeText(...)

public static Matcher<View> nthChildOf(final Matcher<View> parentMatcher, final int childPosition) { 
return new TypeSafeMatcher<View>() { 
    @Override 
    public void describeTo(Description description) { 
    description.appendText("with "+childPosition+" child view of type parentMatcher"); 
    } 

    @Override 
    public boolean matchesSafely(View view) { 
    if (!(view.getParent() instanceof ViewGroup)) { 
     return parentMatcher.matches(view.getParent()); 
    } 

    ViewGroup group = (ViewGroup) view.getParent(); 
    return parentMatcher.matches(view.getParent()) && group.getChildAt(childPosition).equals(view); 
    } 
}; 
1

感謝您的幫助,這真的幫助了我很多:)

我在這裏把這個答案,任何人這可能面臨同樣的問題。 我把代碼約翰上述建議,並如下使用:

onView(nthChildOf(withId(R.id.parameter_layout),0)).perform(typeText("aaa"), closeSoftKeyboard()); 

我可以編輯文本並正確執行類型的文本。