We are always in hurry right! Then why should we execute our test cases in a sequential manner?
The software development lifecycle has been changed and the ability to test things parallely is a very very important features of TestNG. Let us first understand the business need for testing parallely.
Example:
Amazon is adding 100 new features and they want to test it for IE, Firefox and Chrome. Assume that they have written 100 test cases. So they are executing 100 test cases for each browser. If they adapt sequential testing the time consumption will be heavy. In this case, we should opt for parallel testing.
Let us understand how to do parallel testing with the help of the below example.
package seleniumWithTestNG;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class ParallelTesting {
@Test
public void openGoogle(){
System.setProperty("webdriver.gecko.driver", "C:\\Users\\Elcot\\Desktop\\drivers\\gecko\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
driver.quit();
}
@Test
public void openBing(){
System.setProperty("webdriver.gecko.driver", "C:\\Users\\Elcot\\Desktop\\drivers\\gecko\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.bing.com");
driver.quit();
}
}
and keep the testng.xml file as mentioned below.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Test Suite" verbose="2">
<test name="Parallel Test">
<classes>
<class name="seleniumWithTestNG.ParallelTesting" />
</classes>
</test>
</suite>
Now run the testng.xml file and you can see
- firefox is launched
- Bing opened
- firefox closed
Again,
- firefox is launched
- Google opened
- firefox closed
This is how the sequential execution is done, now let us make it parallel execution. To achieve this we are going to make a small change in the testng.xml file. Notice the change, parallel and thread-count has been added.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Test Suite" verbose="2" parallel="methods" thread-count="2">
<test name="Parallel Test">
<classes>
<class name="seleniumWithTestNG.ParallelTesting" />
</classes>
</test>
</suite>
Now run the testng.xml file and observe the results. Two browser instances will be opened parrallely and the tests will be executed. This is how the parallelism is achieved in testng. Hope you understood the concept, in case you have any doubt, feel free to contact me.