seleniummavenselenium-webdrivercucumbercucumber-java

How to resolve java.lang.NullPointerException in Selenium with Cucumber?


Problem Statement: I am working on Selenium using a Maven Project and executing a Cucumber BDD test for user registration. However, the web page is not receiving inputs, and I am encountering a java.lang.NullPointerException during execution.

Interestingly, when I execute the same code in a Java Project (non-Maven), it works perfectly fine.

Project Structure: I have the following packages in my Maven project:

  1. Resources
  2. Features
  3. stepDefinitions
  4. Runner

Feature File: Register.feature

Feature: The Registration Page

Background: Given The User is on the Registration Page

Scenario: The User Should be able to Register
When The User Enters Contact Information
And The User Enters Mailing Information
When The User Enters User Information

Step Definition: RegisterSteps.java

public class RegisterSteps {

public WebDriver driver;

@Given("^The User is on the Registration Page$")
public void the_User_is_on_the_Registration_Page() throws Throwable {
    System.out.println("Background:- The User is on the Home Page");
    System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\cucumberBDDProject\\src\\Resources\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("http://www.newtours.demoaut.com/");
    Thread.sleep(3000);
    driver.navigate().to("http://www.newtours.demoaut.com/mercuryregister.php");
    Thread.sleep(3000);
}

@When("^The User Enters Contact Information$")
public void the_User_Enters_Contact_Information() throws Throwable {
    System.out.println("Step1:- The User Enters Contact Information");
    try {
        WebElement e = driver.findElement(By.name("firstName"));
        e.click();
        e.sendKeys("Manoranjan");
        driver.findElement(By.name("lastName")).sendKeys("Das");
        driver.findElement(By.name("phone")).sendKeys("9019139239");
        driver.findElement(By.name("userName")).sendKeys("testmail@gmail.com");
    } catch (Exception e) {
        System.out.println("Exception has Caught");
    }
}

@And("^The User Enters Mailing Information$")
public void the_User_Enters_Mailing_Information() throws Throwable {
    System.out.println("Step2:- The User Enters Mailing Information");
    driver.findElement(By.name("address1")).sendKeys("Lakeview Street");
    driver.findElement(By.name("address2")).sendKeys("123 Southern Ave");
    driver.findElement(By.name("city")).sendKeys("Dallas");
    driver.findElement(By.name("state")).sendKeys("Texas");
    driver.findElement(By.name("postalCode")).sendKeys("75001");
    Thread.sleep(3000);
    WebElement e = driver.findElement(By.name("country"));
    Select s = new Select(e);
    s.selectByValue("USA");
    Thread.sleep(3000);
}

@When("^The User Enters User Information$")
public void the_User_Enters_User_Information() throws Throwable {
    System.out.println("Step3:- The User Enters User Information");
    driver.findElement(By.name("email")).sendKeys("testmail@gmail.com");
    driver.findElement(By.name("password")).sendKeys("Pass@123");
    driver.findElement(By.name("confirmPassword")).sendKeys("Pass@123");
}
}

Runner Class: Runner.java

package runners;

import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(features="C:\\Selenium\\cucumberBDDProject\\ProjectFeatures", glue="stepDefinitions")
public class LoginRunner {
}

Error Message:

Background:- The User is on the Home Page
Starting ChromeDriver 2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e) on port 39832
Only local connections are allowed.
INFO: Detected dialect: OSS
Step1:- The User Enters Contact Information
Exception has Caught
Step2:- The User Enters Mailing Information
Step1:- The User is on the Login Page
Starting ChromeDriver 2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e) on port 35493
Only local connections are allowed.
INFO: Detected dialect: OSS
[WARNING]: Timed out connecting to Chrome, retrying...
[WARNING]: Timed out connecting to Chrome, retrying...

Failed scenarios:
Register.feature:6 # Scenario: The User Should be able to Register
login.feature:2    # Scenario: The User should be able to Login with Valid Credentials.

2 Scenarios (2 failed)
9 Steps (2 failed, 5 skipped, 2 passed)

Exception:

java.lang.NullPointerException
    at stepDefinitions.RegisterSteps.the_User_Enters_Mailing_Information(RegisterSteps.java:56)
    at ?.And The User Enters Mailing Information(Register.feature:8)

Solution

  • Within RegisterSteps Class you have declared a global WebDriver instance as:

    public  WebDriver driver;
    

    But moving ahead within void the_User_is_on_the_Registration_Page() method you have initialized another WebDriver instance as:

    WebDriver driver = new ChromeDriver();
    

    Which have no scope outside void the_User_is_on_the_Registration_Page() method. Hence you see java.lang.NullPointerException

    Solution

    Within void the_User_is_on_the_Registration_Page() method change it as:

    driver = new ChromeDriver();