Selenium Webdriver - Driver Sessions
The webdriver sessions mostly refer to the beginning and closing of the browser. A session is created by default by initializing the driver class object. There are two types of driver class - Local Driver and Remote Driver.
For quitting the driver, the quit() method is used. It is always recommended to use the quit() instead of close(), because close() method can only close the browser in focus and the session id will be returned as invalid or expired, while the quit() method terminates the driver session, and the session id will be returned as null.
Session Handling with Selenium Webdriver
We can perform session handling with the help of Selenium webdriver using the TestNG framework. To trigger different sessions, we shall use the attribute parallel in the TestNG XML file.
A TestNG execution configuration is done in the TestNG XML. To create multiple sessions, we shall add the attributes parallel and thread-count in the XML file.
The attribute thread-count controls the number of sessions to be created while executing the tests in a parallel mode. The value of the parallel attribute is set to methods.
Get Session IDS In Selenium Webdriver
To get multiple session ids, we would create three methods which will in parallel. We would get the different session ids in this test using the RemoteWebdriver class.
A RemoteWebdriver is a class which implements the webdriver interface. The RemoteWebdriver is used widely to trigger execution in the remote machines. A session id is a distinct number assigned to each session. It has a format in UUID.
Example - Get Session IDS
Code Implementation in SessionHandling.java
package Report;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.SessionId;
import org.testng.annotations.*;
import org.openqa.selenium.remote.RemoteWebDriver;
public class SessionHandling {
WebDriver driver;
@Test
public void method1() {
// Initiate the Webdriver
WebDriver driver = new ChromeDriver();
//launch URL
driver.get("https://www.tutorialspoint.com/selenium/practice/browser-windows.php");
//get session ID
SessionId s = ((RemoteWebDriver)driver).getSessionId();
System.out.println("Session Id is for method1: " + s);
}
@Test
public void method2() {
// Initiate the Webdriver
WebDriver driver = new ChromeDriver();
//launch URL
driver.get("https://www.tutorialspoint.com/selenium/practice/login.php");
//get session ID
SessionId s = ((RemoteWebDriver)driver).getSessionId();
System.out.println("Session Id is for method2: " + s);
}
@Test
public void method3() {
// Initiate the Webdriver
WebDriver driver = new ChromeDriver();
//launch URL
driver.get("https://www.tutorialspoint.com/selenium/practice/accordion.php");
//get session ID
SessionId s = ((RemoteWebDriver)driver).getSessionId();
System.out.println("Session Id is for method3: " + s);
}
}
Configurations in testng.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite" parallel="methods" thread-count="3">
<test verbose="2" preserve-order="true" name="TestNGTest.java">
<classes>
<class name="Report.SessionHandling"></class>
</classes>
</test>
</suite>
Dependencies in pom.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>SeleniumJava</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.11.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.9.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Project structure of the above implementation is shown in the below image −
Output
Session Id is for method1: 63eae5bba42f97e30e7d01ef211f51e9 Session Id is for method2: 0b23f13aee68ca7c92de1cd93ee78c93 Session Id is for method3: e06fdaa8de91f4706e81566c854d1cf1 =============================================== Default Suite Total tests run: 3, Passes: 3, Failures: 0, Skips: 0 =============================================== Process finished with exit code 0
The result in the console shows Total tests run: 3, as there were three methods with @Test annotations - method1(), method2(), and method3(). Also, we had obtained the session ids of the three methods running in the parallel with the messages in console - Session Id is for method1: 63eae5bba42f97e30e7d01ef211f51e9, Session Id is for method2: 0b23f13aee68ca7c92de1cd93ee78c93, and Session Id is for method3: e06fdaa8de91f4706e81566c854d1cf1.
Finally, the message Passes: 3, and Process finished with exit code 0 was received, signifying successful execution of the code.
The below link provides a detailed description of TestNG −
https://www.tutorialspoint.com/testng/index.htm.
Get Single Session ID in Selenium WebDriver
To get a single session id, we would open a session using the RemoteWebdriver class. A session id is unique to every session. A RemoteWebdriver is a class which implements the webdriver interface. The RemoteWebdriver is used widely to trigger execution in the remote machines.
Example - Get Single ID
Code Implementation in HandlingSessionID.java
package org.example;
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;
public class HandlingSessionID {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new ChromeDriver();
// adding implicit wait of 15 secs
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
// Opening the webpage where we will get the session id
driver.get("https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php");
//get webdriver session id
SessionId s = ((RemoteWebDriver) driver).getSessionId();
System.out.println("Session Id is: " + s);
// Closing browser
driver.quit();
}
}
Dependencies in pom.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>SeleniumJava</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.11.0</version>
</dependency>
</dependencies>
</project>
Output
Session Id is: 78eae5bba42f97e30e7d01ef234f51e9 Process finished with exit code 0
In the above example, we had obtained the session id of webdriver with the message in the console - Session Id is: 78eae5bba42f97e30e7d01ef234f51e9.
Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.
This concludes our comprehensive take on the tutorial on Selenium Webdriver - Driver Sessions. Weve started with describing a webdriver session, differences between close() and quit() methods, and walked through an example illustrating how to get the session ids with Selenium.
This equips you with in-depth knowledge of the Selenium webdriver - driver sessions. It is wise to keep practicing what youve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.