首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >尝试所有仍面临- system.property org.testng.TestNGException的解决方案后:无法实例化类

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

Stack Overflow用户
提问于 2018-08-06 03:30:31
回答 2查看 56关注 0票数 0

在运行我的第一个TestNG程序时,我得到了上面的错误(org.testng.TestNGException: Cannot instantiate class)。我已经检查了所有的解决方案,但问题仍然存在。

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

下面是我的课程:我的问题没有被张贴,因为内容解释较少,尽管我写了很多东西,所以也添加了这个。宽恕我吧。

代码语言:javascript
运行
复制
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()?

代码语言:javascript
运行
复制
[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

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

EN

Stack Overflow用户

发布于 2018-08-06 08:47:29

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

代码语言:javascript
运行
复制
@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实例。

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51697597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档