Java Reference
In-Depth Information
12.6
Generating Selenium tests
The Selenium
IDE
is a great way to get up and running fast. Before you record a test,
edit the package name and class name in the
IDE
Source pane to match the directory
and Java filename you desire. Note that the generated code is
JU
nit 3 code, and as
such it subclasses the Selenium class
SeleneseTestCase
.
12.6.1
A live example
The same user interaction as in our first HtmlUnit test generated the following exam-
ple. Go to Google, enter a query, and click to go to the expected site. Listing 12.13
shows our first Selenium example.
Listing 12.13
Our first Selenium example
[...]
public class
FirstTestJUnit3
extends
SeleneseTestCase {
@Override
public void
setUp()
throws
Exception {
setUp("
http://www.google.com/", "
*iexplore");
}
B
public void
testSearch()
throws
Exception {
selenium.open("/");
assertEquals("Google", selenium.getTitle());
selenium.type("q", "Manning Publishing Co.");
selenium.click("btnG");
selenium.waitForPageToLoad("30000");
assertEquals("Manning Publishing Co. - Google Search",
selenium.getTitle());
selenium.click("link=Manning Publications Co.");
selenium.waitForPageToLoad("30000");
assertEquals("Manning Publications Co.", selenium.getTitle());
}
}
First, the
setUp
method
b
calls the superclass's
setUp
method with the base
URL
for
the tests and the browser launcher to use, in this case, Internet Explorer (see sec-
tion 12.8 1, “Testing for a specific web browser” for other settings). This initializes
the
selenium
instance variable to a
DefaultSelenium
instance.
In the test method, we start by opening the home page
C
. Next, we set the value of
the input field
D
, as if a user had typed it in, and we click the Search button
E
. The
click
argument is a Selenium locator, which here is the button name (more on the
locator concept later). We wait for the new page to load and assert the opened page's
title. Then, we click a link
F
using a Selenium link locator. Again, we wait for the new
page to load and assert the opened page's title.
Selenium tests subclass
SeleneseTestCase
, which in turn subclasses
JU
nit's
Test-
Case
class. You'll note that methods aren't annotated; Selenium-generated tests are
JU
nit 3 tests.
C
D
E
F