由于NullPointerException

时间:2019-05-01 13:03:58

标签: java selenium selenium-webdriver testng selenium-chromedriver

当我尝试以testng的身份运行此错误

java.lang.NullPointerException
at java.util.Hashtable.put(Unknown Source)
at java.util.Properties.setProperty(Unknown Source)
at java.lang.System.setProperty(Unknown Source)
at com.w2a.base.TestBase.setUp(TestBase.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:523)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:224)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:146)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:326)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

某些软件包可能是旧的,但是我指的是旧教程,是否是由于我使用的是旧软件包?

  • 测试6.11
  • selenium-java 2.53.1
  • 范围报告2.41.2
  • 报告1.1.4
  • guice 4.1.0
  • log4j 1.2.17
  • 邮件1.4.7
  • mysql-connector-java 6.0.5
  • poi 3.6
  • poi-ooxml 3.6
  • dom4j 1.1
  • xmlbeans 2.3.0
  • poi-ooxml-schemas 3.6

这是我的TestBase类

    public class TestBase {

    public static WebDriver driver;
    public static Properties config = new Properties();
    public static Properties OR = new Properties();
    public static FileInputStream fis;
    @BeforeSuite
    public void setUp() throws IOException {
        if(driver == null) {
             FileInputStream fis = new FileInputStream("C:\\Users\\acer\\eclipse-workspace\\DataDrivenFramework\\src\\test\\resources\\properties\\Config.properties");
             config.load(fis);     
             System.out.println(config.getProperty("browser"));

             fis = new FileInputStream("C:\\Users\\acer\\eclipse-workspace\\DataDrivenFramework\\src\\test\\resources\\properties\\OR.properties");
             OR.load(fis);

        }
                 if(config.getProperty("browser").equals("chrome")) {
                 System.out.println("You won");
                System.setProperty("webdriver.chrome.driver",System.getProperty("C:\\Users\\acer\\eclipse-workspace\\DataDrivenFramework\\src\\test\\resources\\executables\\chromedriver.exe"));
                driver = new ChromeDriver();                

             }
                 System.out.println("You won");
             driver.get(config.getProperty("testsiteurl"));
             driver.manage().window().maximize();
             driver.manage().timeouts().implicitlyWait(Integer.parseInt(config.getProperty("implicit.wait")), TimeUnit.SECONDS);
        //}
    }

    @AfterSuite
    public void tearDown() {
        if(driver != null)
        driver.quit();
    }

}

为什么这个testng测试用例失败?

我正在使用chromedriver.exe 2.28。

1 个答案:

答案 0 :(得分:2)

您必须在System.setProperty中输入值:

System.setProperty("webdriver.chrome.driver","C:\\Users\\acer\\eclipse-workspace\\DataDrivenFramework\\src\\test\\resources\\executables\\chromedriver.exe");