Tuesday, April 12, 2011

Selenium Grid with JUnit

I just tried my hands on Selenium Grid few days back as our tests were taking too much time to execute, almost 5 hours if we run all and so we thought to use Selenium Grid to cut down the time.  I downloaded the Selenium Gid, run the demo and it all went well until I come to know that your framework should also support parallel execution if you want to run multiple test parallel using Grid. And the problem was JUnit doesn't provided parallel execution. One more reason why you should use TestNG...

There is solution where you can write your own class to parallelize the JUnit testcases  or better download from the Internet. There are so many available. But I found the another and bit easy way.

You can use the ANT parallel task to achieve this parallelism. Following is the demo which shows how to execute testcases parallel with Selenium Grid and JUnit.
Download the demo
  1. Download the selenium gird from here and extract it into some folder
  2. Now navigate to extracted folder through command prompt and type following command
    ant launch-hub
    This will launch the hub on port 4444, this is the port where your selenium tests need to connect.
  3. Now you need to launch the remote control.type the following command on command prompt
    ant launch-remote-control
    This will launch the remote control on port 5555. 
  4. Now we have launched one remote control, we will launch another remote control on port 5556. So one class will execute all it's test on remote control running on 5555 and another class will run it's testcases on remote control running on 5556. Type the following command to launch the remote control on 5556 port.
    ant launch-remote-control -Dport=5556
  5. Now we have two remote control running on the same machine we need to run testcases. 
  6. demo1.java
    import static org.junit.Assert.assertTrue;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    
    import com.thoughtworks.selenium.DefaultSelenium;
    import com.thoughtworks.selenium.Selenium;
    
    /**
     * @author Gaurang
     */
    public class demo1 {
     Selenium selenium;
    
     @Before
     public void setUp() throws Exception {
      selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.co.in/");
      selenium.start();
      selenium.setTimeout("6000");
     }
     
     @Test
     public void test_1() throws Exception {
      System.out.println("1");
      selenium.open("/");
      selenium.type("q", "1");
     }
     @Test
     public void test_2() throws Exception {
      System.out.println("2");
      selenium.open("/");
      selenium.type("q", "2");
     }
    
     @After
     public void tearDown() throws Exception {
       selenium.stop();
     }
    }
    
  7. demo2.java
    import static org.junit.Assert.assertTrue;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    
    import com.thoughtworks.selenium.DefaultSelenium;
    import com.thoughtworks.selenium.Selenium;
    
    /**
     * @author Gaurang
     */
    public class demo2 {
     Selenium selenium;
    
     @Before
     public void setUp() throws Exception {
      selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.co.in/");
      selenium.start();
      selenium.setTimeout("6000");
     }
     
     @Test
     public void test_3() throws Exception {
      System.out.println("3");
      selenium.open("/");
      selenium.type("q", "3");
     }
     @Test
     public void test_4() throws Exception {
      selenium.open("/");
      selenium.type("q", "4");
     }
      
     @After
     public void tearDown() throws Exception {
       selenium.stop();
     }
    }
    
  8. Build.xml
    <project name="demo" default="run" basedir=".">
     <path id="lib.classpath">
      <fileset dir="lib">
       <include name="*.jar" />
      </fileset>
      <pathelement location="bin" />
     </path>
     
     <target name="run" depends="compile">
      <parallel threadCount='4'>
       <junit printsummary="withOutAndErr" haltonfailure="no">
        <formatter type="xml" usefile="true" />
        <classpath refid="lib.classpath" />
        <batchtest fork="true" todir="results" failureproperty="seleniumTests.failed" errorproperty="seleniumTests.failed">
         <fileset dir="bin">
          <include name="demo1.class" />
         </fileset>
        </batchtest> 
       </junit>
      
      
       <junit printsummary="withOutAndErr" haltonfailure="no">
       <formatter type="xml" usefile="true" />
       <classpath refid="lib.classpath" />
       <batchtest fork="true" todir="results" failureproperty="seleniumTests.failed" errorproperty="seleniumTests.failed">
        <fileset dir="bin">
         <include name="demo2.class" />
        </fileset>
       </batchtest> 
       </junit>
      </parallel>
     </target>
     
     <target name="compile">
     <echo> compiling.....</echo>
      <javac srcdir="src" destdir="bin" classpathref="lib.classpath" />
     </target>
    </project>
  9. Now you run the testcases using build.xml file. Use the following command to run the testcases.
    ant run
    This will run demo1.class and demo2.class in parellel.

11 comments:

Rahul Mendiratta said...

Its not working for me... Please help

Darshana Jayasinghe said...

Can you show us the error? I tried it, it worked :)

Linkesh Kanna Velu said...

HI, Nice document. But my requirement is to execute the same class with different browser parameters simultaneously. Is it possible using ANT+Grid

Shan said...

Can you please put the commands to run the test cases?
I have stared Hub and opened 2 instances of remote control. Its showing in console page under Available RCs...
But after that, how can I run my own tests?

Unknown said...

Hi Shanmugavel,

Read the step 9 again. you need to run the ant target.

Varun Kumar Rao Ponugoti said...

Is there any way to capture results Browser wise when we are using selenium grid 2 for parallel testing.

EX:I am running the whole suit on windows Firefox and on Linux Firefox, Is it possible to get the results Browser wise ....

The explanation of the way of doing it also sufficient.

Any help regarding this is appreciated

Mallikarjun said...

getting an error:lib not found??

yami said...

Hi,
Thanks for the nice explanation. It helps. I could run this on my machine.On same browser as well on different browser.
Like demo1 on Firefox and demo2 on chrome.
To Linkesh-You need to start the RC instance with -Denvironment=*googlechrome and pass the same from demo2.java. Then it will run on google chrome.

All, i am still trying to understand one issue i.e. currently all the classes from Bin directory is getting executed.

Now suppose, i have created a testSuite called MySuite.java.
It includes two tests A.java and B.java.
While calling ant compile, it produces three class files in bin diretcory.
How should i run only MySuite.java?
In build.xml i gave



But still it runs first A.java then B.java and then MySuite.java(that internally calls A.java and B.java again).

Also it would be great if you can explain the setup to run RC on different machines and how to run the test in such case.

GeeSlimmy said...

So far this works fine for me. I was just curious if WebDriver/JUnit/Grid and Ant build.xml will work?

Here's what I found that works with Grid so far:

1. TestNG / Grid
2. TestNG / WebDriver / Grid
3. JUnit / Grid

Now I'm going to try JUnit / WebDriver / Grid. Not sure if it will work with build.xml and ant.

Anonymous said...

Hi
I got the error:
BUILD FAILED
C:\Java\Myselenium-grid-1.0.8\build.xml:36 destination directory "C:\Java\Myselenium-grid-1.0.8\bin" does not exit or is not a directory

Total time: 0 seconds

Bhanu Prasad said...

Hi Gaurang,

We thought of setting up of selenium grid to our current project. So started reading available blogs.

I have a doubt, since we use same database how this parallel execution will be possible on same machine. I will elaborate, we are writing selenium testcases with Initial data (seed data) at the start of every selenium test case. How the testcases will run?

Thanks
-Prasad

Post a Comment