Table of contents
1.
Introduction
2.
Example
3.
Frequently Asked Questions
4.
Conclusions
Last Updated: Mar 27, 2024
Easy

Session Handling

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Selenium, the TestNG framework is used to perform Session Handling with the help of the Selenium web driver. We use the parallel attribute in the TestNG XML file to trigger parallel sessions. All the configurations are added in the TestNG XML file. 

Another attribute, thread-count, is added to the TestNG XML file to specify the number of threads to be created while executing the tests in parallel mode. 

Example

In the following example, we will execute three different sessions with three different session IDs in parallel using Selenium.

parallelCode.java

import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.SessionId;
public class parallelCode {
   @Test
   public void TestCase1 () {
      System.setProperty("webdriver.chrome.driver", "C:\\Users\\91963\\Desktop\\Java\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.get("URL To Test");
      //Fetch session ID
      SessionId s = ((RemoteWebDriver) driver).getSessionId();
      System.out.println("Session Id for the first method: " + s);
   }
   @Test
   public void TestCase2 () {
      System.setProperty("webdriver.chrome.driver", "C:\\Users\\91963\\Desktop\\Java\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.get("Second URL To Test");
      //Fetch session ID
      SessionId s = ((RemoteWebDriver) driver).getSessionId();
      System.out.println("Session Id for the second method: " + s);
   }@Test
   public void TestCase3 () {
      System.setProperty("webdriver.chrome.driver", "C:\\Users\\91963\\Desktop\\Java\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.get("Third URL To Test");
      //Fetch session ID
      SessionId s = ((RemoteWebDriver) driver).getSessionId();
      System.out.println("Session Id for the third method: " + s);
   }
}

 

TestNG XML implementation

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<!—parallel methods set for execution with 3 threads-->
<suite name="Test-Suite" parallel="methods" thread-count="3">
   <test name="Code Test" >
      <classes>
         <class name="parallelCode" />
      </classes>
   </test>
</suite>

Xpath in Selenium

Frequently Asked Questions

  1. What is Automation Testing?
    Automation Testing can be understood as the use of specialized or specifically designed tools to automate the execution of designed test cases without any human involvement.
  2. What tasks can be performed using Automation Testing tools?
    Automation testing tools can perform all the tasks, such as accessing the test data, controlling the execution of tests, and comparing the observed results against the expected result with more ease and efficiency. We can also use the Automation Testing tools to generate test reports.
  3. What is Selenium?
    Selenium is one of the most famous open-source tools used for Functional Automation Testing. It can be used to write test cases in several popular programming languages like JavaScript, Python, Perl, Java, etc.
  4. What is Session Handling?
    Session Handling is a process that is used to manipulate the server responses such that the application state is preserved during the load testing.
  5. How to perform Session Handling in Selenium?
    In Selenium, the TestNG framework is used to perform Session Handling with the help of the Selenium web driver. We use the parallel attribute in the TestNG XML file to trigger parallel sessions.

Conclusions

This Blog covered all the necessary points regarding the Session Handling using Selenium. We further discussed an example to understand Session Handling.

Don’t stop here; check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems.

Live masterclass