当我在黄瓜中执行转子clasas时,我得到了java.lang.NullPointerException

时间:2017-05-04 04:27:16

标签: java selenium junit cucumber-jvm

我创建了一个特征文件,然后创建了selenium基类,我在其中创建了一个从配置文件中读取数据的方法,然后我在stepdefiniation类中给出了驱动程序的详细信息。然后创建了runner类并执行它。在selenium基类中获取Null指针异常。你能不能请我这个

//Selenium Base class
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.testng.annotations.Test;

public class SeleniumBaseclass
{

    Properties prop;
 public void Property()
    {

        try
        {
            File f= new File("C:\\Users\\watareuman9\\workspace\\Cucumberproject\\src\\test\\resources\\data\\config.property");

            FileInputStream fis = new FileInputStream(f);
             prop=new Properties();

                    prop.load(fis);
                } catch (Exception e) {

                    e.printStackTrace();
                }
        }



        public String getChromepath()
        {
            return prop.getProperty("ChromePath");
        }

        public String getAppurl()
        {
            return prop.getProperty("URL");
        }

    }


//stepdefination file:

    package Stepdfinations;

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

    import cucumber.api.java.en.Given;

    public class Accountstepdefinations  {

        @Given("^I navigated to the Login page$")
        public void i_navigated_to_the_Login_page() 
        {
            SeleniumBaseclass b1=new SeleniumBaseclass();
            System.setProperty("webdriver.chrome.driver", b1.getChromepath());
            WebDriver driver=new ChromeDriver();
            driver.get(b1.getAppurl());

        }

        //@When("^I click on the New Account link$")
        //public void i_click_on_the_New_Account_link() throws Throwable {
            // Write code here that turns the phrase above into concrete actions
           // throw new PendingException();


        //@Then("^I should see the New Account registration page$")
        //public void i_should_see_the_New_Account_registration_page() throws Throwable {
            // Write code here that turns the phrase above into concrete actions
        //    throw new PendingException();
        }

    //
    //}

//Runner clasas:

package Stepdfinations;

import org.junit.runner.RunWith;

import cucumber.api.junit.Cucumber;
import cucumber.api.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(features="src/test/resources/features")



public class Runner 
{


}



//Error appearing as null exception in the following method:

public String getChromepath()
        {
            return prop.getProperty("ChromePath");
        }

1 个答案:

答案 0 :(得分:0)

在调用SeleniumBaseclass.getChromepath()之前,您正在调用SeleniumBaseclass.property()

因此,当您调用prop时,null的值为SeleniumBaseclass.getChromepath()。因此,发生了NullpointerException。