gpt4 book ai didi

java - 使用 Espresso 测试工具栏标题文本 - Android

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:28:39 34 4
gpt4 key购买 nike

我有两个 fragment 附加到一个 Activity ,并且想测试该 Activity 的工具栏标题。

所以我这样写:

@Test
public void testToolbar(){
onView(allOf(instanceOf(TextView.class),withParent(withId(R.id.toolbar))))
.check(matches(withText("Είσοδος/Εγγραφή")));

}

但是,无法识别我放入 withText 方法中的文本。

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with text: is "Είσοδος/Εγγραφή"' doesn't match the selected view.
Expected: with text: is "Είσοδος/Εγγραφή"

这是我的 Activity 代码。

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();
}
}

还有对应的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>

这是怎么回事?一切都在那里。

谢谢,西奥。

最佳答案

onView(withId(R.id.toolbar)).check(matches(hasDescendant(withText("Title"))));

以防你有没有内部 TextView 的简单工具栏。

关于java - 使用 Espresso 测试工具栏标题文本 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39229458/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com