在selenium java中加载属性文件时获取Null指针异常

时间:2018-04-08 10:39:09

标签: java cucumber-java

这是文件阅读器的java代码

save_image

这是包含黄瓜注释的java代码,它作为主要方法

package dataProviders;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class ConfigFileReader {

private Properties properties;
private final String propertyFilePath = System.getProperty("user.dir"+"//src//main//resources//configs/Configurations.properties");

public ConfigFileReader() throws IOException
{

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

}

public String getUrl()
{
    String url = properties.getProperty("url");

    if(url!=null)
    {

        return url;

    }

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

public String driverpath()
{

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

    if(driverpath!=null)
    {

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

}

}

执行我的Cucumber功能文件时,我遇到异常,我尝试了很多东西,但仍然不知道为什么它会抛出空指针异常

package stepDefinations;

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

import junit.framework.Assert;

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;
import dataProviders.ConfigFileReader;

public class EndtoEndTest {

WebDriver driver;
ConfigFileReader cnffilered;


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


    cnffilered = new ConfigFileReader();
    System.setProperty("webdriver.chrome.driver",cnffilered.driverpath());
    driver = new ChromeDriver();
    driver.get(cnffilered.getUrl());
    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();
}
}

任何帮助将不胜感激。提前谢谢。

java.lang.NullPointerException
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at dataProviders.ConfigFileReader.<init>(ConfigFileReader.java:17)
at stepDefinations.EndtoEndTest.user_is_on_Homepage(EndtoEndTest.java:32)
at ✽.Given User is on Homepage(src/test/resources/functionalTest/EndtoEndTest.feature:8)

1 个答案:

答案 0 :(得分:0)

您的属性属性是在类ConfigFileReader中初始化的吗?

私有属性属性;

阅读完代码后,似乎属性对象始终为null。你不是从外面注射它,也不是初始化它。

请尝试propertyFilePath 另外,您没有传递private final String propertyFilePath = System.getProperty("user.dir") + "//src//main//resources//configs/Configurations.properties"; 变量的正确值。错误的连接。 正确一个将如下

{{1}}