Getting started with Selenium 2.0 (WebDriver)

Want to learn more about WebDriver? What do you want to know?

With the release of Selenium 2.0 the best name in web testing and the best API have come together in a match surely made in heaven. In this, the first article in a series, we’ll look at what it takes to start writing automated browser tests using Selenium 2.0.

What Is It?

Selenium is a browser-based testing tool. With it you can write tests that drive the browser – clicking on links, filling in forms – just like your users do; so you can test your application works correctly when used the way your users do. If you’re not doing browser-based end-to-end testing, how do you know your application works when your users actually use it?

What Do I Do?

  1. Download selenium or configure a maven dependency
  2. Write one or more tests
  3. Run them as part of your continuous integration
  4. Profit!

I’ll assume you know how to do (1) so we’ll start with (2) in this article. If you’re interested in (3), why not subscribe because we’ll cover that in future. (4) is an advanced topic and is left as an exercise for the reader.

All the examples below are on github – including an example maven pom if you’re so inclined.

Should I Use Selenium IDE?

NO. Unfortunately Selenium IDE isolates you from the mechanics of how your test is implemented. While this sounds like a good thing, it tends to lead to hard-to-maintain tests and a world of hurt. Instead, write your tests using your favourite testing framework – I’m working in Java, so the tests I’ll show below are JUnit tests.

My First Test

Let’s start with a really simple test, we’ll search Amazon for one of my favourite authors.

First, we need a driver – this is what actually interacts with a running browser – we’ll use the Firefox driver, you could instead use Chrome or Internet Explorer, if that’s what floats your boat.

FirefoxDriver driver = new FirefoxDriver();

This fires up a browser, ready to be controlled. Next we need to visit a page – so let’s go to the Amazon homepage.

driver.get("http://www.amazon.co.uk/");

Now we’re on the Amazon homepage, let’s enter in a search term.

WebElement keywordsField =
    driver.findElement(By.name("field-keywords"));
keywordsField.sendKeys("iain banks");

Wait a minute – what happened there? The first two lines tell the driver to find an element on the page. We’re identifying it by name, e.g. this would find something that looks like:

<input name="field-keywords" ... />

Once we’ve found that element on the page – we send it some keys. This simulates us typing in the search term into the relevant field.

But that’s not quite enough, we also need to execute the search. So let’s click the search button:

WebElement goButton =
    driver.findElement(By.cssSelector("#navGoButton input"));
goButton.click();

As before, we find an element – but this time we use a CSS selector. As the name suggests, this finds elements exactly the same way CSS selectors do. So this finds something that looks like:

    <div id="navGoButton">
        ...
          <input type="..." />
    </div>

Once we’ve found the element, we click on it – this loads the results page. Note: the call to click() will block until the page has finished loading, so whatever we do next will only happen once the page has loaded.

What do we do next? Let’s check that the top result is the book we expect it to be:

WebElement topResultTitle =
    driver.findElement(By.cssSelector("#result_0 .title a"));
assertThat(topResultTitle.getText(), is("Transition"));

Again we identify an element using a CSS selector, but this time we get the text of the element. This finds the relevant link and returns the text it contains – in this case, the title of the book.

If you run this test, you’ll see (racing by): the amazon home page open, the search term entered and the results page loaded.

Congratulations! We wrote our first Selenium test. Now let’s move on to part two: using page objects.

8 thoughts on “Getting started with Selenium 2.0 (WebDriver)

  1. Pingback: Getting started with Selenium 2.0

  2. As good as it gets

    Have you ever worked on a complex Web 2.0 form with multiple AJAX controls, grids, and the sort? Are you seriously suggesting that you would test all these without the use of a tool like Selenium IDE?

    I am growing increasingly tired of reading ‘me too’ types of articles like this which have little practical use in real applications.

    1. Actually it’s because of the work I’ve done on complex apps that means I wouldn’t recommend Selenium IDE. Do you not find your tests are too brittle? Do you not find that every minor change to your application causes all your tests to break?

      I’ll agree that building tests of complex functionality by hand can be time-consuming, but personally I prefer the control that writing these tests manually in JUnit brings. I find this even more valuable in handling complex asynchronous interactions where you need to be careful to avoid race conditions that make tests pass or fail incorrectly.

      1. As good as it gets

        In our team we have a clear separation between testers and developers and testers are not competent in development, let alone in Java programming. We even had to explain to them how (and why) to use CSS selectors in Selenium!

        And I think that in general, you can’t expect from testers to write complex jUnit code which requires multi-disciplinary experience (back-end, front-end). Our approach is to have a tester record most of the script in Selenium IDE, fine-tune it within Selenium IDE for AJAX events (WaitForVisible, WaitForNotVisible, etc.) and then have a developer fine-tune the resulting jUnit code if necessary.

        Maybe in smaller teams you can afford to have developers do all the testing as well.

      2. Ah ok, that would make sense. In my current company (you guessed it, a small team) the developers write acceptance (browser) tests. We don’t actually have a single QA engineer on the team!

        But I’ve also worked at other companies where QA were barely capable of pushing the right buttons, never mind doing something as dangerous as recording a selenium script🙂

        Interesting approach of having QA create the first pass and developers fine tune as necessary. I’d still be scared that the code Selenium IDE generates is pretty horrific to work with – but it certainly saves developers from having to spend time writing tests.

        However having seen it work well, I’m now convinced that (if the business can be persuaded) having developers write browser tests is infinitely superior to getting QA doing automated testing. A good QA engineer’s time is best spent in exploratory testing, not trying to automate tests and definitely not trying to write complex, multi-disciplinary test code.

        Thanks for your comments though. I’m hoping to write more in this series and get into more real world examples – this was really just a very quick example for a developer who’s never used Selenium before. Perhaps this mixed use of Selenium IDE with hand-tuning is something I need to explore in a bit more detail.

  3. sreeram

    nice introduction to the selenium 2.0.Can we have a little complex example,which makes use of other available classes apart from WebElement(s) ?

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