Technical Project Lead @ Huawei Technologies

My photo
Bangalore/Hyderabad, Karnataka/Andhra Pradesh, India
Have an overall 13 + yrs of experience Currently working as an Senior Architect.Expertise in design,implementation of Automation frameworks for Opensource automation tools like Selenium,Jmeter, Webdriver,Appium, Robotium Expertise in integrating Test & ALM Management tools like Rally,Qmetry,JIRA-Zephyr with Automation frameworks Expertise in design and developmenet Mobile Automation frameworks for tools like Appium, Robotium Remote Control,Cucumber BDD,Manual Testing

Followers

Tuesday, October 26, 2010

Parallel distribution of RCTests using Selenium Grid and TestNG

Before getting into Selenium Grid, I would like to show some light on how to achieve distribution and Parallelism using Selenium Grid. Also its time to switch from Test NG over Junit because of TestNG flexibility over unit testing

What is Selenium Grid?
Selenium Grid is an extension of Selenium RC to distribute your tests across multiple platforms
saving you time by running tests in parallel.

TestNG in context to Selenium Grid.
TestNG comes into picture when you want to achieve parallel execution of Selenium RC tests . However there are different frameworks to achieve this parallelism. I found TestNG as a Simple and Powerful Testing Framework that can be integrated alongside of Selenium RC scripts or for Unit Testing purpose. Frameworks like Junit are bound to unit testing. There are times where might require to do an integration testing @ code level.Test Frameworks like Junit has this limitation.TestNG goes a bit beyond the Unit testing and even supports Integration testing too.



Configuring Test NG Plugin in Eclipse
Below are the simple steps on how to configure TestNG as a plug in to Eclipse

  1. Go Eclipse menu Help >> Install New Software, click on Add.. button
  2. Type some name ex TestNG.
  3. Enter this url http://beust.com/eclipse
  4. Click on Ok button.
  5. Now select TestNG from "Work With" drop down.
  6. Select the TestNG plugin
  7. Click on Next again Next .
  8. Accept the License Agreement
  9. Click on finish Button.
  10. Allow eclipse to download the plugin in background
  11. On Prompt, Restart the eclipse.


Step by Step Configuring Selenium Grid Setup
  1. Download the latest selenium grid from the Official Selenium site >> http://selenium-grid.seleniumhq.org/download.html.  Latest one available at the time of writing this blog is 1.0.8
  2. You  Need to have some prerequisites in order to control and configure Grid. So before we go for configuring the grid we will see how to configure Ant. But a question might arise on why we require ant in Grid Context. He is the answer . Selenium Grid uses ant targets to start or launch the Grid. Also the package comes with ant targets to launch Remote Control on desired port
  3. Now download ant from http://ant.apache.org/bindownload.cgi
  4. Unpack the zip and add the ant bin path (ex: D:\Software\Automation\Tools\ant\Ant\apache-ant-1.8.1\bin) to the System Variable Path

4. Also ensure that your JAVA_HOME is added to user variable

 5.Now open the command prompt and type the command ant -version. You should be able the following if you ant is configured properly. Else follow the previous steps and ty again
you should be able to see the below output when u run the command $ ant -version
>> Apache Ant version 1.8.1 compiled on April 30 2010

6.Now the Ant configuration is complete
7.We just need to run few Ant commands to launch the Grid Hub and register multiple remote control to the Hub.
8.Let us consider a scenario where we have some tests that needs to be run against both firefox and as well as on chrome and how do we get these tests run paralley on both firefox and chrome at a time
9.Firstly we need to start the grid . Go to the directory where your grid is located. Open the command prompt and change the directory to the selenium grid(ex:E:\Softwares\Selenium\selenium-grid-1.0.7) where the build.xml exists
10.Now run the following command $ ant launch-hub
11.This will launch the Selenium Grid on 4444 port.
12.Now you need to register 2 remote controls to Grid. One RC for running Firefox tests and the other for Chrome tests
13.Open the command prompt from the Grid directory and run the following command
ant -Dport=5556 -Denvironment=*firefox launch-remote-control . This will start Remote Control instance on 5556 and will be listening to grid
14. Open another command prompt from the Grid directory and run the following command
ant -Dport=5557 -Denvironment=*googlechrome launch-remote-control . This will start Remote Control instance on 5557 and will be listening to grid.
15.Lets try to check whether the remote control really is registered to Grid.
16.Open the browser and run the following URL http://localhost:4444/console. On opening this url you should be able to see the following RCs registered to grid


17.Now our setup is ready what's next ?.................Next ?? We just need to run our scripts against these RC. So simple as this statement right ;)
18.Hmm Now How to run Test Scripts against multiple RCs parallely
19.Below steps shows how to create sample test script which will automate Google.com on Firefox as well as Chrome browsers concurrently.
20.From the eclipse create a New Java Project.
21.How are we going to write our tests ?? We will use Test NG to write our scripts.As in the initial statement, we discussed TestNG in context to Selenium Grid. This is where exactly TestNG is used
22. Here we are going to integrate TestNG alongside of Selenium RC scripts in order to achieve the distribution of tests among multiple browsers
23. We will now create a sample Selenium RC script with TestNG and we will see how the script runs on multiple browsers
24. Create an eclipse Java Project
25.Create a class of type TestNG class
26. Select the source folder , set package name, Test Class and select the @BeforeClass and @AfterClass annotations. These are exactly same as that of Junit 4 annotations


