Thursday, March 15, 2012

Selenium 2 / JUnit 4 Test Case Example

I've had trouble finding one single comprehensive example of a Selenium 2.2 / JUnit 4.1 test case in the web. I've pieced together info from lots of different places to form such an example.

You will need the jar files from selenium-java-2.20.0.zip and junit-4.10.jar in order to run the example.


import java.util.NoSuchElementException;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;


public class SeleniumTestCase
{
private static final int TIMEOUT_IN_SECONDS = 30;
private WebDriver driver;


@Before
public void startWebDriver()
{
driver = new FirefoxDriver();
}


@Test
public void test()
{
// Open home page
driver.get("http://adambuckley.net");
Assert.assertTrue(isTextPresent(driver, "Adam Buckley"));

// Click link
driver.findElement(By.linkText("Academic Research")).click();

/*
* Wait for the page to finish downloading. In this simple case, this
* line isn't necessary because the WebDriver will block until the page
* has finished downloading. However, in other cases, the WebDriver
* won't block and it's up to the calling code to verify that the new
* page has been loaded.
*/
waitForElementPresent(driver, By.linkText("Distributed Multimedia Sequencing Using Java & CORBA"));

Assert.assertTrue(isTextPresent(driver, "Distributed Multimedia Sequencing Using Java & CORBA"));
}


@After
public void closeSession()
{
driver.close();
}


/**
* Waits until the specified element is present in the web page. This method
* is used in situations where Selenium does not block until a page is
* completely downloaded.
* <p>
* From the <a href="http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebElement.html#click%28%29">Selenium documentation</a>:
* If click() causes a new page to be loaded via an event or is done by
* sending a native event (which is a common case on Firefox, IE on Windows)
* then the method will *not* wait for it to be loaded and the caller should
* verify that a new page has been loaded.
*
* @param driver the active WebDriver
* @param by the element locator
* @throws org.openqa.selenium.TimeoutException if the element is not
* present within the page, or if the page does not download within
* TIMEOUT_IN_SECONDS
*/
private static void waitForElementPresent(final WebDriver driver, final By by)
{
new WebDriverWait(driver, TIMEOUT_IN_SECONDS).until(new ExpectedCondition()
{
@Override
public WebElement apply(final WebDriver d)
{
return d.findElement(by);
}
});
}


/**
* Searches for a String. This method is a substitute for the Selenium 1
* method 'verifyTextPresent'
*
* @param driver the active WebDriver
* @param searchStr a String to locate in the downloaded page
* @return true if searchStr was found in the downloaded page, false
* otherwise
*/
private static boolean isTextPresent(final WebDriver driver, final String searchStr)
{
try
{
driver.findElement(By.xpath("//*[contains(.,'" + searchStr + "')]"));
return true;
}
catch(final NoSuchElementException e)
{
return false;
}
}
}

No comments:

Post a Comment