Selenium 在尝试了所有仍然面临的解决方案之后-system.property org.testng.TestNGException:无法实例化类

Selenium 在尝试了所有仍然面临的解决方案之后-system.property org.testng.TestNGException:无法实例化类,selenium,Selenium,我在运行第一个testng程序时遇到上述错误(org.testng.TestNGException:无法实例化类)。我已经通过了所有的解决方案,但问题仍然存在 我已经下载了ChromeDriver,使用syste.property在类中设置它的正确路径。还添加了所需的罐子。我已经为chrome驱动程序设置了system.SetProperty(),但仍然得到相同的错误。我是否需要在pom.xml中添加任何特定的依赖项,或者需要在类中添加main() 下面是我的课堂:我的问题没有被发布,因为即使我

我在运行第一个testng程序时遇到上述错误(
org.testng.TestNGException:无法实例化类
)。我已经通过了所有的解决方案,但问题仍然存在

我已经下载了ChromeDriver,使用syste.property在类中设置它的正确路径。还添加了所需的罐子。我已经为chrome驱动程序设置了system.SetProperty(),但仍然得到相同的错误。我是否需要在pom.xml中添加任何特定的依赖项,或者需要在类中添加main()

下面是我的课堂:我的问题没有被发布,因为即使我写了很多东西,内容解释也很少,所以也添加了这个。请原谅我

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

/**
* Unit test for simple App.
*/
public class AppTest {
/**
* Create the test case
*
* @param testName
* name of the test case
*/


public AppTest(String testName) {
        super();
        System.setProperty("webdriver.chrome.driver", "C:/Users/Dell/Downloads/chromedriver_win32/chromedriver.exe");
}



// public static void main()
// {
//
// }

public WebDriver driver = new ChromeDriver();
String appUrl = "https://google.com";

@Test
public void gmailLogin() {
    // launch the fire fox browser and open the application url
    driver.get(appUrl);
    System.out.println("Suceessfully opened the browser URL");

    // maximize the browser window
    driver.manage().window().maximize();

    // declare and initialize the variable to store the expected title of
    // the web page.
    String expectedTitle = "Sign in - Google Accounts";

    // fetch the title of the web page and save it into a string variable
    String actualTitle = driver.getTitle();
    Assert.assertEquals(expectedTitle, actualTitle);

    // enter a valid user name in the email text box
    WebElement username = driver.findElement(By.id("Email"));
    username.clear();
    username.sendKeys("TestSelenium");

    // enter a valid password in the password text box
    WebElement password = driver.findElement(By.id("Passwd"));
    password.clear();
    password.sendKeys("password123");

    // click on the Sign in button
    WebElement SignInButton = driver.findElement(By.id("signIn"));
    SignInButton.click();

    // close the web browser
    driver.close();
    }

}
下面是错误日志:我为chrome驱动程序设置了
System.setProperty()
,但仍然收到相同的错误。我是否需要在pom.xml中添加任何特定的依赖项,或者需要在类中添加main()

[RemoteTestNG] detected TestNG version 6.14.2
org.testng.TestNGException: 
Cannot instantiate class com.selenium.SampleDemo1.AppTest
    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.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 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:24)
    ... 25 more
