Skip to content

tns test android no reachable hosts #5167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jevenson opened this issue Nov 27, 2019 · 7 comments
Open

tns test android no reachable hosts #5167

jevenson opened this issue Nov 27, 2019 · 7 comments

Comments

@jevenson
Copy link

jevenson commented Nov 27, 2019

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
NativeScript/nativescript-unit-test-runner#30

package.json

@rosen-vladimirov
Copy link
Contributor

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?

@jevenson
Copy link
Author

jevenson commented Dec 4, 2019

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?

@rosen-vladimirov
Copy link
Contributor

@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.

@Unity-G
Copy link

Unity-G commented Dec 5, 2019

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).

@jevenson
Copy link
Author

jevenson commented Dec 5, 2019

@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.

@Fatme
Copy link
Contributor

Fatme commented Dec 7, 2019

@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.

@alexist
Copy link

alexist commented Jul 23, 2021

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

NativeScript/nativescript-unit-test-runner#51

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants