Using Selenium Grid to Run Cucumber Test

Profile picture for user devraj
Submitted by devraj on

To integrate Cucumber with Selenium Grid, add below code to your @Before hook or create below method in TestBase and call it in @BeforeHook:

public class Base
	DesiredCapabilities cap;
	String browser = "chrome";
	public static String hubAddress = "";
	public WebDriver setDriver(WebDriver driver) throws InterruptedException, MalformedURLException
			cap =;
		else if(browser=="firefox")
			cap = DesiredCapabilities.firefox();
		driver = new RemoteWebDriver(new URL(hubAddress), cap);
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		return driver;

The desired capability is a series of key/value pairs that stores the browser properties like browsername, browser version, the path of the browser driver in the system, etc. to determine the behaviour of the browser at run time. The RemoteWebDriver class implements the WebDriver interface to execute test scripts through the RemoteWebDriver server on a remote machine. In above code you need to read browser, hubAddress from external files or Jenkins parameters. This is how you can call in @Before, you can create object tb in step definitions file and call its methods using it. Do not forget to create WebDriver instance in StepDefinition File.

public void bf0() throws MalformedURLException, InterruptedException
	driver = tb.setDriver(driver);	

Our cucumber test will execute on Selenium Grid but it will not execute in parallel, It will execute one browser instance at a time. To execute in parallel check below topics.