Home > Enterprise >  How to keep instance of WebDriver across multiple Step Definitions with Cucumber
How to keep instance of WebDriver across multiple Step Definitions with Cucumber

Time:07-03

My first test is as follows:

public class Deals {
    
    public static WebDriver driver;
    
    static DriverConfig driverConfig = new DriverConfig();
    static HomePage home = new HomePage(driver);
    static DealsPage deals = new DealsPage(driver);
    static GenericFunctions genericFunctions = new GenericFunctions(driver);


    @Given("I am on the home page")
    public void i_am_on_the_home_page() {

        
        driverConfig.setupBrowser();
        driver = driverConfig.getDriver();
        String strPageName = "home";
        String url = genericFunctions.getUrl(strPageName);
        genericFunctions.startBrowser(driver, url);
        genericFunctions.acceptCookies(driver);
    
    }

    @When("^I navigate to ([^\"]*)$")
    public void i_navigate_to_page(String strPage) {
        
        genericFunctions.navigateTo(driver, strPage);
        
    }

    @Then("^the user should be on ([^\"]*)$")
    public void the_user_should_be_on_page(String strPage) {

        deals.verifyDealsPage(driver, strPage);
        driver.quit();
        
    }
    
}

My second test is as follows: Because the GIVEN for this test is exactly the same as the first, it is glued with the GIVEN in the first test.

public class SignIn {

    public static WebDriver driver;
    
    static DriverConfig driverConfig = new DriverConfig();
    static GenericFunctions genericFunctions = new GenericFunctions(driver);
    static HomePage home = new HomePage(driver);
    
    By createPassText = By.xpath("//a[contains(.,'Create Password')]");
    
    
    @When("I try to sign in with invalid credentials")
    public void i_try_to_sign_in_with_invalid_credentials() {

        driver = driverConfig.getDriver();
        home.selectSignInButton(driver);
        
    }
    
    @Then("I should see a box with the text Create your My Sky password")
    public void i_should_see_a_box_with_the_text_create_your_my_sky_password() {
        
        @SuppressWarnings("unused")
        boolean passTextExists = driver.findElement(createPassText).isDisplayed();
        
        if (passTextExists = true) {
            System.out.println("YES THE TEXT EXISTS");
            driver.findElement(createPassText).click();
        }

    }

When I run my test it runs the GIVEN from the Deals test but falls over on the WHEN on the Sign In test and I receive: "java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.findElement(org.openqa.selenium.By)" because "driver" is null at po.HomePage.selectSignInButton(HomePage.java:26)

My DriverConfig is as follows:

public class DriverConfig {

    
    public WebDriver driver;
    

    
    public void setupBrowser() {
        
        System.setProperty("webdriver.chrome.driver", "D:\\eclipse-workspace\\SkyFramework\\src\\test\\java\\chromedriver.exe");
        driver = new ChromeDriver();  
    }
    
    public WebDriver getDriver() {
        return driver;
    }
    
}

How do I pass my WebDriver instance over to the new class? I'm relatively new to setting up new frameworks so please let me know if the structure of my Step Definitions is not best practice.

Thanks

CodePudding user response:

I am not sure but this should work.

//Class that will be inherited by all others test class you use
 

    public abstract class AbstractTest {
          //declare driver
          public static WebDriver driver;
    
          //create driver
          @BeforeSuite
          public void webdriverSetUp() {
            System.setProperty("webdriver.chrome.driver", "D:\\eclipse-workspace\\SkyFramework\\src\\test\\java\\chromedriver.exe");
            driver = new ChromeDriver();        }
    
          //Method you will call in your test 
          public static WebDriver getDriver() {
            return driver;
          }
        }

public class SignIn extends AbstractTest {

Try and let me know if it works, if not, I have two more options :-)

  • Related