Difference between driver.close() and driver.quit()
Difference between driver.close() and driver.quit()
This article will show the difference between .close() and quit() functions. The driver close closes the current browser window running the command. The driver quit command is used to close all browser windows opened by the program, including the current one.
driver.close()
- driver.close(): this method closes the browser window that the driver has focus on.
driver.quit()
- driver.quit(): this method calls dispose method. driver.dispose() : closes all browser windows opened by the driver instance and safely ends the session gracefully.
Let’s see sample programs run in the BrowserStack cloud, demonstrating the difference between the 2 methods.
Code Example
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class CloseAndQuit {
public static final String USERNAME="your_BROWSERSTACK_USERNAME";
public static final String AUTOMATE_KEY = "YOUR_KEY";
public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY
+ "@hub-cloud.browserstack.com/wd/hub";
public static void main(String[] args) throws MalformedURLException {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("browser", "Firefox");
caps.setCapability("browser_version", "45");
caps.setCapability("os", "Windows");
caps.setCapability("os_version", "10");
caps.setCapability("browserstack.debug", "true");
WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
driver.get("http://www.testingdocs.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement element =driver. findElement(By.partialLinkText("CONTACT"));
Actions action=new Actions(driver);
action.contextClick(element).perform();
action.sendKeys("w").perform();
// browser window close
driver.close();
}
}
Quit will close all browser instances :
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class BrowserStackCloseAndQuit {
public static final String USERNAME = "your_username";
public static final String AUTOMATE_KEY = "yourKey";
public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY +
"@hub-cloud.browserstack.com/wd/hub";
public static void main(String[] args) throws
MalformedURLException, InterruptedException {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("browser", "Firefox");
caps.setCapability("browser_version", "45"); caps.setCapability("os", "Windows");
caps.setCapability("os_version", "10");
caps.setCapability("browserstack.debug", "true");
WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
driver.get("http://www.testingdocs.com");
driver.manage().window().maximize();
driver.manage().timeouts(). implicitlyWait(30, TimeUnit.SECONDS);
WebElement element =driver. findElement(By.partialLinkText("CONTACT"));
Actions action=new Actions(driver);
action.contextClick(element).perform();
action.sendKeys("w").perform();
// browser quit driver.quit();
}
}
Selenium Tutorial on this website:
https://www.testingdocs.com/selenium-webdriver-tutorial/
More information on Selenium, visit official website: