Categories
Uncategorized

Close v/s Quit in Selenium

Scenario:
We want to understand the difference between Close, Quit & Dispose commands?

Laymen terms: –

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.

Close method in selenium
My Lovely House

Above story went bit long 🙂 , let’s try to relate the things now :-

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.

2. 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.

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

Close: – This method is used to close the current open window. It closes the current open window on which driver has focus on.

Quit: – 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.

Dispose: – This method closes all Browser windows and safely ends the session

Usage:-
Suppose 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.

Code:-
Close 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.

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

}

}



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

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

}

}

For more Selenium Tutorials

Leave a Reply

Your email address will not be published. Required fields are marked *