HomeLinuxEasy methods to Shut a Browser in Selenium

Easy methods to Shut a Browser in Selenium


On this article, we are going to focus on in regards to the strategies to shut the browser. Selenium helps two strategies to shut the browser: (1) shut() methodology and (2) stop() methodology. However each are utilized in totally different conditions. We are going to clarify how and the place we use every of them with an carried out instance. This text implements two approaches to verify whether or not the WebDriver session ID is lively or null.

Easy methods to Shut a Browser in Selenium

Closing the browser is among the operations once we end the web site verification testing. The Selenium framework additionally gives the instructions to shut the whole browser or tabs after ending the Automation Testing with the assistance of WebDriver.

Selenium helps two strategies to shut the browsers: (1) driver.shut() and (2) driver.stop(). How and when every of the strategies is used are defined within the following:

Shut() Technique

The shut() methodology is among the operations that’s supported by Selenium to shut the browser window which is in focus. If there are a number of tabs which can be open within the browser, the shut() methodology, with the assistance of WebDrivers, solely closes the targeted tab on which the Selenium Automation Take a look at is at present operating and the remainder of the tabs are usually not closed. The shut() methodology solely closes the present tab however doesn’t destroy the WebDriver session.

The syntax to shut the browser window is given within the following:

The place the “driver” is the occasion of Webdriver.

Stop() Technique

The Selenium framework helps the stop() operation to shut all of the open tabs of the browser. Stop() methodology lets you shut all of the tabs which can be open within the browser. The browser WebDriver makes use of the stop() methodology to finish the WebDriver session which is at present lively.

The stop() methodology not solely closes all opened browser tabs however terminates the Webdriver classes by calling the motive force.dispose methodology. In case the Webdriver doesn’t destroy the session after closing the browser home windows, there are probabilities of leaking reminiscence information and errors to happen.

The syntax to shut the browser window is given within the following:

The place the “driver” is the occasion of Webdriver.

Method 1: Shut the Present Browser Window Whereas Retaining the WebDriver Session Alive

In Selenium, the shut() methodology permits the Webdriver session alive after closing the present tab. Let’s take a situation the place we open the browser and shut the at present targeted tab then verify whether or not the session just isn’t closed. To take action, we use the https://www.ebay.com/ web site.

Let’s start to implement!

First, create the Java file with the title as Close_browser.java. Additionally, choose the choice of the static void class. Then, the Java file mechanically creates the general public class with the file title and generates a public static void principal class. The code is talked about as follows:

public class Close_browser {

public static void principal(String[] args) {

}

}

Now, let’s import the mandatory packages of WebDriver, SessionId, RemoteWebDriver, and time.Period for implicit class.

Right here is the code for the mandatory import package deal:

import java.time.Period;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.distant.RemoteWebDriver;

import org.openqa.selenium.distant.SessionId;

You may check with the entire code that we supplied within the following. Following that, we break down this code into small chunks so you may perceive what perform each serves.

package deal ui;

import java.time.Period;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.distant.RemoteWebDriver;

import org.openqa.selenium.distant.SessionId;

