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;

	@BeforeClass
	static public void automateTestSetup() {
		selenium = startSeleniumAndSearch();
	}

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

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

	@Test
	public void checkPageTitle() {
		String pageTitle = selenium.getTitle();
		assertTrue("Search term not found in page title: " + pageTitle,
				pageTitle.contains("Selenium-RC"));
	}

	@Test
	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,
				searchTerm.equals("Selenium-RC"));
	}

	@AfterClass
	static public void closeAndStopServer() {
		selenium.close();
		selenium.stop();
	}

	static private Selenium startSeleniumAndSearch() {
		Selenium selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://www.compendiumdev.co.uk/");
		selenium.start();
		selenium.open("/selenium/search.php");
		selenium.type("xpath=//input[@name='q']", "Selenium-RC");
		// When we use an XPath starting with '//', we can opt to leave out 'xpath='
		selenium.click("xpath=//input[@name='btnG' and @type='submit']");
		selenium.waitForPageToLoad("30000");
		return selenium;
	}

}
Advertisements

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