How to share same Webdriver with different Step Definitions in Cucumber


I am new to Cucumber and I want to use same Webdriver in different Step Definitions. Below is my code

public class ContextSteps {

private static boolean initialized = false;

private WebDriver driver;

public void setUp() throws Exception {
if (!initialized) {
// initialize the driver
System.setProperty("", GlobalVariables.browserfilepath+"chromedriver.exe");
driver =new ChromeDriver();
initialized = true;

public WebDriver getDriver() {
return driver;

Calling of above class in Step Definition as

private ContextSteps contextSteps;
// PicoContainer injects class ContextSteps
public LoginToApplication (ContextSteps contextSteps) {
this.contextSteps = contextSteps;
@Given("^User should have access to Cadence$")
public void Open_the_Browser() throws Throwable
WebDriver driver =contextSteps.getDriver();

I am getting “java.lang.NullPointerException”

  • You must to post comments
Showing 0 results
Your Answer
Post as a guest by filling out the fields below or if you already have an account.