简体   繁体   中英

How to allow site to get the machine's IP which ever is acessing its URL

Don't get confused by the title which I know is quite common on Stack overflow but my problem is different.

Requirement: I am implementing Selenium grid in my framework, where I need to provide users an option of selecting machines/nodes where they want to run their tests. I have designed a UI based framework hosted on Machine 1 giving a dropdown of machines/nodes to select the value from. Depending on the selected value, the code will create a nodeURL based on the machine IP and will successfully run the tests on that machine. Below is the code

if(hostMachine.equals("Machine1"))
    nodeURL = new URL("http://IP:PortNo/wd/hub");

 else if (hostMachine.equalsIgnoreCase("Machine2"))
    nodeURL = new URL("http://IP:PortNo/wd/hub");

Similarly I need to provide an option of user machine as well ie which ever machine, user is having, he/she should be able to run their tests on that machine as well. So I Have added an option of My Machine in dropdown, and written the code mentioned below to fetch the IP address of the user's machine dynamically.

else if(hostMachine.equals("My Machine") || hostMachine.equals("Select"))
{    
     try   
     {
     InetAddress ipAddress = InetAddress.getLocalHost();
     nodeURL = new URL("http://"+ipAddress.getHostAddress+":5555/wd/hub");
}

catch(Exception e)
{
            // TODO Auto-generated catch block
            e.printStackTrace();
}

Problem: Now the problem what I am facing is, whenever user selects the My Machine, code is fetching the IP address of Machine 1 (where the framework is hosted currently) and running the tests there rather than on user's machine. I have deployed the war of the framework in Machine 1 using tomcat and access the URL from my machine, to test but not sure why it is fetching the IP address of Machine 1 everytime. Is ipAddress.getHostAddress() fetches the IP address of host server only? Is there anyway to fetch the IP address of user's machine from where we are trying to access the URL so that which ever user selects the option of My machine, the code should fetch their machine IP and run the tests there. Will appreciate your help.

Thanks, Hasan

To get the Ip of host machine you can use the WebRTC extension of HTML5. This allows javascript to query the local client IP address - Assuming your UI based framework uses Javascript

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