Testing React Native with UiAutomator on Android

Viewed 483

I am trying to perform a test with a react native app on Android using UiAutomator. I have started by transplanting the exact code from the Google example here: https://github.com/googlesamples/android-testing/tree/master/ui/uiautomator/BasicSample

Here is the setup code from the example:

@Before
public void startMainActivityFromHomeScreen() {
    // Initialize UiDevice instance
    mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

    // Start from the home screen
    mDevice.pressHome();

    // Wait for launcher
    final String launcherPackage = mDevice.getLauncherPackageName();
    assertThat(launcherPackage, notNullValue());
    mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
            LAUNCH_TIMEOUT);

    // Launch the app
    Context context = InstrumentationRegistry.getContext();
    final Intent intent = context.getPackageManager()
            .getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE);
    // Clear out any previous instances
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    context.startActivity(intent);

    // Wait for the app to appear
    mDevice.wait(Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)),
            LAUNCH_TIMEOUT);
}

This works fine in that it launches the app, but when it reaches the mDevice.wait() I get an NPE:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.view.accessibility.AccessibilityNodeInfo.isVisibleToUser()' on a null object reference

Similarly, when I replace the wait with my own alternative, I also get an NPE:

   mDevice.wait(Until.hasObject(By.desc("get started")), LAUNCH_TIMEOUT);

I saw a couple of suggestions in this question, but they made no difference.

Is there something inherent in react native that would cause this issue?

0 Answers
Related