Sie sind auf Seite 1von 3

1. What is the difference between driver. Close and driver. Quit?

A. webDriver.Close() - Close the browser window that the driver has focus of
webDriver.Quit() - Calls dispose
webDriver.Dispose() Closes all browser windows and safely ends the session

driver.close It closes the the browser window on which the focus is set.

driver.quit It basically calls driver.dispose method which in turn closes all the browser windows
and ends the WebDriver session gracefully.

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.

Above story went bit long :) , lets 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
import
import
import
import
import

java.util.concurrent.TimeUnit;
org.openqa.selenium.By;
org.openqa.selenium.WebDriver;
org.openqa.selenium.WebElement;
org.openqa.selenium.firefox.FirefoxDriver;
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
import
import
import
import
import

java.util.concurrent.TimeUnit;
org.openqa.selenium.By;
org.openqa.selenium.WebDriver;
org.openqa.selenium.WebElement;
org.openqa.selenium.firefox.FirefoxDriver;
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();
}
}

Das könnte Ihnen auch gefallen