Wednesday, 5 August 2015

Running Selenium tests in parallel

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
    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
        public void startSeleniumServer()throws Exception{
            rc = new RemoteControlConfiguration(); 
            seleniumServer = new SeleniumServer(rc);
            if(!seleniumServer.getServer().isStarted()){ 
                seleniumServer.start();       
            }
        }

        @Parameters({"port","browser"})
        @BeforeClass
        public void startSelenium(int port,String browser) {
            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
        public void stopSelenium() throws Exception {
            selenium.stop();
         }
        @AfterSuite
        public void stopSeleniumServer()throws Exception {
            seleniumServer.stop();
        }     
    }

          Below is the class that runs in firefox browser.This class extends from 'MasterTest' so that before 'FirefoxTest' the selenium server will start, if not started and after this test run the selenium server will stop.

//FirefoxTest.java
    package master;

    import org.testng.annotations.Test;

    public class FirefoxTest extends MasterTest {     

        @Test

        public void firefoxTest() throws Exception {
            selenium.setSpeed("1000");
            selenium.open("/");
            selenium.type("id=lst-ib", "selenium");
            selenium.click("name=btnG");
            selenium.click("link=Selenium - Web Browser utomation");
            selenium.waitForPageToLoad("30000");
        }
    }

          Below is the class that runs in google chrome browser.This class also extends from 'MasterTest' so that before 'ChromeTest' the selenium server will start, if not started and after this test run the selenium server will stop.

// ChromeTest .java

    package master;

    import org.testng.annotations.Test;

    public class ChromeTest extends MasterTest {      
        @Test
        public void chromeTest() throws Exception {
            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");
        }

    }

          Below is the class that runs in Internet Explorer browser.This class also extends from 'MasterTest' so that before 'IETest' the selenium server will start, if not started and after this test run the selenium server will stop.

// IETest .java
    package master;
    import org.testng.annotations.Test;
 

    public class IETest extends MasterTest {
        @Test
        public void ieTest() throws Exception {
           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");

        } 

    }

          Finally, here is the TestNG's configuration file that runs all the tests giving 'port' and 'browser' as input parameters to the 'MasterTest' in each and every test run.

          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>

          So, when you will run the above TestNG's configuration file, firstly, it will run 'FirefoxTest' in firefox browser,secondly, it will run 'ChromeTest' in chrome browser and thirdly, it will run 'IETest' in internet explorer browser in the order, one after one.If you want to run all these 3 tests concurrently in 3 browsers respectively, make parallel="tests" in the TestNG's configuration file.

No comments:

Post a Comment