27.Provide the XML Suite file name as testng.xml . Click on Finish button
28.Now your eclipse should create a class as shown in figure Below
29.Open the testng.xml under src folder and copy/paste the below stuff


<suite name="Suite" parallel="tests">
    <test name="IexploreTests">
        <parameter name="selenium.host" value="localhost"></parameter>
        <parameter name="selenium.port" value="5556"></parameter>
        <parameter name="selenium.browser" value="*firefox"></parameter>
        <parameter name="selenium.url" value="http://www.google.com"></parameter>
        <parameter name="searchCriteria" value="IExploreTests"></parameter>
        <classes>
            <class name="com.mycompany.GoogleTest" />
        </classes>
    </test>
    <test name="FirefoxTests">
        <parameter name="selenium.host" value="localhost"></parameter>
        <parameter name="selenium.port" value="5557"></parameter>
        <parameter name="selenium.browser" value="*firefox"></parameter>
        <parameter name="selenium.url" value="http://www.google.com"></parameter>
        <parameter name="searchCriteria" value="FirefoxTests"></parameter>
        <classes>
            <class name="com.mycompany.GoogleTest" />
        </classes>
    </test>
</suite>
30. I had created 2 tests with name 'IexploreTests'  and 'FirefoxTests'
31.This is just to drive a single testcase on different browsers. If you want to run the test on Chrome, We will have to create another test parameter with different RC configurations.
32.Also parallel parameter should be set to tests inorder to run these test parallely on multiple browsers
33.Now open the testng class which I had created under step 26
34. Copy and Paste the below code into the testng class and Save it
35.In the below code, i had created one test which Im parameterizing with different sets of values from testng.xml

 package com.mycompany;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

public class GoogleTest {
    Selenium selenium;

    @BeforeClass
    @Parameters({ "selenium.host", "selenium.port", "selenium.browser",
            "selenium.url" })
    public void beforeClass(String host, String port, String browser, String url) {
        this.selenium = new DefaultSelenium(host, Integer.parseInt(port),
                browser, url);
        this.selenium.start();
        this.selenium.open("");
    }

    @AfterClass
    public void afterClass() {
        this.selenium.close();
        this.selenium.stop();
    }

    @Test
    @Parameters({ "searchCriteria" })
    public void testGoogleSearch(String criteria) {
        this.selenium.type("name=q", criteria);
        try {
            Thread.sleep(15000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }   
}



35.To run the tests, open the Testng.xml and right click >> Run as >> Run Configurations
36. Select TestNG from the left list
37. Click on Add (+) button on the topleft
38.Select the project
39.Select the TestSuite as src/testng.xml
40.Apply and run the settings
41.Now you should be able to run the tests on multiple browsers parallely

20 comments:

  1. Hey Kiran i am eagerly waiting for the post....

    ReplyDelete
  2. Hopefully by this week end I would post it

    ReplyDelete
  3. Hi Kiran,
    The post was wonderful. tx u so much.

    When are the next posts coming up
    "Parallel distribution of tests over multiple browsers on different machines and on same machines"

    ReplyDelete
  4. Hey Kiran
    Good work !!
    i am eagerly waiting for the post too !

    ReplyDelete
  5. Hmm, I don't think he will post the next thing. After I searched three whole days for a nice guide, I found an incomplete one :(

    ReplyDelete
  6. Sorry Friends. I couldnt get time to update this . Definetly, I would complete this post by this week :)

    ReplyDelete
  7. Friends, Finally Im done with the posting. Can any of you try to execute the steps. Please let me know, if you have any issues in setting up and running grid

    ReplyDelete
  8. Kiran, I am using perl framwork can we do this with perl ..?

    ReplyDelete
  9. I think should be poosible but not very sure on how to achieve it :-(

    ReplyDelete
  10. Yes kiran.. if there is a way(may be a package) to execute code in parallel using perl I am done. Let me try.

    ReplyDelete
  11. Kiran, I tried the steps you shared here. I run firefox and Ie parallel for my tests, what happening is test on firewfox is sucessful but opening the URL with IE failed saying session Id returned is null. Have you face this problem?

    ReplyDelete
  12. Hey Kiran I have a quick question. Despite of having @Test provided for a method, I get an error "junit.framework.AssertionFailedError"

    Any thoughts?

    ReplyDelete
  13. Hey Kiran I have a quick question. Despite of having @Test provided for a method, I get an error "junit.framework.AssertionFailedError"

    When I comment "extends SeleneseTestCase" It works for me.

    Any thoughts why?

    ReplyDelete
  14. Nag,

    I think you had tried to create tests in Junit 3 .
    @Test are the annotations provided from Junit 4 onwards

    ReplyDelete
  15. Hi kiran,

    very good ground work...Your way of presentaiton is simple superb.. You are providing the step by step, it is very useful for us..

    we are waiting for your next post........

    ReplyDelete
  16. good tutorial!!!Thanks Kiran. But, How can we maintain Historic Testng HTML reports on Hudson?

    ReplyDelete
  17. hi,thanks for the tutorial , its working fine but when i run , it only gives me a blank browser, and thats it , shouldnt it open google.com ??

    please help

    ReplyDelete
  18. This comment has been removed by the author.

    ReplyDelete
  19. How to handle robot key and mouse events from remote machine (selenium grid) ?? please advice how to resolve this issue.

    ReplyDelete