在java中使用selenium webdriver运行黄瓜测试时获取Null指针异常

时间:2018-04-11 18:10:56

标签: java selenium cucumber

这是带黄瓜注释的类

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

}
}

这是具有FageFactory初始化的类

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

}

驱动程序初始化

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;

    }
    }

执行时我得到空指针异常

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)

我理解一件事可能是驱动程序实例变为null,但可能是什么解决方案,任何帮助?提前致谢。

1 个答案:

答案 0 :(得分:1)

需要将已在ConfigFileReader类中初始化的驱动程序传递给stepdefinition类,以初始化该类的驱动程序变量。

将最后一行添加到stepdefinition给定方法。

@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();**
}

在班级getDriver()

中添加方法ConfigFileReader
public WebDriver getDriver() {
    return driver;
}

如果它们位于同一个包中,则使ConfigFileReader中的驱动程序变量受到保护并直接访问它,而不需要在stepdefinition类中使用驱动程序变量。