Using Selenium Grid to Run Cucumber Test

Profile picture for user devraj

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 = "http://192.168.1.74:8888/wd/hub";
	
	public WebDriver setDriver(WebDriver driver) throws InterruptedException, MalformedURLException
	{
		if(browser=="chrome")
		{
			cap = DesiredCapabilities.chrome();
			cap.setBrowserName("chrome");
		}
		else if(browser=="firefox")
		{
			cap = DesiredCapabilities.firefox();
			cap.setBrowserName("firefox");     	
		}
	
		driver = new RemoteWebDriver(new URL(hubAddress), cap);
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		driver.manage().window().maximize();
		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.

@Before
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.