2

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.

3 Answers 3

4
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;
Sign up to request clarification or add additional context in comments.

1 Comment

Thats great thank you so much.
2

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/");
    }

2 Comments

You got the right idea, the webdriver wasn't instantiated, but you missed the reason why.
Thats great thank you so much.
0

public class BaseClass {

//every time we need to open url and browser so we need to create variable for url an dbrowser 
//we have to read url and browser so in base class create object of ReadConfigue class

ReadConfigue readconfigue=new ReadConfigue();
 String url=readconfigue.getbaseurl();   //store getbaseurl and getbrowser in string 
 String browser=readconfigue.getbrowser();
 public static WebDriver driver;
 public static Logger logger;
 
 
 //create method of launch broswer 
 @BeforeClass
 public void setup() 
 {
     String  browser="Chrome";
     switch(browser.toLowerCase())
     {
     case "Chrome":
         WebDriverManager.chromedriver().setup();
         driver=new ChromeDriver();
         break;
         
/*   case "msedge":
         WebDriverManager.edgedriver().setup();
         driver=new EdgeDriver();
         break;

     case "firefox":
         WebDriverManager.firefoxdriver().setup();
         driver=new FirefoxDriver();
         break; */
         default:
         driver=null;
         break;
         
     }
     driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    
     // for logging create object of logger
     //create static varible of logger  outside class and intitize here
     logger =LogManager.getLogger("AutomationFramework");
     
 }

 @AfterClass
 public void tearDown() 
 {
     driver.close();
     driver.quit();
 }

1 Comment

Please adjust code block, so it looks correctly (the first string and the end bracket)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.