문제

을 열 수 있는 방법 탭에서 에스프레소 테스트?I tried to do Espresso.onView(ViewMatchers.withId(R.id.practice_results_tab)).perform(ViewActions.click());, 지만,지 않는다.에는 코드 개의 레이아웃을 이 탭을 누릅니다.가 XML file:

    <TabHost
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/practice_tabHost">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
            </TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                <LinearLayout
                    android:id="@+id/practice_settings_tab"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical">

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/practice_results_tab"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical">

                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>

What ID 를 사용해야 합 열 탭?

에 오류가 콘텐츠:

Caused by: java.lang.RuntimeException: Action will not be performed because the target     view does not match one or more of the following constraints:
at least 90 percent of the view's area is displayed to the user.
Target view: "LinearLayout{id=2131296384, res-name=practice_results_tab, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}"
도움이 되었습니까?

해결책

완료한 코드

당신이 정말로 코드를 추가해야를 위해 저희에게 적절한 대답합니다.난 여기 사용되는 TabHost 및 TabWidget 에서 방법으로 이 예제: https://maxalley.wordpress.com/2012/10/25/android-creating-a-tab-layout-with-tabhost-and-tabwidget/

내가 만든 샘플에서 프로젝트 https://github.com/hanscappelle/SO-25016397

귀하의 활동을 수 있는 다음과 같습니다:

public class MainActivity extends TabActivity {

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

        TabHost tabHost = getTabHost();

        // setNewTab(context, tabHost, tag, title, icon, contentID);
        this.setNewTab(this, tabHost, "tab1", R.string.textTabTitle1, R.drawable.ic_tab_settings, R.id.practice_settings_tab);
        this.setNewTab(this, tabHost, "tab2", R.string.textTabTitle2, R.drawable.ic_tab_results, R.id.practice_results_tab);

    }

    private void setNewTab(Context context, TabHost tabHost, String tag, int title, int icon, int contentID ){
        TabSpec tabSpec = tabHost.newTabSpec(tag);
        String titleString = getString(title);
        tabSpec.setIndicator(titleString, context.getResources().getDrawable(android.R.drawable.star_on));
        tabSpec.setContent(contentID);
        tabHost.addTab(tabSpec);
    }
}

내가 찾는 다른 코드를 들어에 https://maxalley.wordpress.com/2012/10/27/android-styling-the-tabs-in-a-tabwidget/ 도우미와 방법을 삽입 ImageView 에 대한 탭이 있습니다.

private View getTabIndicator(Context context, int title, int icon) {
        View view = LayoutInflater.from(context).inflate(R.layout.tab_layout, null);
        ImageView iv = (ImageView) view.findViewById(R.id.imageView);
        iv.setImageResource(icon);
        TextView tv = (TextView) view.findViewById(R.id.textView);
        tv.setText(title);
        return view;
    }

이제 재미있게 되는 원인이 방법으로 우리는 쉽게 설정할 수 있습 ID 또는 태그에서 이러한 주사 View 개체에 이들을 사용한 에스프레소입니다.

솔루션:태그 보기

는 경우에 적응하는 도우미를 받아들이 태그를 위한 각각기 도우미 코드는 아래와 같습니다:

private View getTabIndicator(Context context, int title, int icon, int viewId, String viewTag) {
        View view = LayoutInflater.from(context).inflate(R.layout.tab_layout, null);
        ImageView iv = (ImageView) view.findViewById(R.id.image_view);
        iv.setImageResource(icon);
        TextView tv = (TextView) view.findViewById(R.id.text_view);
        tv.setText(title);
        tv.setTag(viewTag);
        return view;
    }

만 사용하는 경우 아이콘을 설정할 수 있습니다 ID 에 또한 이미지 뷰.

및 에스프레스 코드를 클릭 탭:

Espresso.onView(ViewMatchers.withTagValue(Matchers.is((Object)"tab1"))).perform(ViewActions.click());

대체 솔루션:를 사용하여 보 Id

는 경우 이동에 대한 Id 야 할 이러한 Id 를 정의할 수 있다.사용한 간단한 안드로이드 리소스 파일과 함께 몇 가지 정의 ID.

View ID 리소스,파일 이름/values/ids.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="tab1" type="id" />
</resources>

