This blog will take you on a journey to Selenium world. I will talk about choosing framework for QA test automation using Selenium WebDriver. Further I have given instance of few Selenium test cases written using TestNG and how to integrate them with one of the build tool – Ant.
21
Apr 2014
by:
Archana Mulay
Automated Selenium Testing TesNG

 

Many, perhaps most, software applications today are written as web-based applications to be run in an Internet browser. Test automation is nothing but using a software tool to run repeatable tests for such web applications. There are many advantages of test automation. Most are related to the repeatability of the tests and the speed at which the tests can be executed. There are a number of commercial and open source tools available for assisting with the development of test automation. But ‘Selenium’ is possibly the most widely-used open source solution for this.

 

Selenium IDE

Selenium's tool suite includes the following:

 
  1. Selenium IDE
  2. Selenium 1 i.e. Selenium RC or Remote Control
  3. Selenium 2 i.e. WebDriver
  4. Selenium Grid

 

 

Why choose ‘Selenium Webdriver’ for QA test automation?

 

 'Selenium RC' and 'Selenium WebDriver' allow you to write automation scripts in programming languages with great ease. WebDriver is the successor to Selenium RC. Selenium RC has been officially deprecated in favor of Selenium WebDriver. Selenium IDE is a Record and Playback tool. Though, it’s very easy to use, but is highly unreliable. It also does not provide programming facility. Although the scripts recorded

 by Selenium IDE can be saved for later use, it is not designed to run your test passes. The test cases will fail due to a small change in UI, or for things like change in name of a UI component. Selenium-Grid allows the Selenium RC solution to scale for large test suites and for test suites that must be run in multiple environments. Hence, Selenium Webdriver is the preferred tool to automate your testcases. 

 

What is an automation framework? 

 

A test automation framework is a set of concepts, and practices that provide support for automated software testing. It is a methodology built to successfully carry out test automation. If we do not have any frameworks, then it is difficult to get proper reports, handle checkpoints, or exception handling.

 

Which Automation framework to choose?

 

Now either you may choose from any of the frameworks available like JUnit & TestNG or you can design our own framework.

Junit is a unit testing framework or the Java programming language. TestNG is specially designed to cover all types testing categories like Unit, Functional testing, Integration testing, End-to-end etc. 

TestNG is a preferred framework of QA analysts as it allows you to generate test reports in both HTML and XML formats. TestNG enable you to group test cases easily which is not possible in JUnit.  

Usually we want to integrate execution of automated test suites with build process. Ant is a software tool for automating software build processes. Ant can get source code from version control, compile source code, package compiled source code and resources, handle dependencies between its targets, run test cases, generate reports of test execution and much more. It is advisable to call your TestNG tests through Ant script, to integrate the testing with the build process. Using Ant you can also generate better reports than the default reports generated by TestNG.

 

Let’s get started with TestNG!                                    

                                                                                                                                       Introduction TestNG Framework

Here is a small tutorial to help QA professionals write Selenium test cases in TestNG, and calling the TestNG suite from Ant. You can use any of the programming language supported by Selenium 2. I have used Java in below tutorial.

Before getting started, install these software's

