ESPRESSOのタブを開く方法
-
21-12-2019 - |
質問
ESPRESSOテストでタブを開く方法は?Espresso.onView(ViewMatchers.withId(R.id.practice_results_tab)).perform(ViewActions.click());
をやろうとしましたが、それは機能しません。そのコードでは、このタブのレイアウトを開きました。
XMLファイルがあります:
<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>
.
タブを開くにはどのIDを使うべきですか?
LogCATのエラー:
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/ < / a>
https://github.com/hanscappelle/so-25016397 < / a>
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/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;
}
.
今度は興味深い原因このようにして、これらの注入されたView
オブジェクトにIDまたはタグを簡単に設定し、これらをESPRESSOで使用できます。
解決策:ビュー
タグ各ビューヘルパーコードのタグを受け入れるようにそのヘルパーを適応させると、次のようになります。
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;
}
.
アイコンのみを使用する場合は、ImageViewでIDを設定できます。
とESPRESSOコードをクリックしてこれらのタブをクリック:
Espresso.onView(ViewMatchers.withTagValue(Matchers.is((Object)"tab1"))).perform(ViewActions.click());
.
代替解決策:ビューIDを使用する
IDの場合は、これらのIDをどこかに定義する必要があります。単純なAndroidリソースファイルをいくつかのID定義で使用してください。
View IDリソースファイル、/values/ids.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="tab1" type="id" />
</resources>
.
適応ヘルパー:
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;
}
.
アイコンのみを使用する場合は、ImageViewでIDを設定できます。
とESPRESSOコードをクリックしてこれらのタブをクリック:
Espresso.onView(ViewMatchers.withId(R.id.tab1)).perform(ViewActions.click());
.
タブホストについて一般的な
なぜあなたはそもそもこのタブホストを使ったのですか?このクラスは今では廃止予定されています。 a viewer_"/lateal.html"> viewpagerタブまたはアクションバーは、ユースケースに応じてオプションが良い場合があります。
ViewHierarchyツール
を使うこのような場合、最初の問題は適切なビューを見つけることが多いです。このために View hierarchy ツールを使用します。それはAndroid SDKの一部であり、Toolsディレクトリに存在します。
このようなコマンドラインから起動できます。
cd ANDROID_SDK_LOCATION/tools
hierarchyviewer
.
または Android Studio メニュー:Tools> Android> Androidデバイスモニタ。次に、デバイスモニタのメニューから階層ビューのパースペクティブを開きます。ウィンドウ> [パースペクティブ]> [階層ビュー]
私はデバイスモニタが私たちの意図のために多すぎるという理由だけで最初のオプションを好みます。
[表示プロパティ]ビューと組み合わせてレイアウトビューを使用して、必要なビューのIDとタグを見つけます。
このツールのいくつかの説明:>>>> http://developer.android.com/ツール/デバッグ/ 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です。そして、あなたが「買い物」と呼ばれるタブを持っていたら、それをタブテキストとして渡します。
onview( "text("あなたのタブラベル "))。実行(())
カスタムインジケータメソッドを使用せずにタブテキストにタグを設定することもできます。これは、作成後にTabのTextView
にアクセスすることで行います。
HCPLの例のコードからMainActivity
を使用すると、これが次のようになります。
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");
}
}
.
あなたのespressoテストでは、このようなタブにアクセスできます。
onView(withTagValue(Matchers.is((Object)"TAG_TAB_TWO"))).perform(click());
.