有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Webdriver TestNG空指针异常

我正在尝试运行相互依赖的TestNG测试用例,比如在旅游网站中测试用例是1)登录2)预订3)取消等

在第二次测试中调用Webdriver时,我面临“NullPointer异常”。。 任何想法,因为我也宣布驱动程序为公共静态。 我正在从属性文件中读取定位器

这是一个TestNG错误吗

这是我的代码,请向下滚动查看指针异常

公共类登录测试{

   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) 个答案

  1. # 1 楼答案

    问题是您在loginTest()中声明了WebDriver driver,然后试图引用booking()driverloginTest()实例

    如果按如下方式修改loginTest(),它应该可以工作:

    driver = new FirefoxDriver();