Mrunal Gosar
Mrunal Gosar

Reputation: 4683

initialize a webdriver instance

Hi i have a global class as belows:

public class Global 
{
    public WebDriver driver=null;
    public WebDriverWait wait=null;

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

I have another class called browser initializer where i want to initialize the driver instance to firefox as below:

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

I have testNG class where i want to run some test as belows :

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();
    }
}

Now i am getting error like :

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)

but when i am initializing the driver instance in the constructor of the Global class i am able to run my test. what seems to be the problem? am i going wrong somewhere in the concepts of java?

Upvotes: 1

Views: 25072

Answers (2)

Robbie Wareham
Robbie Wareham

Reputation: 3428

I woudl approach this in a different way, and use a @BeforeTes annotation;

Create a new webdriver class which extends Firefox driver;

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;
    }
}

Your test would then look like this;

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();
    }
}

I would be tempted to then further create a base test class;

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();
    }
}

In which case you test would then look like this;

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();
    }
}

Upvotes: 0

rocketboy
rocketboy

Reputation: 9741

You have infinite recursion when calling Global's constructor. Your initialize() is perfectly fine.

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

BrowserInitializer is a Global.

So Global's contructor calls BrowserInitializer's constructor (which calls super's i.e Global's constructor again)

Use something like:

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();

Upvotes: 1

Related Questions