public class Close_browser {

public static void principal(String[] args) {

System.setProperty(“webdriver.chrome.driver”, “C:browserdriverschromedriver.exe”);

ChromeDriver driver = new ChromeDriver();

driver.handle().window().maximize();

driver.handle().timeouts().implicitlyWait(Period.ofSeconds(10));

driver.get(“https://www.ebay.com/”);

System.out.println(“Open the Ebay web site in Chrome browser”);

driver.shut();

System.out.println(“Shut the Browser”);

SessionId id = ((RemoteWebDriver)driver).getSessionId();

System.out.println(“Get Session Id after shut the tactic:” + id);

}

}

Let’s now break down the earlier code line by line and clarify how every line works.

For this situation implementation, we use the Chrome browser. Within the preliminary part of the code, entry the native listing of the Chrome drivers earlier than beginning the Chrome browser drivers.

The next syntax is used to implement it:

System.setProperty(“webdriver.chrome.driver”,“C:browserdriverschromedriver.exe”);

ChromeDriver driver = new ChromeDriver();

After that, we maximize the browser window utilizing the next given syntax:

driver.handle().window().maximize();

Take into account that loading all parts of the net web page, this perform waits till the implicit wait is given for the motive force as we now have given in our following code:

driver.handle().timeouts().implicitlyWait(Period.ofSeconds(10));

driver.get(“https://www.ebay.com/”);

The implictlyWait() is the tactic to carry out implicit wait. The length perform mentions the time lag, the “of Second” is the time unit, whereas this system waits 10 seconds for the weather to load and open the requested URL web site.

After the requested web site opens within the browser, print the “Open the Ebay web site in Chrome browser” into the console.

Right here is the syntax to print the textual content into the console:

System.out.println(“Open the Ebay web site in Chrome browser”);

On this chunk of code, we now shut the at present opened browser window utilizing the shut() methodology.

Right here is the syntax code to shut the present browser window and print the “Shut the Browser” textual content into the console:

driver.shut();

System.out.println(“Shut the Browser”);

Now, the ultimate chunk of code refers to checking if the session is destroyed or not:

SessionId id = ((RemoteWebDriver)driver).getSessionId();

Within the previously-given code, the RemoteWebDriver class is used to retrieve the session ID of the WebDriver. A WebDriver server is used to speak with a distant internet browser utilizing the RemoteWebDriver class, a subclass of the WebDriver class. The getSessionId() methodology is used to fetch the session ID if lively.

System.out.println(“Get Session Id after shut the tactic:” + id);

The system.out.println() is the Java perform which is used to print the string and variable values. Right here, we print the session ID of the WebDriver.

Within the following screenshot, we are able to see the output results of the executed code:

As you possibly can observe, after operating the earlier code, the shut() methodology closes the present working browser tab. However nonetheless, the WebDriver session ID is activated which is printed into the earlier console.

Method 2: Shut Each Browser Window and Finish the WebDriver Session

In Selenium, the stop() methodology permits the Webdriver session to finish and shut all of the opened tabs. Let’s take the situation, the place we are going to open the Chrome browser and shut all of the opened tabs within the chrome browser then verify whether or not both session id turns into null.

Let’s start to implement!

You may check with the entire code that we supplied within the following illustration. Following that, we break down this code into small chunks so you may perceive what perform each serves.

package deal ui;

import java.time.Period;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.distant.RemoteWebDriver;

import org.openqa.selenium.distant.SessionId;

public class Close_browser {

public static void principal(String[] args) {

System.setProperty(“webdriver.chrome.driver”, “C:browserdriverschromedriver.exe”);

ChromeDriver driver = new ChromeDriver();

driver.handle().window().maximize();

driver.handle().timeouts().implicitlyWait(Period.ofSeconds(10));

driver.get(“https://www.ebay.com/”);

System.out.println(“Open the Ebay web site in Chrome browser”);

driver.stop();

System.out.println(“Shut the Browser”);

SessionId id = ((RemoteWebDriver)driver).getSessionId();

System.out.println(“Get Session Id after shut the tactic:” + id);

}

}

Let’s now break down the supplied code line by line and clarify how every line works.

For this situation implementation, we repeat all the identical steps of strategy #1. To launch the browser, maximize the browser and implement the implicit wait.

On this chunk of code, we now shut all of the opened browsers window utilizing the stop() methodology.

Right here is the syntax code to shut all of the browsers window and print the “Shut the Browser” textual content into the console:

driver.stop();

System.out.println(“Shut the Browser”);

Now, the ultimate chunk of code refers to checking if the session is destroyed or not:

SessionId id = ((RemoteWebDriver)driver).getSessionId();

Within the previously-given code, the RemoteWebDriver class is used to retrieve the session ID of the WebDriver. A WebDriver server is used to speak with a distant internet browser utilizing the RemoteWebDriver class, a subclass of the WebDriver class. The getSessionId() methodology checks if the session is null.

System.out.println(“Get Session Id after shut the tactic:” + id);

The system.out.println() is the Java perform which is used to print the string and variable values. Right here, we print the session ID of the WebDriver.

Within the following screenshot, we are able to see the output results of executed code:

As you possibly can observe, after operating the earlier code, the stop() methodology closes all of the opened browser tabs and destroys the WebDriver session that’s printed into the earlier console which is null.

Conclusion

You realized the 2 strategies of closing the browser: shut() and stop() strategies. This text defined each strategies intimately on the place to make use of the shut() methodology and the place to make use of the stop() methodology. Then, we carried out each approaches with the instance to verify whether or not the WebDriver session ID is alive or not. This tutorial is greatest for each newbie and skilled builders.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments