Search code examples
pythonandroidseleniumappium

Could not proxy command to the remote server: Socket hang up


I am running two Android devices in parallel. On occasion, this error shows itself.

selenium.common.exceptions.WebDriverException:
Message: An unknown server-side error occurred while processing the command. 
Original error: Could not proxy command to the remote server. 
Original error: socket hang up

Other people have suggested changing the systemPort. I have done that and seen very limited improvement.

Other people have also suggested issues with my capability syntax. I have triplechecked them and do not believe there are any issues.

desired_cap_1 ={
    "platformName": "Android",
    "deviceName": "Galaxy A60",
    "platformVersion": "11",
    "app": "appname",
    "appPackage": "apppackage",
    "appActivity": "appactivity",
    "fullReset": "False",
    "noReset": "True",
    "systemPort": 8202

}

desired_cap_2 ={
    "platformName": "Android",
    "deviceName": "Galaxy S7 Edge",
    "platformVersion": "8",
    "app": "appname",
    "appPackage": "apppackage",
    "appActivity": "appactivity",
    "fullReset": "False",
    "noReset": "True",
    "systemPort": 8203
}

Other people have mentioned something about "teardown" in Appium during parallel testing, a concept I am not familiar with and have found little documentation about.

What could be the cause for this?


Solution

  • I have experienced this myself and found that starting and stopping the Appium server with the correct methods is important before, during and after testing. Your Desired Capabilities seems fine, these do not affect how the Appium server is started and stopped, only for details about your functional test like device and app. If I look at my own projects, I have the following executions to correctly start and stop the Appium server:

    1. Check is the server is already running on the port (you see that I do not start the Appium server if it's already running - otherwise this caused some problems)
        public boolean checkServerRunning(int port) {
            boolean isServerRunning = false;
            ServerSocket serverSocket;
            try {
                serverSocket = new ServerSocket(port);
                serverSocket.close();
            } catch (IOException e) {
                isServerRunning = true;
            }
            return isServerRunning;
        }
    
         if (!service.checkServerRunning(port)) {
             service.startServerCustom(url, port, null);
         }
    
    1. I assign the AndroidDriver to the URL of the Appium server
    driver = new AndroidDriver<>(new URL("http://" + url + ":" + port + "/wd/hub"), cap);
    

    At this point, I assume my Appium server is running correctly and my AndroidDriver instance is coupled

    1. Performing all transactions in my test.

    Now here comes the teardown part which works best for me, since using these steps I have not encountered the socket hang up error:

    1. Calling the AndroidDriver to execute the quit command - this must be performed at all times after my test so that nothing for my AndroidDriver is in the memory of the Appium Server
    driver.quit();
    
    1. Quit the server if I have started it with my script (you cannot stop the server if the server is not started inside the script itself, because the service. instance has no Appium Server in it's memory.
     if (serverFlag) service.stopServer();
    

    Last but not least Parallel running requires a seperate instance of Appium for each device on the same machine. If you follow the steps carefully for each instance - different port/AndroidDriver per instance - the server will handle this correctly.