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?
- Download selenium or configure a maven dependency
- Write one or more tests
- Run them as part of your continuous integration
- 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.
Image may be NSFW.
Clik here to view.
Clik here to view.
