Close v/s Quit in Selenium – User friendly Tech help

Scenario: –
nWe want to understand the difference between Close, Quit & Dispose commands?n

Laymen terms: –

n

We are going for sleep inside our gorgeous house and charming room . It is really hot today but we can feel some cool breeze, so we would prefer not to close the windows of the house, but we simply need to close our entryway. After some time we realized that cool breeze has taken a shape of massive storm, immediately we need to close all the windows to feel safe. But we are really sleepy and lethargic so we asked our accomplice to close all the windows and we slept peacefully.
n

n

n

n

n

n

n

n

n

My Lovely House

n

1. Closing only the entryway is similar to closing only the current instance of browser on which selenium is working, it is a Close command.
n
n2. Closing all windows on storm was Quit command, while asking our partner to do it was Dispose command. Thus internally Quit is calling Dispose command.

n

Enough after that boring and sleepy story let’s dive straight into technical terms,

n

Close: – This method is used to close the current open window. It closes the current open window on which driver has focus on.
n
nQuit: – This method is used to destroy the instance of WebDriver. It closes all Browser Windows associated with that driver and safely ends the session. Quit() calls Dispose method to achieve this.
n
nDispose: – This method closes all Browser windows and safely ends the session

n

Usage:-
nSuppose if we are working with only one window, close () and quit () technically do exactly same thing that is closes the browser/window and releases the memory by quitting the driver. But if in our test, it opens another window while execution and we wish to close one of the windows, to continue execution; we can switch the focus to desired window and perform close action on it. On other hand if we are done with the execution and we want all the browser instances to be killed and release the memory, Quit command is recommended.
n
nCode:-
nClose Method:- In the below code, we open the “About Us” page in new window and we used Close method at the end, still this browser window is not closed.Thus only the Home page is closed as selenium was having focus on that page.
n

n

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class CloseVSQuit {
public static void main(String[] args) {
WebDriver selenium = new FirefoxDriver();
System.out.println("Launching Browser");
//Opening the URL
selenium.get("http://www.uftHelp.com");
//Implicit wait for the browser to launch
selenium.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//Opening a "ABout-us" page in new window of browser
WebElement element =selenium.findElement(By.id("contact-Us"));
Actions oAction=new Actions(selenium);
oAction.contextClick(element).perform();
oAction.sendKeys("w").perform();
System.out.println("About Us Page is opened but not Closed");
//Closing the browser window using Close method
selenium.close();
}
}

n

n
nQuit Method:- All the opened windows are closed,which was opened by selenium.

n

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class CloseVSQuit {
public static void main(String[] args) {
WebDriver selenium = new FirefoxDriver();
System.out.println("Launching Browser");
//Opening the URL
selenium.get("http://www.uftHelp.com");
//Implicit wait for the browser to launch
selenium.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//Opening a "ABout-us" page in new window of browser
WebElement element =selenium.findElement(By.id("contact-Us"));
Actions oAction=new Actions(selenium);
oAction.contextClick(element).perform();
oAction.sendKeys("w").perform();
System.out.println("All the opened browsers are closed");
//Closing the browser window using Quit method
selenium.quit();
}
}

n

For more Selenium Tutorials

Was this article helpful?
YesNo

Similar Posts