Home > Blockchain >  Cannot invoke "org.openqa.selenium.WebDriver.get(String)" because "this.driver"
Cannot invoke "org.openqa.selenium.WebDriver.get(String)" because "this.driver"

Time:11-07

I am getting the error below. I am new to Java and thus any help would be appreciated.

Cannot invoke "org.openqa.selenium.WebDriver.get(String)" because "this.driver" is null

Please see the code below:

package steps;

import io.cucumber.java.en.Given;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class hotelBookingFormPage {

    public WebDriver driver;

    @Before
    public void startBrowser() {
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
    }

    @After
    public void closeBrowser() {
        driver.close();
        driver.quit();
    }

    @Given("I navigate to the hotel booking form page")
    public void iNavigateToTheHotelBookingFormPage() {
        driver.get("http://hotel-test.equalexperts.io/");
    }

Any help would be appreciated.

CodePudding user response:

import org.junit.After;
import org.junit.Before;

With this you're importing the JUnit hook annotations, not the Cucumber ones. So Cucumber doesn't know you want to run the annotated methods before and after each scenario.

Cucumbers annotations are in a different package:

import io.cucumber.java.en.Before;
import io.cucumber.java.en.After;

CodePudding user response:

The casting of the Webdriver interface with Chromedriver has only been declared in your @Before step (where it is not used), the other steps are unaware. Amend to the following:

public class hotelBookingFormPage {

    public WebDriver driver = new ChromeDriver();

    @Before
    public void startBrowser() {
        WebDriverManager.chromedriver().setup();
    }

    @After
    public void closeBrowser() {
        driver.close();
        driver.quit();
    }

    @Given("I navigate to the hotel booking form page")
    public void iNavigateToTheHotelBookingFormPage() {
        driver.get("http://hotel-test.equalexperts.io/");
    }
  • Related