【问题标题】:Exception Selenium Webdriver TestNg [duplicate]异常 Selenium Webdriver TestNg [重复]
【发布时间】:2015-07-19 16:17:30
【问题描述】:

试图创建属性文件并从中检索信息,但得到一个 java.lang.NullPointerException,还尝试了 try and catch,因为我对编码非常陌生,谁能告诉我为什么会出现空指针异常。

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() {
  }
}

【问题讨论】:

    标签: java selenium


    【解决方案1】:

    可能是小错字。您没有实例化驱动程序。 new ChromeDriver(); 不会实例化驱动程序。在将驱动程序 exe 设置为系统属性后执行driver = new 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");
         //this is what you are missing
         driver = new ChromeDriver();
        }
    
        @AfterTest
        public void afterTest() {
        }
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    • 2018-09-08
    相关资源
    最近更新 更多