Search code examples
javaseleniumselenium-webdrivercucumberbdd

How scope of driver move in this StepDefinition class?


This is the StepDefinition class I am using:

@Given("^user is already on Login Page$")
public void user_already_on_login_page() {
    System.setProperty("webdriver.chrome.driver", "/Users/ss/Downloads/chromedriver.exe");
    driver = new ChromeDriver();
    driver.get("https://www.google.com/index.html");
}

@When("^title of login page is Free CRM$")
public void title_of_login_page_is_free_CRM() {
    String title = driver.getTitle();
    System.out.println(title);
    Assert.assertEquals("Google", title);
}

If I instantiate driver in "user_already_on_login_page()" method and then how scope of driver moved to next method "title_of_login_page_is_free_CRM()"?


Solution

  • In the line:

    driver = new ChromeDriver();
    

    You are initializing the driver, where as the driver declaration is within the global scope which is as:

    WebDriver driver;
    

    and should be declared at the class level with global scope.

    So either of the methods user_already_on_login_page() and title_of_login_page_is_free_CRM() have access to the driver variable.