- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to do session handling in Selenium Webdriver?
We can perform session handling with the help of Selenium webdriver with a 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 parallel attribute is set to methods.
In our example, we would have three methods with three different session ids which are executing in parallel.
Example
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 TestNG10 { @Test public void myTest () { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); //launch URL driver.get("https://www.tutorialspoint.com/index.htm"); //get session ID SessionId s = ((RemoteWebDriver) driver).getSessionId(); System.out.println("Session Id is for method1: " + s); }@Test public void myTest1 () { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); //launch URL driver.get("https://www.google.com/"); //get session ID SessionId s = ((RemoteWebDriver) driver).getSessionId(); System.out.println("Session Id is for method1: " + s); }@Test public void myTest2 () { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); //launch URL driver.get("https://www.tutorialspoint.com/about/about_careers.htm/"); //get session ID SessionId s = ((RemoteWebDriver) driver).getSessionId(); System.out.println("Session Id is for method1: " + 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="Tutorialspoint" > <classes> <class name="TestNG10" /> </classes> </test> </suite>
Output
- Related Articles
- How can I get Webdriver Session ID in Selenium?
- Handling DropDown And Multiple Select in Webdriver using Selenium
- How do I open Chrome in selenium WebDriver?
- What can selenium WebDriver do?
- How do I resolve the ElementNotInteractableException in Selenium WebDriver?
- How do I set the Selenium webdriver get timeout?
- How do I keep a session alive for long Selenium scripts in automation?
- How do I get current URL in Selenium Webdriver 2 Python?
- How do I set browser width and height in Selenium WebDriver?
- How to Download & Install Selenium WebDriver?
- How to close a browser session in Selenium with python?
- How do you automatically download a Pdf with Selenium Webdriver in Python?
- How to record a video in Selenium webdriver?
- How to get Tooltip Text in Selenium Webdriver?
- How do I find an element that contains specific text in Selenium Webdriver?

Advertisements