//
you're reading...
Continuous Integration

Continuous Integration with Selenium

I have seen a lot of queries from people who basically want to know how to blend Selenium, Maven, and hudson to achieve continous integration.

So I thought I would write up my own recipe which has worked for me.

The ingredients that you would need for this recipe .

  • Maven Standalone : A standalone installation of maven. (You can refer here for instructions on how to get it done )
  • An IDE : I have used only eclipse. As a popular saying goes “a known devil is better than the unknown angel” I am sticking to eclipse. You can download a copy of eclipse from here
  • Maven plugin : The newer versions of eclipse by default comes bundled with the maven plugin. But if you already have an eclipse copy with you and just want to add the maven plugin, you can add it from here
  • TestNG plugin : You can add TestNG plugin to your eclipse installation from here.  If you are not very familiar with adding plugins to eclipse, you can refer here
  • Jenkins/Hudson instance for your continuous integration runs. For setting up hudson you can refer here

So lets start, now that we have our environment all setup and ready.

To begin with lets create a simple maven project from the command line. (for detailed help, you can refer here )

Let us first start by creating a simple maven project, from the command line. You would need to ensure you have maven available in your local machine.

Run the command :

mvn archetype:generate -DgroupId=organized.chaos 
-DartifactId=insanity -DpackageName=organized.chaos.insanity -Dversion=1.0-SNAPSHOT

So here maven would create a directory named “insanity” which represents the root directory of my new maven project.

Once the project is created, create a folder under src/test and call it as “resources”.

Now lets take a look at the folder structure of our empty project (the folder name would be ‘insanity’)

$ find . -type d -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g' (I found this cool unix command from here)
 .
 |____src
 | |____main
 | | |____java
 | | | |____organized
 | | | | |____chaos
 | |____test
 | | |____java
 | | | |____organized
 | | | | |____chaos
 | | |____resources

Lets go ahead and add TestNG and selenium as dependencies to our pom file. Open up the pom file in eclipse or any other editor of your choice and add the below :

<dependency>
 <groupId>org.testng</groupId>
 <artifactId>testng</artifactId>
 <version>6.4</version>
 </dependency>
 <!-- Adding Selenium dependency -->
 <dependency>
 <groupId>org.seleniumhq.selenium</groupId>
 <artifactId>selenium-server</artifactId>
 <version>2.21.0</version>
 </dependency>

Time to add the compiler plugin because we need to use TestNG annotations and annotations worked only with JDK 1.5 and above.

So, lets add a <build> section to our pom file, and then lets add the compiler plugin as below:

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-compiler-plugin</artifactId>
 <configuration>
 <source>1.6</source>
 <target>1.6</target>
 </configuration>
 </plugin>

Now go ahead and add all of your TestNG tests as desired and then create a suite file out of them. The simplest way of creating a suite file out of your TestNG tests from within eclipse would be to
multi-select them (using shift/ctrl + click) and then right click and say convert to TestNG.

The TestNG plug-in should give you a nice IDE using which you can create your suite file on the fly.

So our suite file is ready, and we need to just let maven know, that it has to pick up our suite file, whenever the test phase is invoked within maven.
This is where we leverage surefire-plugin. You can read more about surefire-plugin and all the options that it comes with here

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-surefire-plugin</artifactId>
 <version>2.6</version>
 <configuration>
 <!-- We use a property here, because we want the ability to change the suite file on the fly 
during runs '-DsuiteXmlFile=src/test/resources/testng-sample.xml' -->
 <suiteXmlFiles>
 <suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
 </suiteXmlFiles>
 <!-- Build with '-DskipTests=true' to bypass test execution @ build time Default: false -->
 <skipTests>${skipTests}</skipTests>
 <testFailureIgnore>true</testFailureIgnore>
 </configuration>
 </plugin>

Now our project is all ready. Here I have used two custom defined properties viz., “suiteXmlFile” and “skipTests”. This is how their definition looks like.

<properties>
 <suiteXmlFile>src/test/resources/test1.xml</suiteXmlFile>
 <skipTests>false</skipTests>
 </properties>

Now why would I want to define custom properties, add it to the surefire plugin ?

Well I am doing this, so that when I am running tests from the command line, I can have the luxury of changing my suite file or telling maven to ignore my tests.

