【问题标题】:I'm getting java.lang.NullPointerException when i executed the runner clasas in cucumber [duplicate]当我在黄瓜中执行跑步者类时,我得到了 java.lang.NullPointerException [重复]
【发布时间】:2017-05-04 04:27:15
【问题描述】:

我创建了一个功能文件,然后创建了 selenium 基类,我创建了一个从配置文件中读取数据的方法,然后我在 stepdefinition 类中给出了驱动程序的详细信息。然后创建 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");
        }

【问题讨论】:

    标签: java selenium junit cucumber-jvm


    【解决方案1】:

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

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

    【讨论】:

    • 那么我应该在 stepdefination 类中调用 SeleniumBaseclass.property() 吗?你能详细说明一下吗
    • 你应该以任何方式实例化prop。但最好的方法是如你所说,你应该在 stepdefination 类中调用 SeleniumBaseclass.property()
    猜你喜欢
    • 2018-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    • 2018-12-19
    • 1970-01-01
    相关资源
    最近更新 更多