Start Selenium hub programmatically using Gridlauncher
hariselva | Feb. 8, 2020, 10:23 a.m.

Gridlauncher is the useful utility to integrate in any Java based Selenium automation projects.

  • This helps the developers to skip the start of selenium grid hub and node manually.
  • Developers often forget to start the grid while developing the test scripts.
  • Gridlauncher overcomes these error scenarios during development and execution.
import org.openqa.grid.selenium.GridLauncherV3;
public void beforeSuite() {
	String[] windowsHub = { "-role", "hub" };
	try {
		GridLauncherV3.main(windowsHub);
		Thread.sleep(5000);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	String[] macHub = { "-role", "hub", "-port", "4445" };
	try {
		GridLauncherV3.main(macHub);
		Thread.sleep(5000);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

@AfterSuite
public void afterSuite() {
	logger.info(
			"After suite will always execute at last when all the annotations or test in the suite have run.");
}

@Parameters({"browser","platform"})
@BeforeTest
public void beforeTest(String browser, String platform) throws Exception {
	strBrwsr = browser;
	if(!platform.equalsIgnoreCase("mac")) {
		if (browser.equalsIgnoreCase("ie")) {
			String[] node = { "-role", "node", "-hub", "http://localhost:4444/grid/register", "-browser",
					"browserName=internet explorer", "-port", "5555" };
			GridLauncherV3.main(node);
		}else if (browser.equalsIgnoreCase("MicrosoftEdge")) {
			String[] node = { "-role", "node", "-hub", "http://localhost:4444/grid/register", "-browser",
					"browserName=" + strBrwsr + "", "-port", "5556" };
			GridLauncherV3.main(node);
		}else if (browser.equalsIgnoreCase("Chrome")) {
			String[] node = { "-role", "node", "-hub", "http://localhost:4444/grid/register", "-browser",
					"browserName=" + strBrwsr.toLowerCase() + "", "-port", "5557" };
			GridLauncherV3.main(node);
		}else if (browser.equalsIgnoreCase("firefox")) {
			String[] node = { "-role", "node", "-hub", "http://localhost:4444/grid/register", "-browser",
					"browserName=" + strBrwsr.toLowerCase() + "", "-port", "5558" };
			GridLauncherV3.main(node);
		}
		Thread.sleep(1000);
	}
}