简体   繁体   中英

How to reach appium server from android emulator

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:

在此处输入图像描述

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM