10000 fix(detox): fixed android connectivity issues (#635) · NativeScript/plugins@28edd9c · GitHub
[go: up one dir, main page]

Skip to content

Commit 28edd9c

Browse files
authored
fix(detox): fixed android connectivity issues (#635)
1 parent 6201be1 commit 28edd9c

File tree

9 files changed

+110
-39
lines changed

9 files changed

+110
-39
lines changed

packages/detox/README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,36 @@ module.exports = {
180180
> **Note:** A default NativeScript Android project uses 17 as the minimum SDK, but Detox requires >=21. Remove or modify the `minSdkVersion` in your `App_Resources/Android/app.gradle`.
181181
182182

183-
### Allow Local Networking (**iOS Only**)
183+
### Allow Local Networking
184+
185+
#### Android (Compulsory)
186+
187+
We have added a network security config file at `./res/xml/network_security_config.xml`, add it to the applications `AndroidManifest.xml` as below
188+
189+
```xml
190+
<!-- ./res/xml/network_security_config.xml -->
191+
<?xml version="1.0" encoding="utf-8"?>
192+
<network-security-config>
193+
<domain-config cleartextTrafficPermitted="true">
194+
<domain includeSubdomains="true">10.0.2.2</domain>
195+
<domain includeSubdomains="true">localhost</domain>
196+
</domain-config>
197+
</network-security-config>
198+
```
199+
200+
```xml
201+
<!-- ./res/xml/network_security_config.xml -->
202+
...existing code...
203+
204+
<application
205+
...existing attributes...
206+
android:networkSecurityConfig="@xml/network_security_config">
207+
...existing code...
208+
```
209+
210+
If you have an existing network security config, incorporate the configuration specified above into it.
211+
212+
#### iOS (Optional)
184213

185214
Dependending on your setup iOS may not be able to communicate with Detox off the bat. In that case, you need to add the following to your `Info.plist` file to allow for local networking requests.
186215

packages/detox/package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nativescript/detox",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "Simplifies usage of wix/Detox e2e tests in NativeScript apps!",
55
"main": "index",
66
"typings": "index.d.ts",
@@ -13,8 +13,7 @@
1313
},
1414
"nativescript": {
1515
"platforms": {
16-
"ios": "6.0.0",
17-
"android": "6.0.0"
16+
"android": "8.0.0"
1817
},
1918
"hooks": [
2019
{
@@ -32,7 +31,6 @@
3231
"NativeScript",
3332
"JavaScript",
3433
"TypeScript",
35-
"iOS",
3634
"Android",
3735
"e2e",
3836
"testing",

packages/detox/platforms/android/AndroidManifest.xml

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:usesCleartextTraffic="true">
4+
5+
<!-- Required to target the app under test -->
6+
<instrumentation
7+
android:name="androidx.test.runner.AndroidJUnitRunner"
8+
android:targetPackage="__PACKAGE__"
9+
android:label="Detox Tests" />
10+
11+
<application>
12+
<!-- No actual UI, but required to declare something in the test APK -->
13+
<uses-library android:name="android.test.runner" />
14+
</application>
15+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.facebook.react.modules.systeminfo;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class ReactNativeVersion {
7+
public static final Map<String, Object> VERSION;
8+
9+
static {
10+
VERSION = new HashMap<>();
11+
VERSION.put("major", 99); // Arbitrary placeholder values
12+
VERSION.put("minor", 0);
13+
VERSION.put("patch", 0);
14+
VERSION.put("prerelease", null);
15+
}
16+
}
Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.tns;
22

3+
import com.tns.NativeScriptActivity;
4+
35
import com.wix.detox.Detox;
6+
import com.wix.detox.config.DetoxConfig;
47

58
import org.junit.Rule;
69
import org.junit.Test;
@@ -9,16 +12,28 @@
912
import androidx.test.ext.junit.runners.AndroidJUnit4;
1013
import androidx.test.filters.LargeTest;
1114
import androidx.test.rule.ActivityTestRule;
15+
import android.util.Log;
1216

1317
@RunWith(AndroidJUnit4.class)
1418
@LargeTest
1519
public class DetoxTest {
1620

17-
@Rule
18-
public ActivityTestRule<com.tns.NativeScriptActivity> mActivityRule = new ActivityTestRule<>(com.tns.NativeScriptActivity.class, false, false);
21+
@Rule
22+
public ActivityTestRule<NativeScriptActivity> mActivityRule = new ActivityTestRule<NativeScriptActivity>(
23+
NativeScriptActivity.class, false, false);
24+
25+
@Test
26+
public void runDetoxTests() {
27+
DetoxConfig detoxConfig = new DetoxConfig();
28+
detoxConfig.idlePolicyConfig.masterTimeoutSec = 90;
29+
detoxConfig.idlePolicyConfig.idleResourceTimeoutSec = 60;
30+
detoxConfig.rnContextLoadTimeoutSec = getRuntimeTimeout();
31+
Log.i("Detox", "🚀 Starting Detox tests...");
32+
Detox.runTests(mActivityRule, detoxConfig);
33+
Log.i("Detox", "✅ Detox.runTests called");
34+
}
1935

20-
@Test
21-
public void runDetoxTests() {
22-
Detox.runTests(mActivityRule);
23-
}
24-
}
36+
private static int getRuntimeTimeout() {
37+
return "debug".equalsIgnoreCase(System.getenv("E2E_MODE")) ? 180 : 60;
38+
}
39+
}

packages/detox/platforms/android/androidTest/java/com/tns/DetoxTestRunner.java

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
repositories {
2-
maven { url "$rootDir/../../node_modules/detox/Detox-android"}
2+
maven { url = project.file('../../../node_modules/detox/Detox-android') }
33
}
44

55
dependencies {
@@ -8,15 +8,24 @@ dependencies {
88
}
99

1010
android {
11+
def testBase = project.file('../../../node_modules/@nativescript/detox/platforms/android/androidTest')
12+
1113
defaultConfig {
12-
minSdkVersion 18
13-
sourceSets {
14-
androidTest.setRoot("${project.rootDir}/../../node_modules/@nativescript/detox/platforms/android/androidTest")
15-
androidTest {
16-
java.srcDirs = ["${project.rootDir}/../../node_modules/@nativescript/detox/platforms/android/androidTest/java"]
17-
}
18-
}
1914
testBuildType System.getProperty('testBuildType', 'debug')
20-
testInstrumentationRunner 'com.tns.DetoxTestRunner'
15+
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
16+
}
17+
18+
sourceSets {
19+
androidTest {
20+
java.srcDirs += ["${testBase}/java"]
21+
manifest.srcFile "${testBase}/AndroidManifest.xml"
22+
}
23+
}
24+
25+
buildTypes {
26+
debug {
27+
minifyEnabled false
28+
shrinkResources false
29+
}
2130
}
2231
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<network-security-config>
3+
<domain-config cleartextTrafficPermitted="true">
4+
<domain includeSubdomains="true">10.0.2.2</domain>
5+
<domain includeSubdomains="true">localhost</domain>
6+
</domain-config>
7+
</network-security-config>

0 commit comments

Comments
 (0)
0