Lucky
Lucky

Reputation: 107

Null pointer exception in selenium cucumber JUnit Framework

Can someone tell the issue in my code for null pointer exception?

Error Message in console

  =>test.pages.HomePage@31e75d13<=

    [31mFailed scenarios:[0m
    [31mE2E_Test.feature:3 [0m# Scenario: Test scenario

    1 Scenarios ([31m1 failed[0m)
    10 Steps ([31m1 failed[0m, [36m8 skipped[0m, [32m1 passed[0m)
    0m12.461s

    java.lang.NullPointerException
        at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
        at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
        at com.sun.proxy.$Proxy17.sendKeys(Unknown Source)
        at test.pages.HomePage.enterSearchText(HomePage.java:31)
        at stepDefinitions.Steps.he_search_for(Steps.java:49)
        at ✽.When he search for "test"(E2E_Test.feature:5)

Although I am getting the driver object and its not coming as Null as well but still getting a null pointer exception.

I am trying to run the selenium webdriver code to automate some test case. Here i am trying to open google.com and wants to enter some text in search box but after opening the google.com, when the execution reaches searchtextbox.sendkeys("test"), it gives null pointer exception. I tried debugging it to see if the homepage class object is coming as null or not but its showing the value and not null.

This is the test base class that i am using to initiate the google site and maximize the code

public class TestBase {

  public static WebDriver driver;
  public static Properties prop;
  public static EventFiringWebDriver e_driver;
  public static WebEventListener eventListener;

  public TestBase() {
    try {
      prop = new Properties();

      FileInputStream ip = new FileInputStream(System.getProperty("user.dir") + "/src/main/java/test" +
        "/config/config.properties");
      prop.load(ip);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  // This method is used to initiatize the site url

  public static void initialization(String url) {
    String browserName = prop.getProperty("browser");

    if (browserName.equals("chrome")) {
      System.setProperty("webdriver.chrome.driver", "C:\\Users\\test\\Downloads\\driver\\chromedriver.exe");
      driver = new ChromeDriver();
    }



    e_driver = new EventFiringWebDriver(driver);
    // Now create object of EventListerHandler to register it with EventFiringWebDriver
    eventListener = new WebEventListener();
    e_driver.register(eventListener);
    driver = e_driver;

    driver.manage().window().maximize();
    driver.manage().deleteAllCookies();
    driver.manage().timeouts().pageLoadTimeout(TestUtil.PAGE_LOAD_TIMEOUT, TimeUnit.SECONDS);
    driver.manage().timeouts().implicitlyWait(TestUtil.IMPLICIT_WAIT, TimeUnit.SECONDS);
    if (url == "google") {
      driver.get(prop.getProperty("url"));
    }

  }
}

//    Steps Definition file (Steps.java):    This is the step defintion file // there is a function he_search_for called where the exception occurs

public class Steps extends TestBase {

  WebDriver driver;
  TestUtil testUtil;

  HomePage homePage;

  @Given("^user is on google home page$")
  public void user_is_on_google_home_page() throws Throwable {
    initialization("google");
    testUtil = new TestUtil();
    homePage = new HomePage(driver);
  }

  @When("^he search for \"([^\"]*)\"$")
  public void he_search_for(String arg1) throws InterruptedException {
    System.out.print("=>" + homePage + "<=");
    homePage.enterSearchText();
  }
}

//    HomePage Class is used to define all the page elements here in this class, i used enterSearchText function to enter the text in a search box. 

public class HomePage extends TestBase {

  @FindBy(name = "q")
  WebElement searchTextBox;


  WebDriver driver;

  // Initializing the Page Objects:
  public HomePage(WebDriver driver) {
    this.driver = driver;
    PageFactory.initElements(driver, this);
  }

  public void enterSearchText() {
    searchTextBox.sendKeys("Test");
  }
}

Upvotes: 1

Views: 3395

Answers (2)

Problem lies within your code design pattern between Classes Steps & TestBase. Please note

First, Class Steps is extending TestBase which already has WebDriver variable declared & initialized in it. So you do not need to again define WebDriver instance with in Steps. So please remove "WebDriver driver;" from below peace of code.

public class Steps extends TestBase {

  WebDriver driver;
  TestUtil testUtil;

Second, Please do not declare WebDriver as static variable. Kindly declare it as Non-static as Keeping static may create problem during parallel execution as well.

public class TestBase {

  public  WebDriver driver; 

Making WebDriver instance as non-static and having it as Thread safe

TestBase.java

public class TestBase {

    public WebDriver driver;
    public static Properties prop;

    // This method is used to initiatize the site url
    public synchronized void initialization(String url) {
        String browserName = prop.getProperty("browser");
        if (browserName.equals("chrome")) {
            System.setProperty("webdriver.chrome.driver", "C:\\Users\\test\\Downloads\\driver\\chromedriver.exe");
            driver = new ChromeDriver();
            DriverManager.setWebDriver(driver);
        }
    }
}

DriverManager.java

import org.openqa.selenium.WebDriver;
public class DriverManager {

public static ThreadLocal<WebDriver> dr = new ThreadLocal<WebDriver>();

public static WebDriver getDriver() {
    return dr.get();
}

public static void setWebDriver(WebDriver driver) {
    dr.set(driver);
}
}

Upvotes: 1

Fenio
Fenio

Reputation: 3635

The problem is here

public class Steps extends TestBase {

  WebDriver driver;
  TestUtil testUtil;

  HomePage homePage;

  @Given("^user is on google home page$")
  public void user_is_on_google_home_page() throws Throwable {
    initialization("google");
    testUtil = new TestUtil();
    homePage = new HomePage(driver);
  }

  @When("^he search for \"([^\"]*)\"$")
  public void he_search_for(String arg1) throws InterruptedException {
    System.out.print("=>" + homePage + "<=");
    homePage.enterSearchText();
  }
}

The WebDriver driver is null. You initialized WebDriver in initialization("google") method but you don't assign the value of created WebDriver to your driver

Additional line of code might help you.

  @Given("^user is on google home page$")
  public void user_is_on_google_home_page() throws Throwable {
    initialization("google");
    this.driver = TestBase.driver; //asign initialized WebDriver to this instance variable
    testUtil = new TestUtil();
    homePage = new HomePage(driver);
  }

You can also remove the local WebDriver driver variable. Since TestBase contains static WebDriver, you can just use it directly since you use inheritance.

However, I highly suggest reading about WebDriverFactory or any similar term, like WebDriverManager. Anything to handle WebDriver instantiation without creating a static WebDriver. It will cause a lot of issues in the future with parallel execution.

Upvotes: 0

Related Questions