This post explains how to set up a concurrent execution environment and considerably reduce your testing times.
We can run multiple testcases across different browsers at a time using TestNG's configuration file.
Below is the class that defines four methods, 'startSeleniumServer()' method is for start the server (if not started in the current port) before suite i.e before test classes in suite started runnig and the method 'stopSeleniumServer()' is for stop the server after the suite i.e after runnig the test classes in suite (you can make these to execute class level as well, if necessary).The method 'startSelenium()' accepts two parameters i.e 'port' and 'browser' from the TestNG's configuration file.This method initiates the selenium, starting the browser before each test run and the method 'stopSelenium()' ends the test session, killing the browser.
//MasterTest.java
//FirefoxTest.java
// ChromeTest .java
// IETest .java
The attribute 'thread-count' allows to specify how many threads should be allocated for the execution. Here i have used thread-count="3" i.e 3 threads are allocated for the tests execution.Another attribute here i have used is 'parallel'.It takes 3 values i.e
parallel="methods":
TestNG will run all the test methods in separate threads.
parallel="tests":
TestNG will run all the methods in the same <test> tag in the same thread, but each <test> tag will be in a separate thread.
parallel="classes":
TestNG will run all the methods in the same class in the same thread, but each class will be run in a separate thread.
//testng.xml
We can run multiple testcases across different browsers at a time using TestNG's configuration file.
Below is the class that defines four methods, 'startSeleniumServer()' method is for start the server (if not started in the current port) before suite i.e before test classes in suite started runnig and the method 'stopSeleniumServer()' is for stop the server after the suite i.e after runnig the test classes in suite (you can make these to execute class level as well, if necessary).The method 'startSelenium()' accepts two parameters i.e 'port' and 'browser' from the TestNG's configuration file.This method initiates the selenium, starting the browser before each test run and the method 'stopSelenium()' ends the test session, killing the browser.
//MasterTest.java
- package master;
- import org.openqa.selenium.server.RemoteControlConfiguration;
- import org.openqa.selenium.server.SeleniumServer;
- import org.testng.annotations.AfterClass;
- import org.testng.annotations.AfterSuite;
- import org.testng.annotations.BeforeClass;
- import org.testng.annotations.BeforeSuite;
- import org.testng.annotations.Parameters;
- import com.thoughtworks.selenium.DefaultSelenium;
- import com.thoughtworks.selenium.Selenium;
- public class MasterTest {
- protected RemoteControlConfiguration rc ;
- protected SeleniumServer seleniumServer ;
- public Selenium selenium;
- @BeforeSuite
- rc = new RemoteControlConfiguration();
- seleniumServer = new SeleniumServer(rc);
- if(!seleniumServer.getServer().isStarted()){
- seleniumServer.start();
- }
- }
- @Parameters({"port","browser"})
- @BeforeClass
- System.err.println("INFO: The test '"+this.getClass().getName()+"' is running on browser '"+browser+"'");
- selenium = new DefaultSelenium("localhost", port, browser, "http://www.google.co.in");
- selenium.start();
- selenium.windowMaximize();
- selenium.windowFocus();
- }
- @AfterClass
- selenium.stop();
- }
- @AfterSuite
- seleniumServer.stop();
- }
- }
//FirefoxTest.java
- package master;
- import org.testng.annotations.Test;
- public class FirefoxTest extends MasterTest {
- @Test
- selenium.setSpeed("1000");
- selenium.open("/");
- selenium.type("id=lst-ib", "selenium");
- selenium.click("name=btnG");
- selenium.click("link=Selenium - Web Browser Automation");
- selenium.waitForPageToLoad("30000");
- }
- }
// ChromeTest .java
- package master;
- import org.testng.annotations.Test;
- public class ChromeTest extends MasterTest {
- @Test
- selenium.setSpeed("1000");
- selenium.open("/");
- selenium.waitForPageToLoad("30000");
- selenium.type("id=lst-ib", "selenium rc");
- selenium.click("name=btnG");
- selenium.click("css=em");
- selenium.waitForPageToLoad("30000");
- }
- }
// IETest .java
- package master;
- import org.testng.annotations.Test;
- public class IETest extends MasterTest {
- @Test
- selenium.setSpeed("1000");
- selenium.open("/");
- selenium.type("id=lst-ib", "selenium");
- selenium.click("name=btnG");
- selenium.click("link=Selenium - Web Browser Automation");
- selenium.waitForPageToLoad("30000");
- }
- }
The attribute 'thread-count' allows to specify how many threads should be allocated for the execution. Here i have used thread-count="3" i.e 3 threads are allocated for the tests execution.Another attribute here i have used is 'parallel'.It takes 3 values i.e
parallel="methods":
TestNG will run all the test methods in separate threads.
parallel="tests":
TestNG will run all the methods in the same <test> tag in the same thread, but each <test> tag will be in a separate thread.
parallel="classes":
TestNG will run all the methods in the same class in the same thread, but each class will be run in a separate thread.
//testng.xml
- <suite name="Parallel Run Suite" parallel="classes" thread-count="3">
- <test name="Test-1">
- <parameter name="port" value="4444" />
- <parameter name="browser" value="*firefox" />
- <classes>
- <class name="master.FirefoxTest" />
- </classes>
- </test>
- <test name="Test-2">
- <parameter name="port" value="4444" />
- <parameter name="browser" value="*googlechrome" />
- <classes>
- <class name="master.ChromeTest" />
- </classes>
- </test>
- <test name="Test-3">
- <parameter name="port" value="4444" />
- <parameter name="browser" value="*iexplore" />
- <classes>
- <class name="master.IETest" />
- </classes>
- </test>
- </suite>