org.testng.TestNGException:无法实例化类

问题描述 投票:0回答:4

我在使用 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

我已经尝试了所有方法但对我不起作用,请告诉我为什么会出现此错误。谢谢

java eclipse selenium testng
4个回答
4
投票

最低的“原因”部分将您指向基类构造函数中的底层异常:您正在使用文件路径调用

System.getProperty
;那可能会返回空值。在我看来,整个调用不应该存在,您只想将文件路径传递给
FileInputStream
构造函数(或使用一些键从系统属性中读取它)


1
投票

查看代码,我知道问题出在基类的构造函数中。 LoginPageTest 调用调用 TestBase 的 super()。

FileInputStream 需要一个文件路径。我怀疑创建的 FileInputSteam 对象为空。

为什么要调用 System.getProperty? config.properties 文件可用吗?


0
投票

只需从您的基类中删除

System.getproperty
。使用这行代码

 prop = new Properties();

FileInputStream ip = new FileInputStream(("C:\\Users\\RxLogix\\eclipse-workspace\\PviIntake\\src\\main\\java\\com\\pvi\\qa\\config\\config.properties"));

prop.load(ip);

0
投票

如果您的自动化套件使用 Jdk_1.8,则可能是以下两个错误之一:-

 1. java.lang.AbstractMethodError: org.openqa.selenium.remote.service.DriverService$Builder.loadSystemProperties()V

 2. org.testng.TestNGException: 
Cannot instantiate class tests.mobiquityPayX.payXUAT.channelApp.BillPayChannelTest

解决方案: 尝试升级最新的 java-client 依赖项。可能是 解决你的问题..

© www.soinside.com 2019 - 2024. All rights reserved.