Creating hub and nodes on Selenium Grid manually and through automation

Selenium-standalone server can act as a jar(java archive) and also server for creating hub(server) and nodes(client machines where automation will run)
Pre-requistie
1. I am creating hub on my local computer on its default port(4444).
2. I am creating nodes again on local machine on port 5555 and port 5556.  
3. I have places my local jar at D:\selenium-server.

java -jar D:\selenium-server\selenium-server-standalone-3.141.59.jar -role hub

java -jar D:\selenium-server\selenium-server-standalone-3.141.59.jar -role node -port 5555 -hub http://localhost:4444/grid/register

java -jar D:\selenium-server\selenium-server-standalone-3.141.59.jar -role node -port 5556 -hub http://localhost:4444/grid/register

Now instead of creating manually, I am using java program and doing it programatically. (making use of java runtime class)

 

import java.io.IOException;
import java.io.OutputStream;
 
public class WindowsCmdUtility {
 
private static String jarWithPath = "D:\\selenium-server\\selenium-server-standalone-3.141.59.jar";
 
    public static void main(String args[]) throws InterruptedException{
    execDOSCmdForHub();
    Thread.sleep(3000);
    execDOSCmdForNode("5555");
    Thread.sleep(3000);
    execDOSCmdForNode("5556");
    Thread.sleep(2000);
    }
    
    
   public static void execDOSCmdForHub() {  
    try {
        // Execute command   
        String command = "cmd /c start "+jarWithPath+" -role hub";
        Process child = Runtime.getRuntime().exec(command);
        child.waitFor();
 
        // Get output stream to write from it
        OutputStream out = child.getOutputStream();
        out.write("cd C:/ /r/n".getBytes());
        out.flush();
        out.write("dir /r/n".getBytes());
        out.close();
    } catch (IOException e) {
    } catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
    }
   
   public static void execDOSCmdForNode(String nodePort) {  
    try {
        // Execute command
        String command = "cmd /c start java -jar "+jarWithPath+" -role node -port "+nodePort+" -hub http://localhost:4444/grid/register";
        Process child = Runtime.getRuntime().exec(command);
        child.waitFor();
 
        // Get output stream to write from it
        OutputStream out = child.getOutputStream();
        out.write("cd C:/ /r/n".getBytes());
        out.flush();
        out.write("dir /r/n".getBytes());
        out.close();
    } catch (IOException e) {
    } catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
   }
}

**************************************************************************************

QACult Best Software Testing Courses in Chandigarh tricity.- We love to enhance your knowledge.

QACult is the premier institute catering to the requirements of experienced and fresh pass-out that gives leaders like you a new way to experience Quality engineering—while you work and giving you the flexibility to both advance your career.
Our faculty have 12+ years of industrial experience and have developed many automation testing frameworks in java using TestNG or BDD (cucumber) methodology. We expertise in developing automation testing frameworks in java, python, javascript, php, ruby(WATIR-webdriver & Capybara) and Appium.
please subscribe our channel for more such updates:  

https://www.youtube.com/channel/UC0xat537YITJbN_9GSMUALA
And visit our website: www.qacult.com for various blogs and Upcoming Events.