Reputation: 517
I'm currently using Cucumber and Selenium WebDriver in Java to test a web application. I'm not very happy with the fact that the browser is closed and reopened between each test cases.
First, it's pretty slow and in addition, it makes no sense to me.
What I want to do is to log off the app between each tests and keep the browser open.
I thought that this line could do the work, but it doesn't work as expected :
driver.navigate().to("http://myurl.url");
Instead of :
driver.get("http://myurl.url");
It opens a new browser. I understand why it does this, but I want to attach to my previous session of my browser.
Upvotes: 4
Views: 9457
Reputation: 517
I found a way to keep my browser open between tests. I used the Singleton Design Pattern
with picocontainer
.
I declare my browser in static
in this class.
public class Drivers {
private static boolean initialized = false;
private static WebDriver driver;
@Before
public void initialize(){
if (!initialized){
initialized = true;
driver = new FirefoxDriver();
driver.get("http://myurl.url");
}
}
public static WebDriver getDriver(){
return driver;
}
}
In each StepDefinitions
class, I have a constructor that instantiates my Singleton
.
public class DashboardSteps extends SuperSteps {
Drivers context;
@After
public void close(Scenario scenario) {
super.tearDown(scenario);
loginPage = homePage.clickLogout();
loginPage.waitPageLoaded();
}
public DashboardSteps(Drivers context) {
super(context);
}
The SuperSteps class :
public class SuperSteps {
protected Drivers context;
public SuperSteps(Drivers context){
this.context = context;
}
As you can see, I go back to the logIn Page after each test, so I can run every tests independently.
Upvotes: 7
Reputation: 197
You could use the @BeforeClass
and @AfterClass
annotations. They run before the @Before and after the @After respectively. Assuming the test cases you are trying to test are in the same class, you could initialize your WebDriver
in the @BeforeClass
method and close it in the @AfterClass
method.
Upvotes: 0
Reputation: 4683
If i m correct ur browser instantiation might be happening in a @Before
marked setUp
method and closing would be happening in @After
marked tearDown
method. If you want to stop closing of browser then just comment out that code in tearDown or whereever you are doing it.
Upvotes: 0