Dra*_*ake 6 android android-tabs android-espresso
如何在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>
Run Code Online (Sandbox Code Playgroud)
我应该使用什么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}"
Run Code Online (Sandbox Code Playgroud)
你真的需要为我们添加代码来给出正确的答案.我在这里猜测你在这个例子的方式中使用了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);
}
}
Run Code Online (Sandbox Code Playgroud)
我在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;
}
Run Code Online (Sandbox Code Playgroud)
现在它变得有趣了,因为这样我们就可以在这些注入的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;
}
Run Code Online (Sandbox Code Playgroud)
如果仅使用图标,则还可以在ImageView上设置ID.
和Espresso代码点击这些标签:
Espresso.onView(ViewMatchers.withTagValue(Matchers.is((Object)"tab1"))).perform(ViewActions.click());
Run Code Online (Sandbox Code Playgroud)
如果您使用ID,则需要在某处定义这些ID.使用带有一些ID定义的简单Android资源文件.
视图ID资源文件,名为/values/ids.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="tab1" type="id" />
</resources>
Run Code Online (Sandbox Code Playgroud)
适应的助手:
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;
}
Run Code Online (Sandbox Code Playgroud)
如果仅使用图标,则还可以在ImageView上设置ID.
和Espresso代码点击这些标签:
Espresso.onView(ViewMatchers.withId(R.id.tab1)).perform(ViewActions.click());
Run Code Online (Sandbox Code Playgroud)
为什么你首先使用这个TabHost?请注意,此类目前已弃用.一个与标签ViewPager或在动作条可能取决于你的使用情况更好的选择.
在这种情况下,第一个问题通常是找到合适的视图.为此,请使用" 视图层次结构"工具.它是android SDK的一部分,位于tools目录中.
您可以从命令行启动它,如下所示:
cd ANDROID_SDK_LOCATION/tools
hierarchyviewer
Run Code Online (Sandbox Code Playgroud)
或使用Android Studio菜单:工具> Android> Android设备监视器.然后从设备监视器的菜单中打开Hierarchy View透视图:Window> Open Perspective> Hierarchy View.
我更喜欢第一个选项,因为设备监视器对我们的意图做得太多了.
现在将布局视图与"视图属性"视图结合使用,以查找所需视图的ID和标记.
有关此工具的一些说明:http://developer.android.com/tools/debugging/debugging-ui.html
| 归档时间: |
|
| 查看次数: |
5538 次 |
| 最近记录: |