2

While running a cucumber feature file using junit with Java, I am getting a NULL pointer exception. I am not able to understand why this exception is coming up.
This is my step definition file, written in java.

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import pageObjects.CartPage;
import pageObjects.Checkoutpage;
import pageObjects.HomePage;
import pageObjects.ProductListingPage;
import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.When;

public class EndtoEndTest {

WebDriver driver;

@Given("^User is on Homepage$")
public void user_is_on_Homepage() throws Throwable {

    System.setProperty("webdriver.chrome.driver", "C:\\Users\\Downloads\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("http://www.shop.demoqa.com");
    driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
    driver.manage().window().maximize();


}

@When("^he searches for \"([^\"]*)\"$")
public void he_searches_for(String arg1) throws Throwable {

    HomePage home = new HomePage(driver);
    home.perform_Search(arg1);
}

@When("^Choose to buy the first item$")
public void choose_to_buy_the_first_item() throws Throwable {
    ProductListingPage productListingPage = new ProductListingPage(driver);
    productListingPage.select_Product(0);
    productListingPage.clickOn_AddToCart(); 
}

@When("^moves to checkout from mini cart$")
public void moves_to_checkout_from_mini_cart() throws Throwable {
    CartPage cartPage = new CartPage(driver);
    cartPage.clickOn_Cart();
    cartPage.clickOn_ContinueToCheckout();
}

@When("^enter personal details onn checkout page$")
public void enter_personal_details_onn_checkout_page() throws Throwable {


    Checkoutpage checkoutPage = new Checkoutpage(driver);
    checkoutPage.fill_PersonalDetails();    
}

@When("^select same delivery address$")
public void select_same_delivery_address() throws Throwable {
    Checkoutpage checkoutPage = new Checkoutpage(driver);
    checkoutPage.check_ShipToDifferentAddress(false);
}

@When("^select payment method as \"([^\"]*)\" payment$")
public void select_payment_method_as_payment(String arg1) throws Throwable {
    Checkoutpage checkoutPage = new Checkoutpage(driver);
    checkoutPage.select_PaymentMethod("CheckPayment");
}

@When("^place the order$")
public void place_the_order() throws Throwable {
    Checkoutpage checkoutPage = new Checkoutpage(driver);
    checkoutPage.check_TermsAndCondition(true);
    checkoutPage.clickOn_PlaceOrder();

    driver.quit();
}
}

This is my PageObject File

import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;

public class HomePage {

WebDriver driver;

public HomePage(WebDriver driver) {
    this.driver = driver;
    PageFactory.initElements(driver, this);
}

@FindBy(how=How.XPATH, using="//a[@class='noo-search icon_search']")
private WebElement click_on_search_icon;

@FindBy(how = How.XPATH, using="//input[@class='form-control']")
private WebElement enter_data_for_search;

public void perform_Search(String search) {

    click_on_search_icon.click();
    enter_data_for_search.sendKeys(search);
    enter_data_for_search.sendKeys(Keys.ENTER);

}

public void navigateTo_HomePage() {
    driver.get("http://www.shop.demoqa.com");
}

}

**Error stacktrace that I am getting while running the feature file:

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.$Proxy13.click(Unknown Source)
    at pageObjects.HomePage.perform_Search(HomePage.java:27)
    at stepDefinations.EndtoEndTest.he_searches_for(EndtoEndTest.java:39)
    at ✽.When he searches for "dress"(src/test/resources/functionalTest/EndtoEndTest.feature:9)

I am not sure why I am getting Null pointer Exception. Any help would be appreciated. Thanks**

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Ab123
  • 415
  • 2
  • 13
  • 34
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Logan Apr 07 '18 at 07:48

3 Answers3

5

You are seeing NullPointerException as you have declared a instance of WebDriver as in :

WebDriver driver;

But moving forward in the user_is_on_Homepage() function you have initiated another instance of WebDriver as in :

WebDriver driver = new ChromeDriver();

As all your functions will be using the same instance of the WebDriver you need to use the instance of WebDriver you have declared globally.

Solution

Change the line from :

WebDriver driver = new ChromeDriver();

To :

driver = new ChromeDriver();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

You are getting Null pointer exception because the scope of your driver object is getting limited inside one method only. It is getting destroyed as soon as cucumber-JVM test runner exits the following method. : user_is_on_Homepage() You have to rewrite your class in the following manner.

public EndToEndTest(){
    System.setProperty("webdriver.chrome.driver", "C:\\Users\\Downloads\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("http://www.shop.demoqa.com");
    driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
    driver.manage().window().maximize();
}

@Given("^User is on Homepage$")
public void user_is_on_Homepage() throws Throwable {

  //do some assertion that verifies you are on Homepage.   


}

Move the driver instantiation to a constructor method. OR create a parent/base class that handles the instantiation and closure of driver object.

If you are going ahead with latter approach, Use @Before and @After annotation from Junit to handle the teardown and setup of driver object prior to feature test execution.

Manmohan_singh
  • 1,776
  • 3
  • 20
  • 29
  • Thanks @Manmohan_Singh, it is working now but I have declared Webdriver driver as an instance variable, so instance variable scope is for class level then how it is getting destroyed just after one method. Do you think it is showing weird behavior???? – Ab123 Apr 07 '18 at 08:47
  • 1
    Well Cucumber JVM works in different way than ordinary JVM. The cucumber annotated methods are searched in all classes in the step definitions directory. Only the matching annotated methods are executed. Entire class is not allocated memory during runtime . – Manmohan_singh Apr 07 '18 at 09:06
0

I also faced the same problem. I have rectified it by making WebDriver as static in Step Definition file as below.

public class StepDefinitions {
    static WebDriver driver;
    HomePage homePage;

    @Given("^I navigate to the website$")
    public void iNavigateToTheWebsite() {
        System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get(config.getString("test.url"));
    }
    @When("^I enter details and click offers$")
    public void iEnterDetailsAndClickOffers() {
        homePage = new HomePage(driver);
        homePage.searchOffer();
    }

    @When("^I update the dates of the search$")
    public void iUpdateTheDatesOfTheSearch(){
        hotelSelectionPage = new HotelSelectionPage(driver);
        hotelSelectionPage.selectNewSearchDates();
    }
}
Andrew Regan
  • 5,087
  • 6
  • 37
  • 73
Raghunath
  • 1
  • 2