적응 helper:

private View getTabIndicator(Context context, int title, int icon, int viewId, String viewTag) {
    View view = LayoutInflater.from(context).inflate(R.layout.tab_layout, null);
    ImageView iv = (ImageView) view.findViewById(R.id.image_view);
    iv.setImageResource(icon);
    TextView tv = (TextView) view.findViewById(R.id.text_view);
    tv.setText(title);
    tv.setId(viewId);
    return view;
}

만 사용하는 경우 아이콘을 설정할 수 있습니다 ID 에 또한 이미지 뷰.

및 에스프레스 코드를 클릭 탭:

Espresso.onView(ViewMatchers.withId(R.id.tab1)).perform(ViewActions.click());

에 대한 TabHosts 에서 일반

왜 당신이 사용하는 이 TabHost 에서 첫 번째 장소는?이 클래스는 사용되지 않습니다.A ViewPager 탭액션 바 수은 더 나은 옵션에 따라 귀하의 사용 사례입니다.

사용 ViewHierarchy 도구

이 같은 경우에는 첫 번째 문제는 종종을 찾기 위해 적절한 보기입니다.이를 위해 사용 계층 보기 도구입니다.그것은 부분의 android SDK 및 생활 도구의 디렉토리에 있습니다.

시작할 수 있습니다 그것은 명령행에서 다음과 같다:

cd ANDROID_SDK_LOCATION/tools
hierarchyviewer

거나 사용 안드로이드 스튜디오 메뉴도구>안드로이드>안드로이드 장치를 모니터링합니다.그때 열린 계층 보기 관점에서 메뉴에서 장치를 모니터링:창>열 관점>계층 보기입니다.

내가 선호하는 첫 번째 옵션은 단지 때문에 장치를 모니터링는 방법으로 너무 많은 우리의 기도가 있다.

지금 사용하여 레이아웃에서 뷰와 함께 볼 속성을 보을 찾 ID 와 태그의 보기를 원합니다.

일부의 지시에서 이구: http://developer.android.com/tools/debugging/debugging-ui.html

다른 팁

나를 위해 일하는 유일한 것은 다음과 같습니다.

public void clickOnTab(String tabText) {    
    Matcher<View> matcher = allOf(withText(tabText),
                                  isDescendantOfA(withId(R.id.ll_tab_container)));
    onView(matcher).perform(click());    
}
.

여기서 ll_tab_container는 내 사용자 정의 탭 레이아웃의 linearlayout입니다.그리고 "쇼핑"이라는 탭이 있으면이를 TabText로 전달합니다.

onview (탭 레이블 "))를 수행합니다. 수행 (클릭 ())

그것은 또한을 설정하려면 가능한 태그 탭에서 텍스트를 사용하지 않고 사용자 지정 표시기는 방법입니다.당신은에 액세스 탭 TextView 후에 그것을 만들었습니다.

를 사용하는 MainActivity 에서 hcpl 의 예제 코드는 다음과 같이 보일 것이다:

public MainActivity extends TabActivity {

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

        TabHost tabHost = getTabHost();

        // setNewTab(context, tabHost, tag, title, icon, contentID);
        this.setNewTab(this, tabHost, "tab1", R.string.textTabTitle1, R.drawable.ic_tab_settings, R.id.practice_settings_tab);
        this.setNewTab(this, tabHost, "tab2", R.string.textTabTitle2, R.drawable.ic_tab_results, R.id.practice_results_tab);

        // Set custom tag on first tab
        View tabView1 = tabHost.getTabWidget().getChildAt(0);
        TextView tabView1Text = (TextView) signUpTabView.findViewById(android.R.id.title);
        signUpTextTab.setTag("TAG_TAB_ONE");

        // Set custom tag on second tab
        View tabView2 = tabHost.getTabWidget().getChildAt(1);
        TextView tabView1Text = (TextView) signUpTabView.findViewById(android.R.id.title);
        signUpTextTab.setTag("TAG_TAB_TWO");
    }
}

그 다음에 에스프레소 테스트,당신은에 액세스할 수 있는 탭을 다음과 같다:

onView(withTagValue(Matchers.is((Object)"TAG_TAB_TWO"))).perform(click());
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top