We start automation scripting with a single test case; Gradually, we increase the number as the business approves stories. A point comes where our automation pack grows in such a huge number wherein we started looking for the performance improvement. So, in that case, parallel execution helps a lot. Today we are going to discuss the execution of multiple test cases simultaneously to improve the performance. In that case, there comes the concept of parallel execution and we are going to discuss it using session handling.
The script can be run parallel in two ways.
1.Using session handling
2.Using Multi-threading
Let’s first discuss session handling today.
Don’t Miss: How to set up Selenium Grid?
Why Session Handling in selenium?
When we execute any test case, the WebDriver interacts with the browser of the machine. If you want to execute another test case in the same machine either with the same browser or different browser along with the test case which is already running, then in that case session handling gives the best solution.
This is basically the ability to execute multiple parts or sub-components of the code in parallel.
WebDriver interface’s source code has one variable declaration, which is sessionId. When we create an object of the WebDriver then one sessionId will be attached to the corresponding driver like chrome/IE or Firefox.
WebDriver driver=new ChromeDriver();
So, for parallel execution for multiples tests, we need to create different sessions which will perform solely for a particular execution.
How to run scripts in parallel?
Here are the steps to run multiple tests simultaneously.
Step# 1: Create tests by defining different sessions
package tests; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; public class CreateSessionExample { @Test public void createSession1(){ //First session of WebDriver System.setProperty("WebDriver.chrome.driver","C:\\Users\\abc\\Desktop\\Selenium\\chromedriver_win32\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); //Goto google site driver.get("https://www.google.com/"); //find search text box and fill it with Test driver.findElement(By.xpath("//*[@id='lst-ib']")).click(); driver.findElement(By.xpath("//*[@id='lst-ib']")).sendKeys("Test"); driver.close(); } @Test public void createSession2(){ //Second session of WebDriver System.setProperty("WebDriver.chrome.driver","C:\\Users\\abc\\Desktop\\Selenium\\chromedriver_win32\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); //Goto google site driver.get("https://www.google.com/"); //find search text box and fill it with Test driver.findElement(By.xpath("//*[@id='lst-ib']")).click(); driver.findElement(By.xpath("//*[@id='lst-ib']")).sendKeys("Test"); driver.close(); } @Test public void createSession3(){ //Third session of WebDriver System.setProperty("WebDriver.chrome.driver","C:\\Users\\abc\\Desktop\\Selenium\\chromedriver_win32\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); //Goto google site driver.get("https://www.google.com/"); //find search text box and fill it with Test driver.findElement(By.xpath("//*[@id='lst-ib']")).click(); driver.findElement(By.xpath("//*[@id='lst-ib']")).sendKeys("Test"); driver.close(); } }
Step# 2: Configure your testing.xml
Add parallel attribute within the suite tag:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="TestSuite" thread-count="4" parallel="methods" > <test name="test"> <classes> <class name="tests.CreateSessionExample"> </class> </classes> </test> </suite>
The parallel attribute can include methods, classes, tests, and instances.
If you have mentioned parallel as methods, then all the methods under @Test annotation will be executed. Similarly, for classes, all the test classes under java classes will run in parallel. The same tactic applies to Tests and Instances as well.
Suppose you have several instances of test cases, like A and B then all the methods of instance A will be executed in thread TA. All the methods of instance B will be executed in thread TB and TA! =TB.
The attribute thread-count=4 means 4 sessions or browser will be opened for the execution.
Defining Dependencies
If you want to set the execution dependency, then you can use dependsOnMethods attribute in the testing.xml
Suppose if you want to execute createSession2 method before execution createSession1 then you have to use the below testng.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="TestSuite" thread-count="3" parallel="methods" > <test name="testGuru"> <classes> <class name="tests.CreateSessionExample"> <methods> <include name="createSession1" dependsOnMethods=" createSession2"/> <include name="createSession2"/> </methods> </class> </classes> </test> </suite>
Output
Right click on testing.xml then select Run as TestNG
Here testcase2 i.e. Createsession2 is executed before createsession1
This was all about parallel execution of the test cases through session handling. We will discuss another technique for parallel execution through Multi-threading in another post.
But what if i need execute them in right order and input specific text in specific session? Does this approach thread safe?
yes, we can do this.