Jacobvu84 / serenity-pageobject-junit-webdriver

4 stars 1 forks source link

System Port Scanner for appium-uiautomator2-server #77

Open Jacobvu84 opened 3 years ago

Jacobvu84 commented 3 years ago
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.util.SocketUtils;

public class SystemPortScanner {

        private final static int MIN = 8200;
    private final static int MAX = 8299;

    public static void main(String[] args) {
        System.out.println("getSystemPortInUse: "+getSystemPortInUse(MIN, MAX));
        System.out.println("findAvailableTcpPort: "+SocketUtils.findAvailableTcpPort(MIN, MAX));
    }

    private static List<Integer> getSystemPortInUse(int min, int max) {
        Socket Skt;
        String host = "localhost";
        List<Integer> port = new ArrayList<>();

        for (int i = min; i < max+1; i++) {
            try {
                Skt = new Socket(host, i);
                System.out.println("There is a server on port " + i + " of " + host);
                port.add(i);
            } catch (UnknownHostException e) {
                System.out.println("Exception occured" + e);
                break;
            } catch (IOException e) {
            }
        }
        return port;
    }
}

SystemPort used to connect to appium-uiautomator2-server or appium-espresso-driver. The default is 8200 in general and > selects one port from 8200 to 8299 for appium-uiautomator2-server, it is 8300 from 8300 to 8399 for appium-espresso-> > driver. When you run tests in parallel, you must adjust the port to avoid conflicts.