(if you don't have):

 

  1. JDK
  2. Eclipse
  3. TestNG: Download TestNG from Eclipse's Help menu.
  4. Download Selenium server jar from http://docs.seleniumhq.org/download/

 

Steps to follow:

 

Step 1: Use Eclipse. Create a new Java project.

 

Step 2: Add Selenium server jar as external jar file to your project. To do this, right click on any file/folder in the project in Eclipse. Select Build Path -> Configure Build Path. Select 'Libraries' tab. Click on 'Add external jar'. Select the Selenium server jar file you have previously downloaded.

 

Step 3: Use TestNG library in your project: right click on any file/folder in the project in Eclipse. Select Build Path -> Configure Build Path. Select 'Libraries' tab. Click on 'Add Library'. Select TestNG. Click 'Ok'.

 

Step 4: A TestNG testsuite comprises of number of tests. A TestNG 'test' comprises of one or more classes, and a TestNG class consists of multiple 'Test Methods'.

 

Add a new Java class 'TestClass1' to the project. Following is a set of Selenium test cases written as TestNG methods. In the code below, I have written a set of test cases to test search functionality of the website http://www.floraindia.com. 4 test cases are added here for demonstration purposeAdd following code to the Java class.

 

 import java.util.concurrent.TimeUnit;

 import org.openqa.selenium.By;

 import org.openqa.selenium.WebDriver;

 import org.openqa.selenium.firefox.FirefoxDriver;

 import org.testng.Assert;

 import org.testng.annotations.BeforeClass;

 import org.testng.annotations.Test;

 import org.testng.annotations.AfterClass;

 

 public class TestClass1 {

            protected static WebDriver wd;

            protected static String result;

            @BeforeClass

 public static void setup()  {

            wd = new FirefoxDriver();

            wd.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

           }

            @Test

 void Testcase1() {

            wd.get("http://www.floraindia.com");

            wd.findElement(By.id("kwsch")).sendKeys("Red");

            wd.findElement(By.xpath("//input[@src='images/go.gif']")).click();

            result = wd.findElement(By.xpath("//font[text()='Total Items  :']//following::td[1]")).getText();

 

 Assert.assertEquals(result, "115");

            }

           

            @Test

 void Testcase2() {

            wd.get("http://www.floraindia.com");

            wd.findElement(By.id("kwsch")).sendKeys("Blue");

            wd.findElement(By.xpath("//input[@src='images/go.gif']")).click();

            result = wd.findElement(By.xpath("//font[text()='Total Items  :']//following::td[1]")).getText();

            Assert.assertEquals(result, "13");

             }

            @Test

 void Testcase3() {

            wd.get("http://www.floraindia.com");

            wd.findElement(By.id("kwsch")).sendKeys("Yellow");

            wd.findElement(By.xpath("//input[@src='images/go.gif']")).click();

            result = wd.findElement(By.xpath("//font[text()='Total Items  :']//following::td[1]")).getText();

            Assert.assertEquals(result, "27");

             }

            @Test

 void Testcase4() {

            wd.get("http://www.floraindia.com");

            wd.findElement(By.id("kwsch")).sendKeys("Purple");

            wd.findElement(By.xpath("//input[@src='images/go.gif']")).click();

            result = wd.findElement(By.xpath("//font[text()='Total Items  :']//following::td[1]")).getText();

            Assert.assertEquals(result, "10");

             }

           @AfterClass

           public static void teardown()  {

            wd.close();

            wd.quit();

             }

 }

 

Step 5: Add following TestNG testsuite to your project as 'TestNG.xml'

 

 <?xml version="1.0" encoding="UTF-8"?>

 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

 <suite name="My sample Suite">

  <test name="Search test">

    <classes>

      <class name="TestClass1" />

    </classes>

  </test>

 </suite>

 

Step 6: Execute above TestNG testsuite by right clicking on the TestNG.xml and selecting Run as -> TestNG suite. The testcases are executed and the summary of test execution is shown in Eclipse's console. TestNG automatically generates report for your test execution. To see the report, go to your project directory, and select test-output folder. Open idex.html. As it can be seen in the below report, Testcase1,2 and 3 are passed. Testcase4 is failed.

 

Test Execution Report TestNG

                                    Default Test execution Report generated by TestNG

 

Step 7: So TestNG suite was executed successfully! Now we want to use Ant to run the TestNG testsuite, instead of eclipse. We need to specify location of Selenium server jar and TestNG jar to Ant. Let's create a new folder in the project and put the jar files there.

i. Create a folder 'lib' in your Java project.

ii. Copy Selenium server jar to this 'lib' folder.

iii. Copy testng.jar too, in the same folder. In you Eclipse directory, go to plugins directory. There will be a directory like org.testng.eclipse_6.8.6.20130914_0724. Under this directory, in lib folder you will have testng.jar. 

 

Step 8: Add Ant script with name build.xml to your project as follows. 

 

 <?xml version="1.0" encoding="UTF-8"?>

 <project name="automation" default="main" basedir=".">

 <!--Define Path-->

 <path id="project-classpath">

    <fileset dir="./lib" includes="*.jar"/>

    <pathelement location="bin"/>

 </path>

            <target name="main" depends = "init, compile, run">

            </target>

 <!-- Initialization -->

            <target name="init">

                        <echo>starting init task</echo>

                        <delete dir="bin"/>

                        <mkdir dir="bin"/>

            </target>

 <!--Compile-->

            <target name="compile">

                        <javac srcdir="src" destdir="bin">

                                    <classpath>                            

                                        <fileset dir="./lib" includes="*.jar"/>

                                        <pathelement location="bin"/>                             

                                    </classpath>

                        </javac>

            </target>

 <!--Define TestNG task-->

 <taskdef name="testng" classname="org.testng.TestNGAntTask">

               <classpath>

                        <pathelement location="./lib/testng.jar"/>

               </classpath>

              </taskdef>

 <!--Execute TestNG testsuite -->

            <target name ="run">

               <testng outputdir="./test-output" classpathref="project-classpath"  useDefaultListeners="true">

       <xmlfileset dir="." includes="src/TestNG.xml"/>

               <!-- generate report title -->

                <sysproperty key="org.uncommons.reportng.title" value="Test Automation"/>

                <sysproperty key="org.uncommons.reportng.escape-output" value="false"/>

               </testng>

            </target>

 </project>

 

 

Step 9: Finally, execute the Ant file. Go to command prompt, go to you project's directory. Type ant. It will start executing the ant script build.xml in your project. When the ant script is executed successfully, view the report generated by TestNG at same location i.e. index.html file in your project's test-output directory.

Happy QA with Seleniumsmiley

 

This blog is written by Archana Mulay, Senior QA at Faichi Solutions. Faichi holds expertise in QA & provide end to end QE services to several clients. Automation testing is an integral part of Faichi QA to ensure quality in a speedy and reliable way. We have designed our own test automation framework using SELENIUM and TestNG. This framework enables us to consistently deliver quality solutions across all clients. Know more about our QA Services.

 

With inputs from Selenium official documentation http://docs.seleniumhq.org/docs.

 

Share this post

By, Archana Mulay

Archana is working as a Senior QA at Faichi Solutions. She holds expertise in Selenium WebDriver, TestNG and has industry experience of more than a decade, working as a software engineer & QA analyst. Her major focus is to achieve excellent product quality for all projects by planning appropriate testing required for a product and following up with carrying out tests. She is passionate about developing and maintaining standardized QA processes for every project. Archana is an avid reader & a traveler who loves to explore new places.