Exception Selenium Webdriver TestNg



尝试创建属性文件并从中检索信息,但是正在获得java.lang.nullpointerexception,也尝试尝试捕获,因为编码是非常新的,任何人都可以让我让我知道为什么要获得零指针异常。

package ObjectRepository;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Properties;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
public class PropertiesGuru99Bank {
    WebDriver driver;
      @Test
  public void f()  {
      File file=new File("E:\selenium\Rahul\Project\src\ObjectRepository\object.properties");
      FileInputStream f=null; 
      try {
          f=new FileInputStream(file);
    } catch (Exception e) {
        // TODO: handle exception
        System.out.println(e.getStackTrace());
    }
      Properties prop=new Properties();
        try {
             prop.load(f);
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println(e.getStackTrace());
        }
     driver.get(prop.getProperty("url"));
      driver.findElement(By.id(prop.getProperty("id"))).sendKeys("rahul");
  }
  @BeforeTest
  public void beforeTest() {
    System.setProperty("webdriver.chrome.driver", "E:\selenium\lib\chromedriver_win32\chromedriver.exe");
    new ChromeDriver();   
  }
  @AfterTest
  public void afterTest() {
  }
}

可能的小错字。您没有实例化驾驶员。new ChromeDriver();不插入驱动程序。将driver = new ChromeDriver();或在将驱动程序EXE设置为系统属性后显示。

import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
public class PropertiesGuru99Bank {
    WebDriver driver;
    @Test
    public void f()  {
        File file=new File("E:\selenium\Rahul\Project\src\ObjectRepository\object.properties");
        FileInputStream f=null; 
        try {
            f=new FileInputStream(file);
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println(e.getStackTrace());
        }
        Properties prop=new Properties();
        try {
            prop.load(f);
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println(e.getStackTrace());
        }
        driver.get(prop.getProperty("url"));
        driver.findElement(By.id(prop.getProperty("id"))).sendKeys("rahul");
    }
    @BeforeTest
    public void beforeTest() {
    System.setProperty("webdriver.chrome.driver", "E:\selenium\lib\chromedriver_win32\chromedriver.exe");
     //this is what you are missing
     driver = new ChromeDriver();
    }
    @AfterTest
    public void afterTest() {
    }
}

相关内容

最新更新