嗨,我有一个全球课程,如下所示:
public class Global
{
public WebDriver driver=null;
public WebDriverWait wait=null;
public Global()
{
BrowserInitializer obj=new BrowserInitializer();
obj.initialize();
}
}
我有另一个名为浏览器初始化程序的类,我想将驱动程序实例初始化为 firefox,如下所示:
public class BrowserInitializer extends Global
{
public void initialize()
{
driver=new FirefoxDriver();
driver.manage().window().maximize();
wait=new WebDriverWait(driver, 5000);
}
}
我有 testNG 类,我想在其中运行一些测试,如下所示:
public class TestNG1
{
Global globalObj=new Global();
@Test
public void login()
{
globalObj.driver.get("someURL");
globalObj.driver.findElement(By.id("someid")).sendKeys("someusername");
globalObj.driver.findElement(By.id("someid")).sendKeys("somepassword");
globalObj.driver.findElement(By.id("someid")).submit();
}
}
现在我收到如下错误:
org.testng.TestNGException:
Cannot instantiate class unitTest.myTest.TestNG1
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:38)
at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:387)
at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:299)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:110)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:186)
at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:120)
at org.testng.TestRunner.initMethods(TestRunner.java:409)
at org.testng.TestRunner.init(TestRunner.java:235)
at org.testng.TestRunner.init(TestRunner.java:205)
at org.testng.TestRunner.<init>(TestRunner.java:160)
at org.testng.remote.RemoteTestNG$1.newTestRunner(RemoteTestNG.java:141)
at org.testng.remote.RemoteTestNG$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG.java:271)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:577)
at org.testng.SuiteRunner.init(SuiteRunner.java:157)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:111)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1299)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1286)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
... 21 more
Caused by: java.lang.StackOverflowError
at unitTest.myTest.Global.<init>(Global.java:12)
at unitTest.myTest.BrowserInitializer.<init>(BrowserInitializer.java:6)
at unitTest.myTest.Global.<init>(Global.java:14)
但是当我在 Global 类的构造函数中初始化驱动程序实例时,我能够运行我的测试。似乎是什么问题?我在java概念的某个地方出错了吗?