How to do session handling in Selenium Webdriver using Java?
In Selenium WebDriver, managing browser sessions is crucial for ensuring that each test runs independently and without interference. A browser session in Selenium is identified by a unique session ID, which helps track and manage the session throughout the test.
Proper session handling in Selenium WebDriver using Java is essential for creating robust and reliable automation scripts, especially when dealing with complex test scenarios.
Prerequisites
- Java Development Kit (JDK) installed on your system.
- Eclipse IDE or any Java-compatible IDE.
- Basic knowledge of Java and Selenium WebDriver.
- Selenium WebDriver and Browser Driver installed.
What is Session Handling in Selenium Webdriver?
Session handling is a critical aspect of web application testing using Selenium WebDriver. A session represents a single interaction between a user and an application, keeping the status consistent across various HTTP requests. Effective management of sessions can guarantee that users remain logged in during their interactions and that test scripts operate smoothly without repeated logins.
Example
Let's start by creating a simple Selenium WebDriver project demonstrating session handling. We will use ChromeDriver as our WebDriver and TestNG as our testing framework.
Step 1: Set Up the Project
First, make sure you have Maven set up in your development environment. Create a new Maven project and add the following dependencies in your pom.xml
file:
<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>com.example</groupId>
<artifactId>selenium-session-handling</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- Selenium WebDriver dependency -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.11.0</version>
</dependency>
<!-- WebDriverManager to manage driver binaries -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.4.0</version>
</dependency>
<!-- TestNG for testing framework -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.7.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
</plugin>
</plugins>
</build>
</project>
Step 2: Write the Session Handling Code
Create a new Java class, SessionHandlingTest.java
, under src/test/java/com/example/tests
. Here's a complete code example:
package com.example.tests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.SessionId;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.testng.annotations.Test;
public class SessionHandlingTest {
@Test
public void testSessionHandling() {
// Set up ChromeDriver using WebDriverManager
WebDriverManager.chromedriver().setup();
// Initialize ChromeDriver instance
WebDriver driver = new ChromeDriver();
// Launch URL
driver.get("https://www.geeksforgeeks.org/");
// Get session ID
SessionId sessionId = ((RemoteWebDriver) driver).getSessionId();
System.out.println("Session ID for GeeksforGeeks: " + sessionId);
// Close the browser
driver.quit();
}
@Test
public void testSessionHandlingGoogle() {
// Set up ChromeDriver using WebDriverManager
WebDriverManager.chromedriver().setup();
// Initialize ChromeDriver instance
WebDriver driver = new ChromeDriver();
// Launch URL
driver.get("https://www.google.com/");
// Get session ID
SessionId sessionId = ((RemoteWebDriver) driver).getSessionId();
System.out.println("Session ID for Google: " + sessionId);
// Close the browser
driver.quit();
}
@Test
public void testSessionHandlingTutorialsPoint() {
// Set up ChromeDriver using WebDriverManager
WebDriverManager.chromedriver().setup();
// Initialize ChromeDriver instance
WebDriver driver = new ChromeDriver();
// Launch URL
driver.get("https://www.gmail.com/");
// Get session ID
SessionId sessionId = ((RemoteWebDriver) driver).getSessionId();
System.out.println("Session ID for Gmail: " + sessionId);
// Close the browser
driver.quit();
}
}
Output:
When you run the above tests using TestNG, you should see output similar to the following in the console:

This output indicates that each test method runs independently with its own unique browser session. The session ID is a unique identifier for each WebDriver session, which is crucial when you want to manage or validate sessions during testing.
Conclusion
Effective session handling in Selenium WebDriver using Java allows testers to maintain control over their browser instances, ensuring that each test is isolated and runs smoothly. By leveraging session IDs and managing sessions properly, you can avoid conflicts and ensure that your Selenium WebDriver tests are consistent and reliable, leading to more efficient and error-free automation.