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

时间:2018-08-05 19:30:31

标签: selenium

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

我已经下载了ChromeDriver,并使用syste.property在课堂上设置了正确的路径。还添加了所需的罐子。我已经为chrome驱动程序设置了system.setproprty(),仍然会得到相同的错误。我需要在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

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

2 个答案:

答案 0 :(得分:0)

您正在设置chrome驱动程序的系统属性的 AppTest 构造函数方法在执行期间不会按照当前代码被调用。您可以添加带有 @BeforeClass 批注的新setUpAppTest方法,以便可以在初始化chrome驱动程序之前为chrome设置system属性,如下所示:

@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实例。

答案 1 :(得分:0)

我发现上述方法的实施方式几乎没有问题。上面的代码是顺序流程的更多内容,您将在代码的后半部分定义初始化,因此您会看到上面的异常。

  1. 尝试拥有一个超类,其中需要初始化驱动程序,系统属性才能运行测试用例。例如:(定义如下所示的驱动程序类)
public class OnUIStart{
 protected void starting(Description description) {
        super.starting(description);
        if (!DriverManager.INSTANCE.getDriver().isDriverStarted()) {
            String testBrowser = System.getenv("TEST_BROWSER");
            BrowserType browserType;
            if (!StringUtils.isBlank(testBrowser)) {
                browserType = BrowserType.valueOf(testBrowser.toUpperCase());
            } else {
                browserType = BrowserType.CHROME;
            }
            DriverManager.INSTANCE.setDriverConfiguration(getDriverConfiguration(browserType));
            DriverManager.INSTANCE.startDriver();
            String url = LocalConfig.INSTANCE.getUrlWithProtocol();
            ChangeResolution.setDesktop();
            log.info("URL: " + url);
            DriverManager.INSTANCE.get(url);
        }
    }
}

使用以上代码,您可以设置所有系统属性和所需的初始化。确保您对定义的方法进行了适当的模块化。

  1. 在您的测试用例中
public class testClass extends OnUIStart {
}

在上述实现中,请确保所有初始化均正确进行。