Webdriver TestNG NullPointer异常

时间:2014-04-22 18:06:38

标签: java selenium selenium-webdriver webdriver testng

我正在尝试运行TestNG测试用例,这是Interdependent在旅行网站中说的测试用例是第1次)登录第2)预订3)取消等。

我在第二次测试时调用Webdriver时遇到'NullPointer异常'。 任何想法,因为我也已将Driver声明为公共静态。 我正在从属性文件中读取定位器。

这是一个TestNG错误吗?

这是我的代码,请向下滚动以查看Pointer异常..

公共类LoginTest {

   public static  WebDriver driver;
   public static  Properties p;
   public static  FileInputStream f ;   

    @Test
    public void loginTest()  {
        System.out.println("Enter LoginTest");
    Properties p=new Properties();
    FileInputStream f = null;
    try {
        f = new FileInputStream("D:\\BOSSFramework\\Framework\\locators.properties");
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        p.load(f);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    WebDriver driver = new FirefoxDriver();
    driver.get("https://in3.seatseller.travel/");
    driver.manage().window().maximize();


     // Login Process Starts here ..
    try{
        driver.findElement(By.name(p.getProperty("login.username.textfield"))).sendKeys("user");
        driver.findElement(By.name(p.getProperty("login.password.textfield"))).sendKeys("password");
        WebElement ele =driver.findElement(By.id(p.getProperty("login.signin.button")));
        ele.click();
        /*String classValue = ele.getAttribute("class");*/
            }
         catch (Exception e) {

            }       

    }
          @Test (dependsOnMethods={"loginTest"})
             public void booking() throws InterruptedException{
        System.out.println("Enter Booking");
        // Type Bangalore on Source Field.. 
        Properties p=new Properties();
        FileInputStream f = null;
        try {
            f = new FileInputStream("D:\\BOSSFramework\\Framework\\locators.properties");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            p.load(f);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          /*Null Pointer is on Below Line*/
            WebDriverWait wait = new WebDriverWait(driver, 30);
            wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(p.getProperty("oneapp.source.textfield"))));
            driver.findElement(By.xpath(p.getProperty("oneapp.source.textfield"))).sendKeys("Bangalore");
            driver.findElement(By.xpath(p.getProperty("oneapp.source.textfield"))).sendKeys(Keys.TAB);
            Thread.sleep(900L);

1 个答案:

答案 0 :(得分:2)

问题是您在WebDriver driver中声明了loginTest(),然后尝试在loginTest()中引用driver booking()的{​​{1}}实例。

如果按如下方式修改loginTest(),则应该有效:

driver = new FirefoxDriver();