Android websocket automation: Any headless browser options?

I’m stuck with a tricky automation task on Android. I need to interact with a website that uses websockets, but my current setup is far from ideal.

Right now, I’m using WebView with a Java-JS interface. It works, but it’s clunky and not very efficient. I’ve got the WebView running invisibly in a service to handle background tasks.

The main issue is that I can’t just use simple GET/POST requests. The site’s JavaScript is heavily obfuscated, making it really hard to figure out the websocket API details.

Is there a better way to do this on Android? I’m looking for a headless browser solution that can handle websockets. Any suggestions would be super helpful!

Here’s a basic example of what I’m dealing with:

public class WebSocketHandler {
    private WebSocket mWebSocket;

    public void connect() {
        Request request = new Request.Builder().url("wss://example.com/socket").build();
        mWebSocket = client.newWebSocket(request, new WebSocketListener() {
            @Override
            public void onMessage(WebSocket webSocket, String text) {
                // Handle incoming messages
            }
        });
    }

    public void sendMessage(String message) {
        if (mWebSocket != null) {
            mWebSocket.send(message);
        }
    }
}

Any ideas on how to improve this setup or alternatives I could try?

hey mate, have u tried puppeteer for node.js? its pretty sweet for headless browsing n can handle websockets like a champ. u could run it on android with termux or sum. might be overkill but it’d give u full control over the browser. just a thought!

I’ve been down this road before, and I feel your pain with the WebView approach. Have you looked into using Jsoup with OkHttp? It’s a solid combo for handling websockets on Android without the overhead of a full browser.

Here’s the gist: Jsoup can parse the HTML and extract any necessary info, while OkHttp handles the websocket connection. It’s lightweight and gives you more control.

Something like this:

OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("wss://example.com/socket").build();
WebSocket ws = client.newWebSocket(request, new WebSocketListener() {
    @Override
    public void onMessage(WebSocket webSocket, String text) {
        // Handle incoming messages
    }
});

// Send a message
ws.send("Your message here");

It’s worked wonders for me in similar situations. Might be worth a shot if you’re looking to ditch the WebView approach.

Have you considered using Selenium with Appium for Android automation? It’s a powerful combination that can handle complex scenarios like websockets. Appium acts as a bridge between Selenium and mobile devices, allowing you to automate Android apps and web interactions.

For your specific case, you could use Selenium’s ChromeDriver in headless mode on Android. This approach would give you a full browser environment capable of executing JavaScript and managing websocket connections, all without a visible UI.

Here’s a basic setup to get you started:

AndroidDriver<AndroidElement> driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.get("https://example.com");

// Execute JavaScript to interact with websockets
driver.executeScript("websocket.send('your message');");

// Handle incoming messages
String response = driver.executeScript("return websocketResponse;");

This method should provide better performance and reliability compared to WebView, while still allowing you to interact with complex, obfuscated JavaScript and websockets.