未找到Android Espresso片段根ID

时间:2018-02-06 15:34:50

标签: android android-fragments android-espresso

我刚遇到这个问题,我试图测试是否显示片段,但出于某种原因,我无法根据片段的根ID进行测试。这是代码:

MainActivity.java:

package com.gregspitz.simplefragmenttesttest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical"
     tools:context="com.gregspitz.simplefragmenttesttest.MainActivity">

    <fragment
         android:id="@+id/main_fragment_holder"
         android:name="com.gregspitz.simplefragmenttesttest.BasicFragment"
         android:layout_width="match_parent"
         android:layout_height="match_parent"/>

 </LinearLayout>

BasicFragment.java:

package com.gregspitz.simplefragmenttesttest;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class BasicFragment extends Fragment {


    public BasicFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_basic, container, false);
    }

}

fragment_basic.xml:

<LinearLayout 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:orientation="vertical"
              android:id="@+id/basic_fragment"
         tools:context="com.gregspitz.simplefragmenttesttest.BasicFragment">

    <TextView
        android:id="@+id/simple_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment"/>

</LinearLayout>

MainActivityTest.java:

package com.gregspitz.simplefragmenttesttest;

import android.support.test.rule.ActivityTestRule;

import org.junit.Rule;
import org.junit.Test;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static org.junit.Assert.*;

/**
 * Created by gregs on 2/6/2018.
 */
public class MainActivityTest {
    @Rule
    public ActivityTestRule<MainActivity> mActivityTestRule =
            new ActivityTestRule<MainActivity>(MainActivity.class);

    @Test
    public void mainActivity_basicFragmentIsDisplayedAtStart() {
        onView(withId(R.id.basic_fragment)).check(matches(isDisplayed()));
    }

    @Test
    public void mainActivity_basicFragmentInnerIdIsFound() {
        onView(withId(R.id.simple_text)).check(matches(isDisplayed()));
    }

}

第二个测试适用于我在布局中寻找ID,但第一个测试没有找到布局的根ID。我只是不明白为什么那不起作用,我该怎么做呢?谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

fragment在视图上膨胀时,Android框架会将fragment布局的根ID替换为fragment本身的ID。

LinearLayout的ID将更改为fragment的ID。

@Test public void mainActivity_basicFragmentIsDisplayedAtStart() { onView(withId(R.id.main_fragment_holder)).check(matches(isDisplayed())); }