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>