Integration of Selenium RC with Jenkins

Hi followers,
 Thanks for the great support and it make me more enthusiastic to post more stuffs related to Selenium. In this post we will see how to integrate Selenium Scripts with Jenkins. 

First we will look what is Jenkins, why we need that and what is the benefit we are going to achieve because of Selenium integration

Jenkins is an open source continuous integration tool written in Java. Jenkins provides continuous integration services for software development. It is a server-based system running in a servlet container such as Apache Tomcat. It supports SCM tools including CVS, Subversion, Git, Mercurial, Perforce and Clearcase, and can execute Apache Ant and Apache Maven based projects as well as arbitrary shell scripts and Windows batch commands. Builds can be started by various means, including being triggered by commit in a version control system, scheduling via a cron-like mechanism, building when other builds have completed, and by requesting a specific build URL.

So that, whenever new build is deployed using Jenkins we don’t want to run the scripts for verifying the functionalities are working fine after some time. We can configure the selenium to run after the build deployment. It provides the End to end test and reduces the Manual intervention.
  
Getting Started with Selenium:
         
      1) Create your selenium framework either Junit or TestNG. By using our selenium with Junit or TestNG we will get a neat report which is autogenerated one.

Here I am using Sample Test program with Junit,
public class ScenarioOne{

                /**
                 * @param args
                 */
                public static SeleniumServer server=null;
                static DefaultSelenium selenium=null;
               
                @Before
                public void setUp() throws Exception {
                               
                                //How to start your Selenium server since we are going to run the program in Jenkin Server
                                startSeleniumServer(server); //for you reference I have added the script within the class
                                selenium = new DefaultSelenium("localhost", 4444, "*firefox", baseUrl);
                                selenium.start();
                                selenium.setTimeout(strPageLoadTime);
                                selenium.windowMaximize();
                }
               
                @Test
                public void testFuctionOne() throws Exception {
                                // TODO Auto-generated method stub

                                //Action which you would like to perform in the application
                               
                                selenium.close();
                }
               
                @After
                public void tearDown() throws Exception {
                                stopSeleniumServer(server, selenium);//for you reference I have added the script within the class
                }
               
                public static void startSeleniumServer(SeleniumServer server) throws Exception {

                try {
                ServerSocket serverSocket = new ServerSocket(RemoteControlConfiguration.DEFAULT_PORT);
                serverSocket.close();
                                                //Server not up, start it
                                                try {
                                                 RemoteControlConfiguration rcc = new RemoteControlConfiguration();
                                                 rcc.setPort(RemoteControlConfiguration.DEFAULT_PORT);
                                                 server = new SeleniumServer(false, rcc);

                                                } catch (Exception e) {
                                                                System.err.println("Could not create Selenium Server because of: "
                                                                                                + e.getMessage());
                                                                e.printStackTrace();
                                                }
                                                try {
                                                                server.start();
                                                                System.out.println("Server started");
                                                } catch (Exception e) {
                                                                System.err.println("Could not start Selenium Server because of: "
                                                                                                + e.getMessage());
                                                                e.printStackTrace();
                                                }
                                } catch (BindException e) {
                                                System.out.println("Selenium server already up, will reuse...");
                                }
                }

                public static void stopSeleniumServer(SeleniumServer server, DefaultSelenium selenium){
                                selenium.stop();
                                if (server != null)
                                  {
                                                 try
                                                 {
                                                                 selenium.shutDownSeleniumServer();
                                                                 server.stop();

                                                                server = null;
                                                 }
                                                 catch (Exception e)
                                                 {
                                                                e.printStackTrace();
                                                 }
                                  }
                }
}
2) Create the build.xml file, which are going to use to run the scripts from Jenkins. The xml snippet is below,
<project>
    <path id="classpath">
        <fileset dir="lib" includes="**/*.jar"/>
    </path>

    <target name="clean">
        <delete dir="build"/>
                   <delete dir="reports"/>
    </target>

    <target name="compile" depends= "clean">
        <mkdir dir="build/classes"/>
        <javac srcdir="src" destdir="build/classes" classpathref="classpath"/>
    </target>

    <target name="jar" depends= "compile">
        <mkdir dir="build/jar"/>
       
                <jar destfile="build/jar/Demo.jar" basedir="build/classes"/>
          
    </target>
                <target name="test-all-xml" depends= "jar">
                                <mkdir dir="reports"/>
                                <mkdir dir="reports/xml"/>
                                <junit fork="yes" printsummary="no">
                                                <classpath>
                                                <path refid="classpath"/>
                                                                <path location="build/jar/Demo.jar"/>
                                </classpath>
                                                <formatter type="xml" />
                                <batchtest todir="reports/xml" fork="yes">
                                                <fileset dir="build/classes">
                                                <include name="**/Scenario*.class" />
                                                </fileset>
                                </batchtest>
                                </junit>

                                <junitreport todir="reports">
                                <fileset dir="reports/xml" />
                </junitreport>
    </target>
</project>
3) Our script part is over, now we will move to Jenkins part.

For demo purpose, here I have started Jenkins server locally.
If you have direct access to the Jenkins webpage you can make use of it.
Except the Jenkins server initiation, remaining procedures are same.
a) Download the Jenkins war file from the location “http://jenkins-ci.org/”
b) In order to start the Jenkins server locally. From the command prompt, navigate to the location where you have saved the war file and You can execute Jenkins like this:
java -jar jenkins.war
c) To access the Jenkins, open a web browser with the URL http://localhost:8080 (by default Jenkins start at the port 8080)
d) Click on NewJob links which is displayed in the Left pane.
e) Provide the Job Name. For example “JenkinDemo”
f)  Select the first option “Build a free style software project” option since we are going to map our scripts (our local path or you can use SCM path where you have uploaded the scripts with build.xml file) and going to execute the selenium scripts.
g) Click on Ok button
h) Enter  the project name in the configuration page  and mention any description to explain the purpose of this project for other team members
i) Navigate to the section “Advanced Project Options” and select “Use custom workspace” option.
j) Mention the path of the folder which contains the build.xml file (You can also mention the SCM folder path also)
k) Name the path by entering some value in the Display Name text field
l)  Navigate to Build section and select Invoke Ant option since here we are using Build.xml file for script execution.
m) After selection Invoke Ant option,  two text field will be displayed
n)  Enter “Default” in the Ant Version and Enter the Target name (Target Name can be found in the Build.xml file) which you wish to run from Jenkins. For example “test-all-xml”
o)  To print the Junit report in the Jenkins, just navigate to the section “Post-build Actions” and select “Publish JUnit test result report” option.
p)  Enter the folder name and the report xml file in the “Test report XMLs” edit field. As per our Build.xml file, the Junit report folder will be generated in the folder where a Build.xml file is presented and the xml files are in the folder “report”. So, the value is “reports\TESTS-*.xml”
q)  Check the checkbox “Retain long standard output/error”, to retain the value in Jenkins.
r)   Click on Apply and Save button to save all the configuration settings.

Now we are good to execute the Selenium scripts from Jenkins by simply clicking Build Now link from the left pane.
 for more details about Jenkins, please refer,  https://wiki.jenkins-ci.org/display/JENKINS/Starting+and+Accessing+Jenkins

Popular posts from this blog

Selenium Webdriver Interview Questions with Answer

Selenium IDE Interview Questions with Answer

DLL to Jar file conversion