Vidya Shankar NS
Vidya Shankar NS

Reputation: 59

driver.quit() does not close all browser windows

Here's the code

WebDriver driver = new FirefoxDriver();
driver = new FirefoxDriver();

This opens two firefox windows. In the @AfterMethod, I am calling

driver.quit();

In-spite of this, the first browser window does not close. I tried getting the windowhandles but only one window handle is returned. Is there anyway I can close both the broswer windows?

Upvotes: 0

Views: 1286

Answers (3)

Renuka Deshpande
Renuka Deshpande

Reputation: 174

You are opening browser two times, with same object, apart from it if you want to use single object and if it opens two different web pages after doing some process then it will work, in case of your scenario, create two different object and one by one try to quit it, it will work.

Upvotes: 1

optimistic_creeper
optimistic_creeper

Reputation: 2799

Your are creating a new instance of a browser and assigning it to the previous driver reference. Hence, calling driver.quit() will closes all windows related to the second instance but not the first one.To do this, you may instantiate two WebDriver references in two places if you need like this:

WebDriver driver1 = new FirefoxDriver();
WebDriver driver2= new FirefoxDriver();

& call quit() method for two drivers:

driver1.quit();
driver2.quit();

But if you need only one instance of it then avoid reassigning.

Upvotes: 0

CyberClaw
CyberClaw

Reputation: 445

You are opening two windows on purpose? If so, use 2 variables one for each window. Or, close the first window before creating a new window.

You are assigning a variable to a new FireFox window. Then you assign the same variable to a new window. You lost your connection to the first window because you stored the new window in the variable.

This would work:

WebDriver driver = new FirefoxDriver();
WebDriver driver2 = new FirefoxDriver();
(...)
driver.quit();
driver2.quit();

This would work too:

WebDriver driver = new FirefoxDriver();
(...)
driver.quit();
driver = new FirefoxDriver();
(...)
driver.quit();

Upvotes: 1

Related Questions