Open jevenson opened 4 years ago
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:
Do you think these solutions could work for you?
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 , 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.
I do have the same issue in all my apps.
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).
@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,
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.
I made a pul request, to display an error message when this error happen
https://github.com/NativeScript/nativescript-unit-test-runner/pull/51
Environment
Describe the bug && Reproduction I'm experiencing the same issue outlined in #4119.
Run
tns test android
withusesCleartextTraffic=false
, the test app shows "no reachable hosts". SettingusesCleartextTraffic=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