0

This is Class with cucumber annotations

public class EndtoEndTest {

WebDriver driver;
//private ConfigFileReader cnffilered;
//private CartPage cartpage;
//private Checkoutpage checkoutpage;
//private ProductListingPage productlistingpage;


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


    ConfigFileReader cnffilered= new ConfigFileReader();
    cnffilered.getBrowserType();
    cnffilered.getUrl();
    cnffilered.Implicitwait();
    cnffilered.MaxmimizeWindow();

}

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

        HomePage homepage = new HomePage(driver);
        homepage.perform_Search(arg1);

}
}

This is the class with FageFactory Initialization

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

}

Driver Initialization

package dataProviders;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class ConfigFileReader {

private static Properties properties;
private final String propertyFilePath = "F:\\Cucumber_Framework\\Cucumber_Framework\\src\\main\\resources\\configs\\Configurations.properties";
private WebDriver driver;
private ConfigFileReader cnffilered;


public ConfigFileReader() throws IOException
{

        properties = new Properties();
        FileInputStream fis = new FileInputStream(propertyFilePath);
        properties.load(fis);
        //fis.close();

}

public  void Implicitwait()
{
    driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
} 

public  void MaxmimizeWindow()
{
    driver.manage().window().maximize();

}
public  String getUrl() throws IOException
{


    String url = properties.getProperty("url");

    if(url!=null)
    {

        driver.get(url);


    }

    else
    {
        throw new RuntimeException("URL is not specified in configuration.properties file");
    }

    return url;


}

public  String driverpath() throws IOException
{

    //cnffilered = new ConfigFileReader();

    String driverpath = properties.getProperty("driverpath");

    if(driverpath!=null)
    {

        //driver.get(driverpath);
        return driverpath;

    }

    else
    {
        throw new RuntimeException("Driver path is not specified in configuration.properties"); 
    }

}

public  String getBrowserType() throws IOException
{
     cnffilered = new ConfigFileReader();

    String Browser_Type= properties.getProperty("BrowserType");

    if(Browser_Type.equalsIgnoreCase("chrome"))
    {
        System.setProperty("webdriver.chrome.driver",cnffilered.driverpath());
        driver = new ChromeDriver();
    }

    else if(Browser_Type.equalsIgnoreCase("firefox"))
    {
        driver = new FirefoxDriver();
    }

    else if(Browser_Type.equalsIgnoreCase("internetexplorer") || Browser_Type.equalsIgnoreCase("ie"))
    {
        System.setProperty("webdriver.ie.driver",cnffilered.driverpath());
        driver = new InternetExplorerDriver();
    }

    else
    {
        throw new RuntimeException("Browser type is not specified in configuration.properties"); 
    }

    return Browser_Type;

    }
    }

While executing i am getting Null Pointer Exception

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:42)
at ✽.When he searches for "dress"(src/test/resources/functionalTest/EndtoEndTest.feature:9)

I have understand one thing that may be driver instance is getting null, but what may be the solution for that, Any help?? thanks in Advance.

Ab123
  • 415
  • 2
  • 13
  • 34

1 Answers1

1

The driver that has been initialized in the ConfigFileReader class needs to be passed to the stepdefinition class to initialize the driver variable of that class.

Add the last line to the stepdefinition given method.

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

    ConfigFileReader cnffilered= new ConfigFileReader();
    cnffilered.getBrowserType();
    cnffilered.getUrl();
    cnffilered.Implicitwait();
    cnffilered.MaxmimizeWindow();
    **this.driver = cnffilered.getDriver();**
}

Add method getDriver() in class ConfigFileReader

public WebDriver getDriver() {
    return driver;
}

If they are in the same package make the driver variable in ConfigFileReader protected and access it directly without need for a driver variable in the stepdefinition class.

Grasshopper
  • 8,908
  • 2
  • 17
  • 32
  • Thanks, @Grasshopper it is working now , what is think here as soon as cnffilered.MaxmimizeWindow(); is executed driver initliazation is getting destroyed and that's why it was throwing null pointer exception. am i correct?? – Ab123 Apr 11 '18 at 19:02
  • Great catch and nice explanation !!! – undetected Selenium Apr 11 '18 at 19:06
  • @Ab123 Nope that is not correct. It has got nothing to do with selenium. It is all about access of variables in Java code. Refer to this -- https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html – Grasshopper Apr 11 '18 at 19:11