简体   繁体   中英

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. Below are my attempt and it's not working. It's still giving me my own Ip address. How can I make my Android application go through appium server and then go through a proxy server?

I am using Proxy Manager from 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

    // 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:

-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. Redirection for UDP is not currently supported.

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

To make it work in your test script try to add avdArgs capability:

uiautomator2 doc says about avdArgs capability value.

Either a string or an array of emulator command line 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>");

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