Android,具有多个 Activity 的 EspressoTesting

标签 android android-activity android-espresso

我想在我的仪器测试中访问多个 Activity 。 例如登录-> 搜索-> 列表-> 详细 Activity

我已经达到“列表 Activity ”,但我想去列表 Activity 元素 [1] 的详细信息页面。

下面是我的代码

    @RunWith(AndroidJUnit4.class)
     public class ContactSearchScreeenTest  extends  ActivityInstrumentationTestCase2<ContactSearchScreen> {

     public ContactSearchScreeenTest() {
        super(ContactSearchScreen.class);
    }

    @Rule
    public ActivityTestRule<ContactSearchScreen> mActivityRule =
            new ActivityTestRule<>(ContactSearchScreen.class);

    @Override
    protected void setUp() throws Exception {
        super.setUp();
    }

    @Test
    public void sendToSearchResultActivity()
    {

        onView(withId(R.id.etSearchName))
                      .perform(typeText("ssasa"), pressKey(KeyEvent.KEYCODE_SEARCH));


       GlobalClass globalVariable = (GlobalClass) mActivityRule.getActivity().getApplicationContext();
        globalVariable.setSearchStr("ssasa");

        mActivityRule.getActivity().callForNextSearchActivity();

    }

}

附加功能

    @Override
    public void callForNextSearchActivity() {
     Intent intent = new Intent(getBaseContext(),  SearchResultsActivity.class);
    final GlobalClass globalVariable = (GlobalClass) getApplicationContext();
    globalVariable.setSearchStr(getSearchStringFromSearchEditText());
    startActivity(intent);
    overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);

}

在 Espresso 测试中是否可以有多个 Activity 层? 如果是..如何?

最佳答案

是的,这是可能的。在其中一个示例中,他们对此进行了演示。

https://code.google.com/p/android-test-kit/source/browse/testapp_test/src/main/java/com/google/android/apps/common/testing/ui/testapp/BasicTest.java#52][1]

public void testTypingAndPressBack() {
// Close soft keyboard after type to avoid issues on devices with soft keyboard.
onView(withId(R.id.sendtext_simple))
    .perform(typeText("Have a cup of Espresso."), closeSoftKeyboard());

onView(withId(R.id.send_simple))
    .perform(click());

// Clicking launches a new activity that shows the text entered above. You don't need to do
// anything special to handle the activity transitions. Espresso takes care of waiting for the
// new activity to be resumed and its view hierarchy to be laid out.
onView(withId(R.id.display_data))
    .check(matches(withText(("Have a cup of Espresso."))));

// Going back to the previous activity - lets make sure our text was perserved.
pressBack();

onView(withId(R.id.sendtext_simple))
    .check(matches(withText(containsString("Espresso"))));
}

阅读内联评论。

关于Android,具有多个 Activity 的 EspressoTesting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37656709/

相关文章:

Android - 添加子菜单时应用程序崩溃

android - 奇怪的 NullpointerException android.content.ComponentName

Android Espresso IdlingResources 和 fragment/Activity 转换

java - 当我添加意式 Espresso 时出现资源错误

Android Activity 返回堆栈导航问题

java - Espresso 'is enabled' 与所选 View 不匹配

java - Google 如何验证 API 调用中的 SHA1 和程序包名称?

android - onAccessibilityEvent 中的 getRootInActiveWindow() 始终为 null

android - 在协程内并行运行两个 Kotlin 协程

java - 在基 Activity 类中初始化公共(public) View