Chapter 10_4: parallel execution in selenium

Why do we need Session Handling?

During test execution, the Selenium WebDriver has to interact with the browser all the time to execute given commands. At the time of execution, it is also possible that, before current execution completes, someone else starts execution of another script, in the same machine and in the same type of browser.

To avoid this confusion, we need a mechanism by which two different executions should not overlap with each other.This can be performed using Session Handling in Selenium.

How to achieve Session Handling in Selenium WebDriver?

If we check the source code of Selenium WebDriver, we will find a variable named as ‘sessionId’. Whenever we create a new instance of a WebDriver object, a new ‘sessionId’ will be generated and attached with that particular browser. Firefox/Chrome/IE Driver ()

Here new session generated for chrome browser.

So anything we do after this will execute only in that particular Chrome browser session.

this is an in-built functionality, there is no definitive need to assign the session id.

Webdriver code: here 2 different sessionId generated for 2 different webdriver.

package TestNG;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class SessionId 
{
public static void main(String[] args)
	{
// first session of webdriver
 WebDriver driver = new ChromeDriver();
driver.get("http://www.google.co.in");
// second session of webdriver
WebDriver driver2 = new ChromeDriver();
driver2.get("http://www.google.co.in");
			}
          }

How to run Parallel Tests with Selenium

There are situations where you want to run multiple tests at the same time.

In such cases, one can use “parallel” attribute

The attribute thread-count allows you to specify how many threads should be allocated for particular execution.

Complete Example: In this Example, three test cases will run parallel and fill login data

package TestNG;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class multipleSessions 
{
@Test    
   public void executSessionOne(){
  //First session of WebDriver
  System.setProperty("webdriver.driver.chromedriver","chromedriver.exe");
      WebDriver driver = new ChromeDriver();
     driver.get("http://newtours.demoaut.com/");
  //find user name text box and fill it
  driver.findElement(By.name("userName")).sendKeys("Driver 1");
      }
 @Test    
 public void executeSessionTwo(){
    //Second session of WebDriver
  System.setProperty("webdriver.driver.chromedriver","chromedriver.exe");
        WebDriver driver = new ChromeDriver();
   driver.get("http://newtours.demoaut.com/");
        //find user name text box and fill it
        driver.findElement(By.name("userName")).sendKeys("Driver 2");
       }
 @Test    
  public void executSessionThree(){
 //Third session of WebDriver
 System.setProperty("webdriver.driver.chromedriver","chromedriver.exe");
     WebDriver driver = new ChromeDriver();
      driver.get("http://newtours.demoaut.com/");
   //find user name text box and fill it
  driver.findElement(By.name("userName")).sendKeys("Driver 3");
     }        
}

TestNG.XML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite thread-count="3" name="Suite" parallel="methods">
  <test thread-count="3" name="Test" parallel="methods">
    <classes>
      <class name="TestNG.multipleSessions"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->