1

嗨,我有一个全球课程,如下所示:

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概念的某个地方出错了吗?

4

2 回答 2

1

调用 Global 的构造函数时有无限递归。你initialize()的很好。

 public Global()
    {
        BrowserInitializer obj=new BrowserInitializer();
        obj.initialize();
    }

BrowserInitializer 是一个 Global

SoGlobal的构造BrowserInitializer函数调用构造函数(再次调用superie Global的构造函数)

使用类似的东西:

public class BrowserInitializer extends Global 
{
     BrowserInitializer(){
     super();
     this.initialize();
    }

    public void initialize()
    {
        driver=new FirefoxDriver();
        driver.manage().window().maximize();
        wait=new WebDriverWait(driver, 5000);
    }
}

//In test class
Global globalObj=new BrowserInitializer();
于 2013-09-09T07:06:02.423 回答
0

我会以不同的方式处理这个问题,并使用@BeforeTes 注释;

创建一个扩展 Firefox 驱动程序的新 webdriver 类;

public class MyFirefoxDriver extends FirefoxDriver {

    private WebDriverWait wait;

    private WebDriver driver;

    public MyFirefoxDriver()
    {
        super();
        this.manage().window().maximize();
        wait = new WebDriverWait(this, 5000);
    }

    public WebDriverWait getWebDriverWait() {
        return wait;
    }
}

您的测试将如下所示;

public class TestNG1
{
    private MyFirefoxDriver driver;

    @BeforeTest
    public void setUp() {
        driver = new MyFirefoxDriver();
    }

    @Test
    public void login()
    {
        driver.get("someURL");
        driver.findElement(By.id("someid")).sendKeys("someusername");
        driver.findElement(By.id("someid")).sendKeys("somepassword");
        driver.findElement(By.id("someid")).submit();

        //To access the wait;
        WebDriverWait wait = driver.getWebDriverWait();
    }

    @AfterTest
    public void tearDown() {
        driver.quit();
    }
}

我很想进一步创建一个基础测试类;

public abstract class TestNGBase
{
    private MyFirefoxDriver driver;

    public MyFirefoxDriver getDriver() {
        return driver;
    }

    @BeforeTest
    public void setUp() {
        driver = new MyFirefoxDriver();
    }

    @AfterTest
    public void tearDown() {
        driver.quit();
    }
}

在这种情况下,您的测试将如下所示;

public class TestNG1 extends TestNGBase
{
    @Test
    public void login()
    {
        getDriver().get("someURL");
        getDriver().findElement(By.id("someid")).sendKeys("someusername");
        getDriver().findElement(By.id("someid")).sendKeys("somepassword");
        getDriver().findElement(By.id("someid")).submit();

        //To access the wait;
        WebDriverWait wait = getDriver().getWebDriverWait();
    }
}
于 2013-09-09T07:54:34.780 回答