java - TestNg 由于 NullPointerException 失败

标签 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。

最佳答案

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

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

关于java - TestNg 由于 NullPointerException 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55936722/

相关文章:

java - 覆盖 Tomcat 基本身份验证

Python 找不到用于 Spotify 滚动框的 xpath

javascript - Selenium "NoSuchAlertError: no alert open"错误

java - 替代 Java 中的固定大小数组?

java - 如何每隔 2 小时自动启动 Tanuki Software Wrapper

selenium - 如何在 XPath 中使用索引

python - 我尝试在 Kickstarter 上获取不同项目的支持者和所在城市

python - 如何在 Python 中使用 BeautifulSoup 通过文本查找特定标签

java - JNI : Convert Java String to Code Page 1252

java - 如何使用selenium web driver java点击一个包含某些单词作为文本的href