nativescript-cli icon indicating copy to clipboard operation
nativescript-cli copied to clipboard

tns test android no reachable hosts

Open jevenson opened this issue 5 years ago • 7 comments

Environment

  • CLI: 6.2.2
  • Cross-platform modules: 6.2.1
  • Android Runtime: 6.2.0
  • iOS Runtime: n/a
  • Plugin(s):

Describe the bug && Reproduction I'm experiencing the same issue outlined in #4119.

Run tns test android with usesCleartextTraffic=false, the test app shows "no reachable hosts". Setting usesCleartextTraffic=true allows the tests to execute.

I don't want to set usesCleartextTraffic to true with my production application, so I'm wondering how I get around this.

Expected behavior I expect the tests to run 😄

Additional context This was supposed to be resolved with this pull request. I see that there is an AndroidManifest.xml file in those changes, and I'm wondering if that is supposed to override the settings in my application's manifest https://github.com/NativeScript/nativescript-unit-test-runner/pull/30

package.json

jevenson avatar Nov 27 '19 17:11 jevenson

Hey @jevenson , When running unit tests on Android API Level 28 or above, you need to set usesCleartextTraffic to true, that's the only way for the moment as the connection from your application is to your local machine. The AndroidManifest.xml file in the nativescript-unit-test-runner is used when you build the application, but the changes in your application's AndroidManifest.xml override the ones from the nativescript-unit-test-runner. There are two options here:

  1. Run unit tests on API level 27 or below.
  2. Include some script on your side that enables usesCleartextTraffic setting in your AndroidManifest.xml only when running the unit tests.

Do you think these solutions could work for you?

rosen-vladimirov avatar Dec 02 '19 10:12 rosen-vladimirov

I'll try one of those two options, thanks for the reply.

Is the goal to allow running unit tests with usesClearTextTraffic set to false?

jevenson avatar Dec 04 '19 15:12 jevenson

@jevenson , the idea is to include a script on your side that sets usesCleartextTraffic to true and then runs the tests, for example in Node.js script you can do something like:

const path = require("path");
const fs = require("fs");
const childProcess = require("child_process");
const os = require("os");
const pathToAndroidManifestInAppResources = path.join(__dirname, "app", "App_Resources", "Android", "src", "main", "AndroidManifest.xml");

const currentContent = fs.readFileSync(pathToAndroidManifestInAppResources).toString();
const newContent = currentContent.replace('android:usesCleartextTraffic="false"', 'android:usesCleartextTraffic="true"');
fs.writeFileSync(pathToAndroidManifestInAppResources, newContent);

const tnsExecutable = os.platform() === "win32" ? "tns.cmd" : "tns";
try {
    const result = childProcess.spawnSync(tnsExecutable, ["test", "android", "--justlaunch"], { stdio: "inherit" });
    console.log(result);
} finally {
    fs.writeFileSync(pathToAndroidManifestInAppResources, currentContent);
}

Currently it is not possible to run the test on API level 28 or above without this setting and we do not have plans to research other ways to achieve this.

rosen-vladimirov avatar Dec 04 '19 16:12 rosen-vladimirov

I do have the same issue in all my apps.

  • tns --version: 6.3.0-2019-12-02-135206-14026
  • tns-core-modules: 6.1.1 and 6.2.3
  • tns-android: 6.1.0 and 6.2.0

I cloned the following demo project: https://github.com/VS-work/ns-tests-demo And it has the same issues.

Adding usesCleartextTraffic=false does not help for me (application & activity).

Unity-G avatar Dec 05 '19 01:12 Unity-G

@rosen-vladimirov Thanks for providing that script, that helps a lot.

I understand that research will not be done to enable test execution with the usesClearTextTraffic set to true. Would it be appropriate to include that provided script in the NativeScript CLI?

If it were included, I would imagine it would need to store the original value of the property and revert back to it after the test run completed.

jevenson avatar Dec 05 '19 03:12 jevenson

@jevenson,

An another option could be to install nativescript-unit-test-runner only for dev builds and uninstall it when running the production app. In such a case you should have the following inside package.json:

{
	"scripts": {
		"test": "npm i nativescript-unit-test-runner && tns test android"
		"run:prod": "npm un nativescript-unit-test-runner && tns run android --release --<keyStoreOptions>" 
	}
}

After that you can execute npm run test or npm run run:prod.

The downgrade of this approach is that the app will be rebuild every time when changing from test to run:prod or vice versa.

Fatme avatar Dec 07 '19 07:12 Fatme

I made a pul request, to display an error message when this error happen

https://github.com/NativeScript/nativescript-unit-test-runner/pull/51

alexist avatar Jul 23 '21 06:07 alexist