简体   繁体   English

如何从 android 模拟器访问 appium 服务器

[英]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.在 ReactNative android 应用程序中,我想检测正在运行的 appium 服务(端到端服务器),以便我的应用程序知道它当前正在运行 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 .从我的 android 模拟器中,为了到达我的localhost ,考虑到 Android 模拟器使用localhost作为它自己的网络接口,我读过 android 开发人员使用10.0.2.2到达主机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:我希望得到以下 JSON 结果:

{"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 : appium 服务器监听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如果我想访问另一个服务,例如在0.0.0.0:3000上运行的正在运行的 webpack 开发服务器,它工作正常

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

Why can't I contact the appium service?为什么联系不上appium服务?

It is necessary to allow clear text traffic for 10.0.2.2 ,有必要允许10.0.2.2的明文流量,

  1. Create an xml directory in: android/app/src/main/res/在: android/app/src/main/res/中创建一个xml目录
mkdir -p android/app/src/main/res/xml
  1. Create a file in it network_security_config.xml :在其中创建一个文件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 .通过在application中添加android.networkSecurityConfig="@xml.network_security_config"来编辑AndroidManifest.xml

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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