2017-05-04 31 views
0

澆道clasas我已經創建了一個特徵文件,然後創建其中i已創建讀取從配置文件中的數據的方法中的硒基類,然後我有在stepdefiniation課堂上給出驅動程序詳細信息。然後,創建轉輪類並執行它.Getting硒基類中的空指針異常。能否請您HEP我出去這我得到顯示java.lang.NullPointerException當我執行在黃瓜

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

回答

0

調用SeleniumBaseclass.property()之前要調用SeleniumBaseclass.getChromepath()。當您調用SeleniumBaseclass.getChromepath()時,prop的值爲null。於是,就會發生NullPointerException異常。

+0

所以我應該在stepdefination類中調用SeleniumBaseclass.property()嗎?請你詳細說明一下 –

+0

你應該以任何方式實例化prop。但最好的辦法是告訴你,你應該在stepdefination類調用SeleniumBaseclass.property() –

相關問題