'Can't run tests for Android in the debug mode after change port

I want to create E2E tests for Android. I went through this guide.

  • run build with configaration - detox build -c android.emu.staging.debug -> get success build apk.
  • After it, run react-native start --port=8087 (Can't use default port, because it busy security programm McAfee)
  • Run test with configuration - detox test -c android.emu.staging.debug
  • After run test have error image (Tests have already started running and wait device.lauchApp() )
  • Then with Ctrl + M change bundle and set 10.0.2.2:8087 image
  • After it bundle start building -> Success build.
  • And then stuck on launchApp and get timeout error.

Tell please, how can I run test on different ports ?

I find the same issue https://github.com/wix/Detox/issues/3190 and set bundleInDebug: true helped me, but i think it's the wrong direction.

Your environment

Detox version: 17.14.3 React Native version: 0.66.0 Node version: 14.15.5 Device model: Android Emulator Pixel 2 API 30 OS: Windows Test-runner (select one): jest-circus / jest+jasmine / mocha / other jest

I try run detox debug test on other port, because of default port (8081) is busy by other program.



Solution 1:[1]

With help detox team got the following solution - https://github.com/wix/Detox/issues/3279

For me all work correct in debug mode.

Solution 2:[2]

I'll post the full answer here as well in the hopes that it may help someone out.

Locate your DetoxTest.java file, see guide and insert the following lines:

At the top of the class:

private static final String RN_DEBUG_HOST_KEY = "debug_http_host";

and inside the runDetoxTests method:

Context context = InstrumentationRegistry.getInstrumentation().getTargetContext().getApplicationContext();

SharedPreferences preferences =
        PreferenceManager.getDefaultSharedPreferences(context);
preferences.edit().putString(RN_DEBUG_HOST_KEY, "localhost:8087").apply();

DetoxConfig detoxConfig = new DetoxConfig();
detoxConfig.rnContextLoadTimeoutSec = 500;

and finally pass the detoxConfig as the third argument to runTests. E.g.:

Detox.runTests(rule, context, detoxConfig);

You will also have to reverse the port - you can do this either manually from the commandline, or by putting the following code inside the beforeAll of your tests:

beforeAll(async () => {
    await device.reverseTcpPort(8087);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user18619840
Solution 2 Jon