How To Close Browser Window In Selenium Webdriver
Selenium is used for automation testing of web applications. In this case, spider web applications refer to software that makes it necessary to instantiate a browser instance, open a browser and so shut a browser when the testing is complete. During testing cycles, this unabridged procedure tin can exist automated by Selenium WebDriver.
This article volition focus on how to close a browser in Selenium.
What is Selenium?
Selenium is an open up-source tool that automates web browsers to replicate user actions for QA purposes. It provides a single interface that lets testers write test scripts in programming languages like Ruby-red, Java, NodeJS, PHP, Perl, Python, and C#, among others.
To explore the nuts of Selenium and its usage in testing, refer to this page.
Earlier proceeding with this, information technology is important to know how to execute your first test case in Selenium to understand the menstruation and operation.
How to Close a Browser in Selenium
To close a browser using Selenium, just use the close() method. Refer to the code below as an case:
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public course CloseBrowserExample{ public static void main(String[] args) throws Exception { System.setProperty("webdriver.chrome.driver", "Path_to_Chrome_Driver"); WebDriver driver=new ChromeDriver(); driver.close(); //closes the browser } } Now allow'due south sympathise a chip more about driver.close and commuter.quit methods which are used to shut a browser in Selenium.
driver.close() and driver.quit() are two methods for closing a browser session in Selenium WebDriver. Information technology is necessary to know when to employ each method in a exam script.
- commuter.close() closes but the current window on which Selenium is running automated tests. The WebDriver session, however, remains active.
- On the other hand, the driver.quit() method closes all browser windows and ends the WebDriver session.
Notation that if no other window is open and the script calls driver.close() then it will also close the current WebDriver session.
Here arises the question: when should a tester use which method?
Allow's take an example to understand how to call both these methods in Selenium.
driver.close()
This method is used when y'all want to shut the current existing window you lot are working on. Please annotation driver.close() will not terminate the browser instance.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import java.util.List; import java.util.Fix; import java.util.Iterator; public class CloseWindow { public static void main(Cord[] args) { System.setProperty("webdriver.chrome.driver", "Path to Chrome Driver"); WebDriver driver = new ChromeDriver(); driver.get("https://world wide web.google.com"); //implicit wait driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS); commuter.findElement(By.proper noun("q")).sendkeys(Selenium); commuter.findElement(By.name("btnk")).click(); // window handles Set w = driver.getWindowHandles(); // window handles iterate Iterator t = w.iterator(); String h = (String) t.side by side(); String p = (String) t.next(); // switching child window driver.switchTo().window(h); // close merely the child browser window driver.close(); } }' On executing the above code, output goes as follows:
driver.quit()
This method is used when you want to terminate the webdriver case window you are working on. Please note driver.quit() volition cease the browser case.
import org.openqa.selenium.Past; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import java.util.List; import coffee.util.Gear up; import java.util.Iterator; public class CloseWindow { public static void chief(Cord[] args) { Arrangement.setProperty("webdriver.chrome.driver", "Path to Chrome Driver"); WebDriver commuter = new ChromeDriver(); driver.get("https://world wide web.google.com"); //implicit wait driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS); driver.findElement(By.name("q")).sendkeys(Selenium); driver.findElement(By.proper name("btnk")).click(); // window handles Set w = driver.getWindowHandles(); // window handles iterate Iterator t = w.iterator(); String h = t.side by side(); String p = t.next(); // switching child window driver.switchTo().window(h); // switching parent window driver.switchTo().window(p); // terminates driver session and closes all windows driver.quit(); } } Behave in mind that Selenium tests must be run on real devices for accurate results. Start running tests on 2000+ existent browsers and devices on BrowserStack'south real device cloud. Run parallel tests on a Deject Selenium Grid to get faster results without compromising on accuracy. Detect bugs earlier users do by testing software in real user conditions with BrowserStack.
Endeavor BrowserStack for Gratis
Source: https://www.browserstack.com/guide/close-browser-in-selenium#:~:text=close()%20closes%20only%20the,and%20ends%20the%20WebDriver%20session.
Posted by: petersonbeforrome1969.blogspot.com

0 Response to "How To Close Browser Window In Selenium Webdriver"
Post a Comment