The*_*heo 7 java android android-studio android-espresso
我有两个片段附加到一个活动,并希望测试该活动的工具栏标题.
所以我写了这个:
@Test
public void testToolbar(){
onView(allOf(instanceOf(TextView.class),withParent(withId(R.id.toolbar))))
.check(matches(withText("???????/???????")));
}
Run Code Online (Sandbox Code Playgroud)
但是,我用inText方法放入的文本无法识别.
android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with text: is "???????/???????"' doesn't match the selected view.
Expected: with text: is "???????/???????"
Run Code Online (Sandbox Code Playgroud)
这是我的活动代码.
public class MainActivity extends AppCompatActivity {
public static final String LOGIN_FRAGMENT = "LOGIN_FRAGMENT";
public static final String REGISTER_FRAGMENT = "REGISTER_FRAGMENT";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("E??????/???????");
LoginFragment loginFragment = new LoginFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(android.R.id.content,loginFragment,LOGIN_FRAGMENT);
fragmentTransaction.commit();
}
public void userReg(View view){
RegisterFragment regFragment = new RegisterFragment();
FragmentManager fragmentManager1 = getSupportFragmentManager();
FragmentTransaction fragmentTransaction1 = fragmentManager1.beginTransaction();
fragmentTransaction1.addToBackStack("added");
fragmentTransaction1.replace(android.R.id.content,regFragment,REGISTER_FRAGMENT);
fragmentTransaction1.commit();
}
}
Run Code Online (Sandbox Code Playgroud)
它是相应的xml文件.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="team.football.ael.MainActivity"
>
<include
android:id="@+id/toolbar"
layout="@layout/tool_lay"
/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
这是怎么回事?一切都在那里.
谢谢,西奥.
小智 11
onView(withId(R.id.toolbar)).check(matches(hasDescendant(withText("Title"))));
Run Code Online (Sandbox Code Playgroud)
如果您有没有内部TextView的简单工具栏。
我假设你tool_layout看起来像这样:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="?colorPrimary"
android:minHeight="?actionBarSize"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:navigationContentDescription="@string/abc_action_bar_up_description"
app:navigationIcon="?homeAsUpIndicator"
app:title="">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/app_name"/>
</android.support.v7.widget.Toolbar>
Run Code Online (Sandbox Code Playgroud)
这是测试:
onView(withId(R.id.toolbar)).check(matches(isDisplayed()));
onView(withText(R.string.app_name)).check(matches(withParent(withId(R.id.toolbar))));
Run Code Online (Sandbox Code Playgroud)
希望它会有所帮助
| 归档时间: |
|
| 查看次数: |
4998 次 |
| 最近记录: |