Popular Posts

Wednesday, December 8, 2010

Automation Testing of web apps using Selenium-TestNG,

Hi All.This is my first post and i am very excited to share about the open source tool-Selenium.Selenium is the most powerfull open source tool for automating web based applications and is now the emerging tool in the market.So if you are a beginner then this is the right place for you to learn about selenium.
To begin with,i have designed a framework for automating web based application which i would be sharing with you all.
Now let's talk about the framework that i have used.if you want to work with this framework then you must have following things in your machine before you start working on selenium.

1.Selenium IDE(mozilla-plugin)-http://seleniumhq.org/download/
2.Firebug-http://getfirebug.com/
3.Selenium-RC-http://seleniumhq.org/download/
4.Selenium-Grid(runs scripts faster than rc)-http://seleniumhq.org/download/
5.TestNG-http://testng.org/doc/download.html
6.Eclipse
7-ReportNG.

Before we start,lets understand these above terms

Letz start with Selenium-IDE,
Selenium-IDE is the Integrated Development Environment for building Selenium test cases. It operates as a Firefox add-on and provides an easy-to-use interface for developing and running individual test cases or entire test suites. Selenium-IDE has a recording feature, which will keep account of user actions as they are performed and store them as a reusable script to play back. It also has a context menu (right-click) integrated with the Firefox browser, which allows the user to pick from a list of assertions and verifications for the selected location. Selenium-IDE also offers full editing of test cases for more precision and control.
Although Selenium-IDE is a Firefox only add-on, tests created in it can also be run against other browsers by using Selenium-RC and specifying the name of the test suite on the command line.

Features:
  • Easy record and playback
  • Intelligent field selection will use IDs, names, or XPath as needed
  • Autocomplete for all common Selenium commands
  • Walk through tests
  • Debug and set breakpoints
  • Save tests as HTML, Ruby scripts, or any other format
  • Support for Selenium user-extensions.js file
  • Option to automatically assert the title of every page
Selenium-RC:
Selenium-RC allows the test automation developer to use a programming language for maximum flexibility and extensibility in developing test logic. For instance, if the application under test returns a result set, and if the automated test program needs to run tests on each element in the result set, the programming language’s iteration support can be used to iterate through the result set, calling Selenium commands to run tests on each item.
Selenium-RC provides an API (Application Programming Interface) and library for each of its supported languages: HTML, Java, C#, Perl, PHP, Python, and Ruby. This ability to use Selenium-RC with a high-level programming language to develop test cases also allows the automated testing to be integrated with a project’s automated build environment.

Selenium-Grid
 Selenium-Grid allows the Selenium-RC solution to scale for large test suites or test suites that must be run in multiple environments. With Selenium-Grid, multiple instances of Selenium-RC are running on various operating system and browser configurations; Each of these when launching register with a hub. When tests are sent to the hub they are then redirected to an available Selenium-RC, which will launch the browser and run the test. This allows for running tests in parallel, with the entire test suite theoretically taking only as long to run as the longest individual test.

Here you can see the traditional set-up for your scripts using Selenium-RC
  Selenium-Grid Set-up



TestNG
TestNG is a testing framework designed to simply a broad range of testing needs, from unit testing(testing a class in isolation of the others),functional testing,browser-compatibility testing with Selenium-RC  to integration testing (testing entire systems made of several classes, several packages and even several external frameworks, such as application servers).
 Learn more about TestNG here:http://testng.org/doc/documentation-main.html
Now lets start implementing it.

First you need to record a script in selenium-ide,just try with a simple script and put some check points(to know how Selenium ide works visit:http://seleniumhq.org/projects/ide/)

Now convert your IDE script to any language as selenium supports multiple platforms like junit,TestNG,Python,C#.

Here i have used TestNG because of it's powerfull features like multi threading and annotaions

so convert the IDE script into TestNG format like this:

Now you must have an editor to do scripting which we call it as descriptive programming.so install Eclipse in your machine and you must set up the configuration is eclipse to run your scripts.Setting up configuration is quite tricky but once learnt how to then it becomes easy for you to set it up again.
You can visit this blog for setting up TestNG,Selenium-RC set up(http://epyramid.wordpress.com/2008/11/26/setting-up-selenium-rc-testng-using-eclipse/)

Once it is done then you can copy-paste your script from ide to Eclipse.

Now we have to instantiate selenium server in order to run your script,so we need to create the selenium instances in our scripts.given below is the code about how we instantiate selenium server on a particular host to launch the scripts

public class TestScript {

    private Selenium selenium;

@BeforeClass
@Parameters({"selenium.host","selenium.port","selenium.browser","selenium.url"})
public void startSelenium(String host,String port,String browser,String url) {

  this.selenium = new DefaultSelenium(host, Integer.parseInt(port), browser, url);
 this.selenium.start();
 this.selenium.open(url);
}


Add this before your code,now we have to pass values to the parameters as you can see in the above code to launch selenium server.we define parameters values in xml.here youc an see how the xml structure looks like:

<suite name=Test >
<parameter name="selenium.host" value="172.16.2.210"/>
<parameter name="selenium.url" value="http://www.google.com"/>
<parameter name="selenium.browser" value="*iexplore"/>
<parameter name="selenium.port" value="4444"/>
 
<test name="TestScript/">

<classes>
<class name="Script">
</class>
</classes>

</test>
 
</suite>

so you can see that we have passed selenium host value,port value,url in the xml.also you can observe that the class name of the test script is called here.
To run the script you need to call the class name of the Test Script in xml as given in above xml.

Now we have to launch the selenium-rc which will run our scripts.
so go to command promt,go to the path where your selenium-rc(selenium-remote-control) is there in your drive and type this command:
C:\selenium-remote-control-1.0.3\selenium-server-1.0.3>java -jar selenium-server.jar -port 4444

 This will launch the selenium-rc in port 4444 and now your script is ready for run.

once your script is succesfully completed then you can see the reports in your workspace directory in test-output folder.
This is just some basics which will be helpfull for beginners,soon i will put another post where i will explain about how to use testng features like groups,annotations,threading concept etc, reporting part and lots more...

If you have any questions and doubts please feel free to post your questions

Regards
Ajay

5 comments:

  1. awesome article... very helpfull indeed... keep posting...

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

    ReplyDelete
  3. good article..... v r waiting for latest updates keep posting
    Regards,
    Naveen kumar M

    ReplyDelete
  4. Very good article. You can also use JSystem. It is open source framework with powerful GUI and built in support for Selenium. It allows you to create and manage full featured scenarios with various types of parameters.

    ReplyDelete