这就是espresso转储视图层次结构的方式:
final ViewInteraction textView = onView(
allOf(
withParent(withId(R.id.main_toolbar)),
withParentIndex(1)
)
);
// Test fails!!! with withParentIndex(1)
textView.check(matches(withText("MobiAd")));
令人困惑的是,视觉上汉堡包菜单位于第一位,App-Title位于第二位。
我在以下测试中苦苦挣扎:
withParentIndex(0)
以上示例适用于private List<string> ListReferencedAssemblies()
{
List<string> refList = new List<string>();
var assemblies = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
foreach (var assembly in assemblies)
{
refList.Add(assembly.Name);
}
return refList;
}
有没有人知道UI-Automator和espresso之间的区别是错误还是有原因?
答案 0 :(得分:0)
我做了一个小样本应用程序,我想我已经知道发生了什么。
您正在测试的应用可能有一个由setSupportActionBar()
设置的自定义工具栏。
由于您的应用程序有一个导航抽屉,汉堡包按钮将显示在您的自定义工具栏上,显然(如布局检查员所示)修改其层次结构。
例如:
<android.support.v7.widget.Toolbar
android:id="@+id/R.id.main_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#c13030">
<!--This textview exists at index 0-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MobiAd"/>
</android.support.v7.widget.Toolbar>
在您的活动中,您可能会发现以下内容:
Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar);
//here is the magic
setSupportActionBar(toolbar);
当显示导航抽屉按钮时,工具栏上显示的是导航抽屉按钮的一部分,但是STILL,您的文本视图存在于索引0而不是1。
如果您想查看真正的层次结构,只需查看工具栏中的内容(找到声明R.id.main_toolbar
的布局文件)。
建议:为什么要通过查看索引来查找文本?如果它将被移动到另一个索引,您将不得不重构您的测试。
您可以使用其字符串资源键(而不是写几行,您可以在一行代码中进行简单的检查):onView(withText(R.string.toolbar_title)).check(matches(isDisplayed()));