Selenium Simplified Notes 2 of X

Looking at the code in the previous notes, we can notice that there is one test (annotated with @Test).  That single test does just about everything:

  • It starts an instance of Selenium
  • It does the actions needed to conduct the search
  • It tests for 4 separate things (i.e., whether the result contains a particular text, whether the result contains a particular URL, it checks the page title, and it checks the content of the Search Box field)
  • It closes the browser and stops Selenium

Chapter 12 of the book focuses on refactoring our initial code. End result is that:

  • Separate methods were defined for the set up and tear down activities
  • Each test now only contains code that is relevant to the actual test
  • Initially the @Before and @After annotations were used — These methods will be done before and after each test
  • @BeforeClass and @AfterClass annotations were introduced. But this required some fields and methods to be changed to static so that they can be used across the entire class
package com.eviltester.seleniumtutorials;

import com.thoughtworks.selenium.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

public class MySimpleSearchTests {

	static Selenium selenium;

	static public void automateTestSetup() {
		selenium = startSeleniumAndSearch();

	public void checkTextIsPresent() {
		assertTrue("That long text is not found",
				"Selenium Remote-Control Selenium RC comes in two parts." +
				" A server which automatically launches and kills browsers,"

	public void checkUrlsAreFound() {
		int matchingCountTotal = 0;
		matchingCountTotal += selenium.getXpathCount("//a[@href='']").intValue();
		matchingCountTotal += selenium.getXpathCount("//a[@href='']").intValue();
		// use starts-with instead
		matchingCountTotal += selenium.getXpathCount("//a[starts-with(@href, '')]").intValue();
		assertTrue("None of those URL were found", matchingCountTotal>0);

	public void checkPageTitle() {
		String pageTitle = selenium.getTitle();
		assertTrue("Search term not found in page title: " + pageTitle,

	public void checkSearchBox() {
		String searchTerm = selenium.getValue("xpath=//input[@name='q' and @title='Search']");
		assertTrue("Search term not found in search box, contains this instead: " + searchTerm,

	static public void closeAndStopServer() {

	static private Selenium startSeleniumAndSearch() {
		Selenium selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "");
		selenium.type("xpath=//input[@name='q']", "Selenium-RC");
		// When we use an XPath starting with '//', we can opt to leave out 'xpath='"xpath=//input[@name='btnG' and @type='submit']");
		return selenium;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your 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