4

I am testing heavy web application on gwt. Test creates new instance of webdriver on each start, opens browser and loads a page.

Some time after, test becomes passed. I don't close browser after test, but when I rerun test, it initializes browser again. Is there any way to get instance of loaded browser and load the page on it?

upd:

I've tried to do:

public class BaseTest {
    protected static ApplicationManager app;

    @Before
    public void setUp() throws Exception {
        if (app == null) {
            app = new ApplicationManager();
            app.getLoginHelper().login();
        }
    }
}

And it is null on each start.

upd2:

I need something like:

initializating new web driver
running test1
running test2
running test3
...
all test are finished

and after rerun:

using previosly launched driver
running test1
running test2
running test3
...
all test are finished
Fr0stDev1
  • 143
  • 2
  • 15

2 Answers2

0

You can have a class with a static part. You can initialize your driver in this class (that have to be static too). After that you can extends your test classes with this class in order to have you driver instance everywhere.

public class Context{
    protected static WebDriver driver;
    static {
        driver = new FirefoxDriver();
        //Do whatever you want.
    }
}

public class MyTestClass extends Context{
    @Test
    public void test1(){
        driver.something();
    }
}
Nicolas G. Duvivier
  • 498
  • 1
  • 6
  • 18
  • No, I did the same: public class BaseTest { protected static ApplicationManager app; @Before public void setUp() throws Exception { if (app == null) { app = new ApplicationManager(); app.getLoginHelper().login(); } } } – Fr0stDev1 Dec 16 '15 at 12:29
  • Where do you think it's the same ? For me, it's completely different. More information about static block here : http://stackoverflow.com/questions/9130461/when-is-the-static-block-of-a-class-executed – Nicolas G. Duvivier Dec 16 '15 at 12:43
  • Yes, there is a difference between static block and static variable, but in my case they work same: after relaunching test all context clears. – Fr0stDev1 Dec 16 '15 at 14:00
  • Oh, my bad, I didn't see that you were rerunning your tests – Nicolas G. Duvivier Dec 16 '15 at 14:03
  • Take a look at RemoteWebDriver. --> Take a look right here : https://code.google.com/p/selenium/issues/detail?id=3927 – Nicolas G. Duvivier Dec 16 '15 at 14:26
0
public class DriverBuilder {
    private WebDriver driver;
        DriverBuilder() {
        try {
            driver = new FirefoxDriver();
        } catch (Exception e) {
            throw new ExceptionInInitializerError(e);
        }
    }   
    public WebDriver getDriver() 
    {
        return driver;
    }
}

And use the same WebDriver instance whereever required as below:

WebDriver driver = DriverBuilder.getDriver();
Abhinav
  • 1,037
  • 6
  • 20
  • 43