Android 为以编程方式创建的编辑文本执行键入文本

Android 为以编程方式创建的编辑文本执行键入文本,android,unit-testing,junit,android-espresso,Android,Unit Testing,Junit,Android Espresso,我是使用浓缩咖啡的新手,现在我正在为我的应用程序开发自动化测试 我的布局如下所示,在id为(R.id.parameter_布局)的线性布局中,我以编程方式创建和膨胀文本作为子视图进行编辑 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android

我是使用浓缩咖啡的新手,现在我正在为我的应用程序开发自动化测试

我的布局如下所示,在id为(R.id.parameter_布局)的线性布局中,我以编程方式创建和膨胀文本作为子视图进行编辑

 <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(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: ");
            }
        };
    }

请告知我遗漏的内容,并感谢您的帮助。

在您的日志中,它写道:

原因:java.lang.RuntimeException:将不执行操作,因为目标视图与一个或多个 以下限制: ((在屏幕上显示给用户)和(支持输入法或可从class:class分配) android.widget.SearchView)

目标视图:“线性布局{id=2131558549,res name=parameter_布局,可见性=可见,宽度=1208,高度=378, has focus=true,has focusable=true,has window focus=true, 可点击=错误,启用=正确,聚焦=错误, 可聚焦=假,布局请求=假,选择=假, 根是布局请求=false,输入连接=false,x=0.0, y=20.0,子计数=2}”

似乎您正在
LinearLayout
上执行
typeText
作为
目标视图

尝试使用想法获取您的
编辑文本
,然后执行
键入文本(…)

公共静态匹配器nthChildOf(最终匹配器parentMatcher,最终int childPosition){
返回新的TypeSafeMatcher(){
@凌驾
公共无效说明(说明){
description.appendText(“带“+childPosition+”parentMatcher类型的子视图”);
}
@凌驾
公共布尔匹配安全(视图){
if(!(view.getParent()实例视图组)){
返回parentMatcher.matches(view.getParent());
}
ViewGroup=(ViewGroup)view.getParent();
返回parentMatcher.matches(view.getParent())和&group.getChildAt(childPosition).equals(view);
}
};

谢谢你的帮助,这对我帮助很大:)

我把这个答案放在这里,给可能面临同样问题的人。 我按照John的建议将代码放在上面,并按如下方式使用:

onView(nthChildOf(withId(R.id.parameter_layout),0)).perform(typeText("aaa"), closeSoftKeyboard());
我可以获得编辑文本并正确执行键入文本

 >  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)
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);
  }
};
onView(nthChildOf(withId(R.id.parameter_layout),0)).perform(typeText("aaa"), closeSoftKeyboard());