This question already has answers here:
What is a NullPointerException, and how do I fix it?
                            
                                (12个答案)
                            
                    
                2年前关闭。
        

    

这是带有黄瓜注释的类

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)


我知道可能是驱动程序实例变为空的一件事,但是对此有什么解决方案,有什么帮助吗?提前致谢。

最佳答案

需要将已在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类中使用驱动程序变量。

08-20 03:42