简体   繁体   English

如何使用 Appium 连接到代理以在 Android 虚拟设备上获取不同的 IP 地址?

[英]How to connect to a proxy to get different IP Address on Android Virtual Device using Appium?

I am trying to make my mobile application go through a proxy so that I can have a different ip address.我正在尝试通过代理制作我的移动应用程序 go,以便我可以拥有不同的 ip 地址。 Below are my attempt and it's not working.以下是我的尝试,但它不起作用。 It's still giving me my own Ip address.它仍然给了我自己的 Ip 地址。 How can I make my Android application go through appium server and then go through a proxy server?如何通过appium服务器制作我的Android应用程序go,然后通过代理服务器制作go?

I am using Proxy Manager from BrightData.我正在使用 BrightData 的代理管理器。 it allow me to input all my proxies (host:port:username:password) and rotate it automatically.它允许我输入所有代理(主机:端口:用户名:密码)并自动旋转它。 Proxy Manager give me a port to use which connect to those rotated proxies.代理管理器给了我一个端口来连接那些轮换的代理。 Eventually I just want to use (host:port:username:password) but I don't know how to authenticate correctly最终我只想使用 (host:port:username:password) 但我不知道如何正确验证

    // Proxy Manager port which connect me to different proxies
    String proxystring = "127.0.0.1:24000";   

    Proxy proxy = new Proxy();
    proxy.setSslProxy(proxystring);
    proxy.setHttpProxy(proxystring);
    
    File appDir = new File("src");
    File app = new File(appDir, "WhatismyIPaddress_v3.02_apkpure.com.apk");
            
    DesiredCapabilities cap = new DesiredCapabilities();
    cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Pixel 3 API 30");
    cap.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
    cap.setCapability(MobileCapabilityType.AUTOMATION_NAME, "uiautomator2");
    cap.setCapability(MobileCapabilityType.PROXY, proxy);
    
    //127.0.0.1:4723 is the ip:port of appium server.
    AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"), cap);
    
    TimeUnit.SECONDS.sleep(3);

Appium seems not support proxing itself, but it's possible to configure proxy for android emulator programmatically passing next args: Appium 似乎不支持代理本身,但可以为 android 模拟器配置代理,以编程方式传递下一个参数:

-http-proxy http://<username>:<password>@<machineName>:<port>

The -http-proxy option forces the emulator to use the specified HTTP/HTTPS proxy for all outgoing TCP connections. -http-proxy 选项强制模拟器对所有传出的 TCP 连接使用指定的 HTTP/HTTPS 代理。 Redirection for UDP is not currently supported.目前不支持 UDP 的重定向。

https://developer.android.com/studio/run/emulator-networking#proxy https://developer.android.com/studio/run/emulator-networking#proxy

To make it work in your test script try to add avdArgs capability:要使其在您的测试脚本中工作,请尝试添加avdArgs功能:

uiautomator2 doc says about avdArgs capability value. uiautomator2 doc 说关于avdArgs能力值。

Either a string or an array of emulator command line arguments.仿真器命令行 arguments 的字符串或数组。

Try something like this, but I'm not fully shure, maybe to pass proxy you'll have to pass string array, not string.尝试这样的事情,但我并不完全同意,也许要传递代理你必须传递字符串数组,而不是字符串。

cap.setCapability("avdArgs",  "-http-proxy http://<username>:<password>@<machineName>:<port>");

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

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