Test Automation for Web Applications using Selenium 2

Posted by batman on Apr 18th, 2011
2011
Apr 18

Test Automation for Web Applications

Many, perhaps most, software applications today are written as web-based applications to be run in an Internet browser. The effectiveness of testing these applications varies widely among companies and organizations. In an era of highly interactive and responsive software processes where many organizations are using some form of Agile methodology, test automation is frequently becoming a requirement for software projects. Test automation is often the answer. Test automation means using a software tool to run repeatable tests against the application to be tested. For regression testing this provides that responsiveness. There are many advantages to 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. Selenium is possibly the most widely-used open source solution.

Introducing Selenium

Selenium is set of different software tools each with a different approach to supporting test automation. Most Selenium QA Engineers focus on the one or two tools that most meet the needs of their project, however learning all the tools will give you many different options for approaching different test automation problems. The entire suite of tools results in a rich set of testing functions specifically geared to the needs of testing of web applications of all types. These operations are highly flexible, allowing many options for locating UI elements and comparing expected test results against actual application behavior.

Selenium’s Tool Suite

Selenium is composed of multiple software tools. Each has a specific role.

Selenium 2 (aka. Selenium Webdriver)

Selenium 2 is the future direction of the project and the newest addition to the Selenium toolkit. This brand new automation tool provides all sorts of awesome features, including a more cohesive and object oriented API as well as an answer to the limitations of the old implementation.

Selenium 1 (aka. Selenium RC or Remote Control)

As you can read in Brief History of The Selenium Project, Selenium RC was the main Selenium project for a long time, before the WebDriver/Selenium merge brought up Selenium 2, the newest and more powerful tool. Selenium 1 is still actively supported (mostly in maintenance mode) and provides some features that may not be available in Selenium 2 for a while, including support for several languages (Java, Javascript, PRuby, HP, Python, Perl and C#) and support for almost every browser out there.

Selenium IDE

Selenium IDE (Integrated Development Environment) is a prototyping tool for building test scripts. It is a Firefox plugin and provides an easy-to-use interface for developing automated tests. Selenium IDE has a recording feature, which records user actions as they are performed and then exports them as a reusable script in one of many programming languages that can be later executed.

Selenium-Grid

Selenium-Grid allows the Selenium RC solution to scale for large test suites and for test suites that must be run in multiple environments. Selenium Grid allows you to run your tests in parallel, that is, different tests can be run at the same time on different remote machines.

An example using Selenium 2, Java and JUnit4

Download Selenium

Create a Java Project

  • Create a Java project using your favourite IDE – I’m using Netbeans 6.9.1, JDK 1.6 and JUnit 4
  • Add selenium-server-standalone-2.0b3.jar to your project classpath.

Create a Unit Test

public class GoogleTest {

    @Test
    public void test() throws Exception {

        WebDriver driver = new FirefoxDriver();

        driver.get("http://www.google.com");
        Assert.assertEquals("Google", driver.getTitle());

        WebElement element = driver.findElement(By.name("q"));
        element.sendKeys("Wikipedia");
        element.submit();
        Assert.assertEquals("Google", driver.getTitle());

        driver.quit();
    }
}
  • Here I am using Java to test a website written in PHP – the website language is actually irrelevant as it is basically html
  • There are of course benefits to having the selenium tests as part of the actual project (test driven development – TDD)
  • It’s comforting to use the visual FirefoxDriver(), but it is slower. For faster performance, use the HtmlUnitDriver() for in-memory testing
  • It really is this easy – try it 🙂

References

Selenium Website

Selenium Documentation

Powered by Zoundry Raven

Technorati : , , ,
Del.icio.us : , , ,
Zooomr : , , ,
Flickr : , , ,

Share and Enjoy:
  • Facebook
  • Twitter
  • Digg
  • StumbleUpon
  • del.icio.us
  • Yahoo! Buzz
  • Google Bookmarks
  • LinkedIn
  • MySpace
  • laaik.it
  • Google Buzz
  • Technorati
  • Slashdot
  • Reddit

2 Responses

  1. Dave@family history Dave@family history Says:

    Hi great article thanks for sharing. Have you had a chance to look at Selenium?(Specifically, we are using it with Java)what are your thoughts?

  2. batman batman Says:

    i have started using it on a production system – and am loving it.  to play catchup to 'start seleniuminizing' a project takes quite an investment in time, but as i develop new functionality i find myself developing the code and the test in parallel (TDD?).

    also – there's nothing like the feeling i get when – after changing a core piece of code that i just KNOW will affect almost everything – that i can just click a button and 'run the entire application' – either 'visually', or 'fast'.

    i haven't been able to use the Firefox plugun SeleniumIDE (it appears to only support pre-selenium2) – if anyone has figured it out please don't hesitate to contact me.

    in short – a big thumbs up – for me its a no-brainer!

Leave a Comment




XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.