Something like this:

mvn test -DskipTests=true (This would tell maven not to run my unit tests at all)
mvn test -DsuiteXmlFile=src/test/resources/test2.xml
 (This would tell maven to run tests from test2.xml and not from test1.xml as defined in the pom file)

Make sure that you check-in your project into some version control system such as git/svn because hudson/jenkins pulls source code from version control systems and not from file systems.

Once you have your hudson instance setup, and are in the configure job page of your jenkins/fusion instance, look for a “build” section.

There in the Goals and options section, all you would do is specify the goals

for e.g.,

test -DsuiteXmlFile=src/test/resources/test2.xml (if you want this job to run unit tests from test2.xml)

or just

test (if you want to by default run the tests from test1.xml)

Here’s how my complete pom file looks like :

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
<groupId>organized.chaos</groupId>
 <artifactId>insanity</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>jar</packaging>
<name>insanity</name>
<properties>
 <suiteXmlFile>src/test/resources/test1.xml</suiteXmlFile>
 <skipTests>false</skipTests>
 </properties>
<dependencies>
 <!-- Adding TestNG for unit test support -->
 <dependency>
 <groupId>org.testng</groupId>
 <artifactId>testng</artifactId>
 <version>6.4</version>
 </dependency>
 <!-- Adding Selenium dependency -->
 <dependency>
 <groupId>org.seleniumhq.selenium</groupId>
 <artifactId>selenium-server</artifactId>
 <version>2.21.0</version>
 </dependency>
 </dependencies>
 <build>
 <plugins>
 <!-- using the compiler plug-in to specify that this project is to be compiled with JDK 1.6 -->
 <!-- This is needed so that we get the JDK annotation support that was introduced recently -->
 <plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-compiler-plugin</artifactId>
 <configuration>
 <source>1.6</source>
 <target>1.6</target>
 </configuration>
 </plugin>
 <plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-surefire-plugin</artifactId>
 <version>2.6</version>
 <configuration>
 <!-- You can specify a specific testng.xml file here <suiteXmlFiles>
 <suiteXmlFile>src/test/resources/testng-sample.xml</suiteXmlFile> </suiteXmlFiles> -->
 <!-- Or dynamically with something like '-DsuiteXmlFile=src/test/resources/testng-sample.xml' -->
 <suiteXmlFiles>
 <suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
 </suiteXmlFiles>
 <!-- Build with '-DskipTests=true' to bypass test execution @ build time Default: false -->
 <skipTests>${skipTests}</skipTests>
 <testFailureIgnore>true</testFailureIgnore>
 </configuration>
 </plugin>
 </plugins>
 </build>
 </project>

Hope that helps you get started with Continuous Integration with Selenium

About these ads

Discussion

9 thoughts on “Continuous Integration with Selenium

  1. Hi Krishnan,

    I have setup like above as you said for suite file and pom.

    My suite file consists of 3 tests say test1,test2, test3.if run this suite from eclipse, was running correctly with the given order [ first test1, then test2 and test3].

    if i run the same suite file from cmd with mvn test -Dsuitefile=/src/…/a.xml
    the order varies [ most of the times test1 is not running at first].

    can you kindly let me know what could be the issue and how to resolve this issue.

    Testng Version 6.1.1
    Maven 3.0.3

    Posted by Pradeep | November 28, 2012, 6:59 pm
  2. Thanks for you article! I have a question: for example, i specified ${suiteXmlFile} and run tests like “mvn test -DsuiteXmlFile=/test.xml2″ but i want that if the -DsuiteXmlFile is not defined, pom.xml would run some default xml.. how can I do that?

    Posted by German | December 6, 2012, 4:21 pm
  3. can u provide me test1.xml for this….

    Posted by subbarao | June 27, 2013, 7:56 pm
  4. Hi Krishnan,

    I have 64 bit windows 7.
    I am usin JDK(32bit) and eclipse Kepler (32bit).

    With eclipse maven is integrated but i want to use maven 2 standalone with the same so exactly how to disable eclipse maven.
    And how i can tell eclipse which JAVA,Maven to use before opening it by using bat file.
    Thanks in advance

    Posted by rahul | September 2, 2013, 4:57 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: