我在使用Java(使用Maven项目)在Selenium Webdriver中运行我的一个测试用例时遇到了“无法实例化类”错误。

下面是我定义驱动程序和属性的父类

package com.pvi.qa.base;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class TestBase {

    public static WebDriver driver;
    public static Properties prop ;

    public TestBase() {
        try {
            prop = new Properties();
            FileInputStream ip = new FileInputStream(System.getProperty("C:\\Users\\RxLogix\\eclipse-workspace\\PviIntake\\src\\main\\java\\com\\pvi\\qa\\config\\config.properties"));
        prop.load(ip);

        }
        catch(FileNotFoundException e) {
            e.printStackTrace();
        }
        catch(IOException e) {
            e.printStackTrace();
        }
    }

public static void initialization () {
String BrowserName = prop.getProperty("browser");
if(BrowserName.equals("chrome")) {
    System.setProperty("webdriver.chrome.driver", "D:\\Softwares\\chromedriver_win32 (1)\\chromedriver.exe");
    driver = new ChromeDriver();}
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(prop.getProperty("url"));
}
}


下面是我通过TestNG运行的测试用例类

package com.pvi.qa.testcases;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.pvi.qa.base.TestBase;
import com.pvi.qa.pages.HomePage;
import com.pvi.qa.pages.LoginPage;

public class LoginPageTest extends TestBase{
    LoginPage loginPage;
    HomePage homePage;


    public LoginPageTest() {
        super();
        }

    @BeforeMethod
    public void setUp() {
        initialization();
        loginPage = new LoginPage();
    }


    @Test
    public void logintest() {
        homePage = loginPage.login(prop.getProperty("username"), prop.getProperty("password"));
    }

    @AfterMethod
    public void tearDown() {
        driver.quit();
    }

}


下面是我得到的错误-

[RemoteTestNG] detected TestNG version 6.14.3
org.testng.TestNGException:
Cannot instantiate class com.pvi.qa.testcases.LoginPageTest
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:30)
    at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:423)
    at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:336)
    at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:125)
    at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:190)
    at org.testng.TestClass.getInstances(TestClass.java:95)
    at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:81)
    at org.testng.TestClass.init(TestClass.java:73)
    at org.testng.TestClass.<init>(TestClass.java:38)
    at org.testng.TestRunner.initMethods(TestRunner.java:389)
    at org.testng.TestRunner.init(TestRunner.java:271)
    at org.testng.TestRunner.init(TestRunner.java:241)
    at org.testng.TestRunner.<init>(TestRunner.java:192)
    at org.testng.remote.support.RemoteTestNG6_12$1.newTestRunner(RemoteTestNG6_12.java:33)
    at org.testng.remote.support.RemoteTestNG6_12$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_12.java:66)
    at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:713)
    at org.testng.SuiteRunner.init(SuiteRunner.java:260)
    at org.testng.SuiteRunner.<init>(SuiteRunner.java:198)
    at org.testng.TestNG.createSuiteRunner(TestNG.java:1295)
    at org.testng.TestNG.createSuiteRunners(TestNG.java:1273)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1128)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.base/java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:24)
    ... 25 more
Caused by: java.lang.NullPointerException
    at java.base/java.io.FileInputStream.<init>(Unknown Source)
    at java.base/java.io.FileInputStream.<init>(Unknown Source)
    at com.pvi.qa.base.TestBase.<init>(TestBase.java:20)
    at com.pvi.qa.testcases.LoginPageTest.<init>(LoginPageTest.java:17)
    ... 30 more


我已经尝试了所有方法,但没有为我工作,请让我知道为什么会收到此错误。谢谢

最佳答案

最低的“ caused”部分将您指向基类构造函数中的基础异常:您正在使用文件路径调用System.getProperty;可能会返回null。对我来说,似乎整个调用不应该存在,而您只想将文件路径传递给FileInputStream构造函数(或使用某些键从系统属性读取它)

10-06 11:24