Java Reference
In-Depth Information
SeleneseTestCase.assertEquals("Manning Publishing Co. - Google
Search", this.selenium.getTitle());
this
.selenium.click("link=Manning Publications Co.");
this
.selenium.waitForPageToLoad("30000");
SeleneseTestCase.assertEquals("Manning Publications Co.",
this.selenium.getTitle());
}
}
Let's examine this more complex setup. The test class is annotated with
@Run-
With(value
=
Parameterized.class)
b
, which directs
JU
nit to run the test class as
many times as there are values returned from our
@Parameters
method
getBrowsers
D
. By contract with
JU
nit, this method must return a
Collection
of arrays; in our
case, we return a list of browser launch strings, one for each browser we want to test.
JU
nit will run all test methods with the test class initialized with
"*iexplore"
and
then do it all over again with
"*firefox"
. You'll need to have both browsers installed
on your machine for this to work.
Let's walk through this
JU
nit subtlety more carefully. When running the test class,
JU
nit creates test class instances for the cross product of the test methods and the test
collection elements. One instance of the class is created for
"*iexplore"
and for a sin-
gle
@Test
method in the class.
JU
nit runs that
@Test
method and repeats the process
for all
@Test
methods in the class.
JU
nit then repeats that whole process with
"*fire-
fox"
and so on for all elements in the
@Parameters
collection
D
.
We no longer have a
@BeforeClass
method; instead we use a
@Before
method
I
to initialize the
selenium
instance variable
H
for each test method. The
selenium
instance variable gets its value from a lazy-initialized static variable
C
. This can work
only by using a
@Before
method and lazy initializing our client driver. Remember, we
want our test class to reuse the same driver instance for each test method in a given
parameterized run.
We have an
@AfterClass
method
G
to clean up the driver at the end of the class
run. Even though we use a static
Map
C
to save our driver across test runs, there's only
one driver in the map at any given time. The
getSelenium
method
E
can safely stop
F
the current driver when creating a new driver because we know that
JU
nit finished
one of its parameterized runs.
Now that you know how to run tests efficiently for a browser suite, let's survey the
API
used to navigate an application.
12.8.3
Application and internet navigation
Unlike HtmlUnit, there's no Selenium
HTML
object model to navigate; instead, you
call the
com.thoughtworks.selenium.Selenium
interface, using a locator string to
address elements (see section 12.8.4, “Accessing elements with references”). This
interface contains more than 140 methods and provides all of the services and setting
toggles needed to write tests. Although there's no object model per se, the
API
pro-
vides some methods to work with certain types of elements. For example,
getAll-
Fields
returns the
ID
s of all input fields on a page.