【问题标题】:Espresso can't find a view: NoMatchingViewExceptionEspresso 找不到视图:NoMatchingViewException
【发布时间】:2016-08-27 07:43:42
【问题描述】:

在我的应用程序中,我在一个活动中附加了两个片段。每个片段包含不同的视图。

第一个有两个编辑文本字段和两个按钮,用户可以登录或注册。所以现在我想应用一些 Espresso 测试。

@RunWith(AndroidJUnit4.class)
public class LoginTest {
@Rule
public final ActivityRule<MainActivity> main = new ActivityRule<>   
(MainActivity.class);

@Test
public void shouldBeAbleToFindViewsOfLoginScreen(){
    onView(withText(R.id.user_name)).perform(click());
   }

 }

但是测试抱怨它找不到 ID 为 R.id.user_name 的视图。

android.support.test.espresso.NoMatchingViewException: No views in hierarchy    
found matching: with string from resource id: <2131493050>[user_name] value:    
false

我也使用从 uiautomatorviewer 中检查的正确 ID。

这个片段的xml代码是

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragmentLogin"
tools:context="team.football.ael.MainActivity"
android:background="@drawable/background">
<include

    android:id="@+id/toolbar"
    layout="@layout/tool_lay"

    />

<LinearLayout
    android:padding="20dp"
    android:background="@drawable/roundedlayout"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_width="wrap_content"
    android:orientation="vertical"
    android:layout_height="wrap_content">
    <ImageView
        android:src="@mipmap/ael_logo"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_gravity="center_horizontal" />
    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp">
    <EditText
        android:id="@+id/user_name"
        android:hint="@string/login_username"
        android:textColor="#fff"
        android:textColorHint="#fff"
        android:layout_width="250dp"
        android:layout_height="wrap_content" />
    </android.support.design.widget.TextInputLayout>
    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp">
    <EditText
        android:id="@+id/user_pass"
        android:textColorHint="#fff"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:layout_marginTop="20dp"
        android:hint="κωδικός"
        android:textColor="#fff"

        />
    </android.support.design.widget.TextInputLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/loginButton"
            android:layout_gravity="center_horizontal"
            android:text="Εισοδος"
            android:layout_marginTop="20dp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:textColor="#fff"
            android:background="@drawable/roundbutton"
            android:layout_height="wrap_content"
            android:onClick="userLogin"/>

        <Button
            android:layout_marginTop="20dp"
            android:layout_gravity="center_horizontal"
            android:text="Εγγραφη"
            android:textColor="#fff"

            android:layout_width="0dp"
            android:layout_marginLeft="10dp"

            android:background="@drawable/roundbutton"

            android:onClick="userReg"
            android:layout_weight="1"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <TextView
        android:textStyle="bold|italic"
        android:textSize="25sp"
        android:layout_gravity="center_horizontal"
        android:text="Ξέχασες τον κωδικό σου?"
        android:id="@+id/forgotPass"
        android:textColor="#fff"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

有什么想法吗?

谢谢。

【问题讨论】:

    标签: android android-testing ui-testing android-espresso


    【解决方案1】:

    回答

    变化:

          onView(withText(R.id.user_name)).perform(click());
    

          onView(withId(R.id.user_name)).perform(click());
    

    说明

    • withId()android:id 的匹配器
    • withText()android:text 的匹配器

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多