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



我正在尝试配置我的 React Native 应用程序的 e2e 测试。我使用的 RN 版本是 0.56。不幸的是,Detox 在 Android 上不支持此版本,我决定在等待 Detox 官方支持的同时直接暂时实施浓缩咖啡测试。

我按照有关为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()));
}

。并在我正在测试的视图中添加了 { 可访问性:真,可访问性标签:id }。

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

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

我自己刚开始在 React Native 中使用 Espresso,但这里可能需要另一个注释:

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

相关内容

最新更新