Caused by: java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html
[RemoteTestNG]检测到TestNG版本6.14.2
org.testng.TestNGException:
无法实例化类com.selenium.SampleDemo1.AppTest
位于org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:30)
位于org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:423)
位于org.testng.internal.ClassHelper.createInstance(ClassHelper.java:336)
位于org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:125)
位于org.testng.internal.ClassImpl.getInstances(ClassImpl.java:190)
位于org.testng.TestClass.getInstances(TestClass.java:95)
位于org.testng.TestClass.initTestClassesAndInstances(TestClass.java:81)
位于org.testng.TestClass.init(TestClass.java:73)
位于org.testng.TestClass.(TestClass.java:38)
位于org.testng.TestRunner.initMethods(TestRunner.java:389)
位于org.testng.TestRunner.init(TestRunner.java:271)
位于org.testng.TestRunner.init(TestRunner.java:241)
位于org.testng.TestRunner.(TestRunner.java:192)
位于org.testng.remote.support.RemoteTestNG6_12$1.newTestRunner(RemoteTestNG6_12.java:33)
位于org.testng.remote.support.RemoteTestNG6_12$delegatingestrunnerfactory.newestrunner(RemoteTestNG6_12.java:66)
位于org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:713)
位于org.testng.SuiteRunner.init(SuiteRunner.java:260)
位于org.testng.testng.run(testng.java:1017)
位于org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
位于org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
位于org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
原因:java.lang.reflect.InvocationTargetException
位于sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)
位于sun.reflect.NativeConstructorAccessorImpl.newInstance(未知源)
位于sun.reflect.delegatingConstructor或AccessorImpl.newInstance(未知源)
位于java.lang.reflect.Constructor.newInstance(未知源)
位于org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:24)
... 25多
原因:java.lang.IllegalStateException:驱动程序可执行文件的路径必须由webdriver.chrome.driver系统属性设置;有关详细信息,请参阅https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. 最新版本可从以下网站下载:http://chromedriver.storage.googleapis.com/index.html

我已经为chrome驱动程序设置了system.SetProperty(),但仍然得到相同的错误。我是否需要在pom.xml中添加任何特定的依赖项,或者需要在类中添加main()?我已经为chrome驱动程序设置了system.SetProperty(),但仍然得到相同的错误。我是否需要在pom.xml中添加任何特定的依赖项,或者需要在类中添加main()?

根据当前代码,在执行过程中没有调用为chrome驱动程序设置系统属性的AppTest构造函数方法。您可以使用@BeforeClass注释添加新的
setUpAppTest
方法,以便在初始化chrome驱动程序之前为chrome设置系统属性,如下所示:

[RemoteTestNG] detected TestNG version 6.14.2
org.testng.TestNGException: 
Cannot instantiate class com.selenium.SampleDemo1.AppTest
    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.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 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:24)
    ... 25 more
Caused by: java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html
@BeforeClass
public void setUpAppTest(String testName) {
    System.setProperty("webdriver.chrome.driver", "C:/Users/Dell/Downloads/chromedriver_win32/chromedriver.exe");
}

您必须导入
org.testng.annotations.BeforeClass
,才能使用BeforeClass。此外,我建议您使用
driver.quit()而不是
driver.close()以确保正确清理webdriver实例。

我发现上述方法的实现方式存在一些问题。上面的代码更多的是顺序流,您将在代码的后面部分定义初始化,因此您将看到上面的异常

  • 尝试创建一个超类,其中需要初始化驱动程序、系统属性来运行测试用例。例如:(定义如下所示的驱动程序类)
  • 使用上述代码,您可以设置所有系统属性和所需的初始化。确保定义的方法具有适当的模块化

  • 在您的测试用例中

  • 上面的实现确保所有初始化都正确进行。

    您能从任何实例方法中这样调用super()吗?
    AppTest
    是一个构造函数,我们可以在其中调用超类构造函数。在这种特殊情况下,调用超类构造函数似乎不会增加任何值,因为没有为
    AppTest
    定义超类,因此它将调用默认的对象类构造函数。然而,我刚刚意识到,我们不能将@BeforeClass与构造函数方法一起使用,因此需要更正代码。感谢@cruisepandey的支持。我的下一个问题是“我们可以在构造函数上使用@BeforeClass吗?”?可能是OP没有扩展当前类。我们不能在构造函数上使用@BeforeClass,因此我建议使用新的设置方法。OP有构造函数,他需要澄清。我知道
    我们不能在构造函数上使用@BeforeClass
    看起来
    chromedriver.exe的路径无效或者有一些权限
    
    public class testClass extends OnUIStart {
    }