使用Espresso测试React Native应用分步说明

时间:2018-08-15 11:32:27

标签: android react-native android-espresso

我正在尝试配置我的React Native应用程序的端到端测试。我正在使用的RN版本是0.56。不幸的是Detox在Android上不支持此版本,因此我决定在等待Detox官方支持时直接实施临时Espresso测试。

我遵循instructions设置了Android应用程序的Espresso。幸运的是,其中大多数已由Detox添加。我还在android / app / src / androidTest / java / [com / package / name /] MainActivityTest.java中创建了一个测试文件。这是它的上下文:

package com.package.name;

import android.support.test.espresso.ViewAssertion;
import android.support.test.espresso.ViewInteraction;
import android.support.test.espresso.assertion.ViewAssertions;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription;
import static org.hamcrest.Matchers.allOf;

@LargeTest
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {

    @Rule
    public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);

    @Test
    public void mainActivityTest() {
        try {
            Thread.sleep(1500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        ViewInteraction view = onView(allOf(withContentDescription("onboardingScreen"), isDisplayed()));

        ViewInteraction button = onView(allOf(withContentDescription("login"), isDisplayed()));

        button.perform(click());

        onView(allOf(withContentDescription("currentMealPlanTab"), isDisplayed()));

}

......,并在我正在测试的视图中添加了{accessable:true,accessibilityLabel:id}。

当我从Android Studio或通过发出“ ./gradlew connectedAndroidTest”运行测试时,得到java.lang.RuntimeException:找不到活动。您是否忘了通过调用getActivity()启动活动。

我无法找到有关如何为React Native设置Espresso的分步指南。请让我知道我在做什么错以及如何进行设置。

1 个答案:

答案 0 :(得分:0)

我自己就开始在React Native中使用Espresso,但是您可能需要在此处添加另一个注释:

    @Rule
    @JvmField
    public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);