1

Within a ReactNative android application, I want to detect a running appium service (an end to end server) so my app knows that it is currently running e2e.

From within my android emulator, to reach my localhost, considering Android emulator use localhost for it's own network interface, I have read that android developer use 10.0.2.2 to reach the host localhost.

This is what I have tried:


// iOS/Android e2e test run on Emulator/Simulator with an Appium server
let isE2e: boolean | null = null

export async function getIsE2e() {
  if (env.ENV === 'production') {
    isE2e = false
  }
  if (isE2e === true || isE2e === false) {
    return isE2e
  }
  try {
    const response = await fetch('http://10.0.2.2/status', {
      mode: 'cors',
      headers: new Headers({
        accept: 'application/json',
      }),
    })
    isE2e = response.ok
  } catch (error) {
    isE2e = false
  }
  return isE2e
}

I have the following error:

TypeError: Network request failed

I expect to have the following JSON result:

{"value":{"build":{"version":"2.0.0-beta.46","git-sha":"258938ef66a2a49a4a400554a6dce890226ae34c","built":"2020-03-05 23:13:56 -0800"}}}

I do not use any proxy, this is the configuration:

enter image description here

The appium server listen on 0.0.0.0:4723:

tcp        0      0 0.0.0.0:4723            0.0.0.0:*               LISTEN      26721/node          

It work fine if I want to reach another service such as a running webpack dev server running on 0.0.0.0:3000

tcp        0      0 0.0.0.0:3000            0.0.0.0:*               LISTEN      30217/node               

Why can't I contact the appium service ?

Dimitri Kopriwa
  • 13,139
  • 27
  • 98
  • 204
  • It is written in the question `From within my android emulator`. Yes `10.0.2.2` map to `localhost`, but why appium service is not reachable, while webpack dev server is, and both listen to the same iface `0.0.0.0` ? – Dimitri Kopriwa Jan 30 '23 at 12:12
  • Servers see the used host name. May be the server can host multiple web sites on the same IP but different hostnames so it uses the hostname to detect which server to use, of course 10.0.2.2 i known known to the server and thus the request fails. Try on your host `curl --header "Host: localhost" http://127.0.0.1:4723/` and `curl --header "Host: 10.0.2.2" http://127.0.0.1:4723/` – Robert Jan 30 '23 at 12:35
  • @Robert, it's a webpack dev server, it host only one website at this address. Also, I don't think it is possible to run curl command within the Android Emulator. Appium service was started with `--allow-cors` – Dimitri Kopriwa Jan 30 '23 at 12:54
  • As I wrote `Try on your host` so not inside the emulator, on your host OS. Curl is included in Windows and Linux. – Robert Jan 30 '23 at 12:56
  • it is a problem with the Android app, because http://10.0.2.2:4723/status can be opened in Chrome Android – Dimitri Kopriwa Jan 30 '23 at 12:59
  • Have you allows clear text traffic (non-TLS) for the debug build? – Robert Jan 30 '23 at 13:02
  • No I did not, and I think this is the problem. (It is not a debug build btw): https://medium.com/livefront/how-to-connect-your-android-emulator-to-a-local-web-service-47c380bff350 , I have tried to allow using this xml config, and building to check agian – Dimitri Kopriwa Jan 30 '23 at 13:05
  • I rather not allow clearTextTraffic for every host – Dimitri Kopriwa Jan 30 '23 at 13:05

1 Answers1

0

It is necessary to allow clear text traffic for 10.0.2.2,

  1. Create an xml directory in : android/app/src/main/res/
mkdir -p android/app/src/main/res/xml
  1. Create a file in it network_security_config.xml:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">10.0.2.2</domain>
    </domain-config>
</network-security-config>
  1. Edit AndroidManifest.xml by adding android:networkSecurityConfig="@xml/network_security_config" in application.

Source : https://medium.com/livefront/how-to-connect-your-android-emulator-to-a-local-web-service-47c380bff350

Dimitri Kopriwa
  • 13,139
  • 27
  • 98
  • 204