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