Java Reference
In-Depth Information
public static void
stopSeleniumClient()
throws
Exception {
if
(selenium !=
null
) {
selenium.stop();
selenium =
null
;
}
}
public static void
stopSeleniumServer()
throws
Exception {
if
(seleniumServer !=
null
) {
seleniumServer.stop();
seleniumServer =
null
;
}
}
@AfterClass
public static void
tearDownOnce()
throws
Exception {
stopSeleniumClient();
stopSeleniumServer();
}
}
Let's examine this code. The test manages two static variables: a Selenium client driver
b
and a Selenium server
C
. The
@BeforeClass
method starts the Selenium server
and then the Selenium client
D
. The
@AfterClass
method stops the client and then
the server
E
. We can now run tests from
ManagedSeleniumServer
subclasses, as this
next example demonstrates.
This class doesn't subclass
SeleneseTestCase
to avoid inheriting its
setUp
and
tearDown
methods, which respectively start and stop a web browser. If you want to sub-
class
SeleneseTestCase
, make sure you override the
setUp
and
tearDown
methods to
do nothing. Let's look at an example of a managed test:
E
public class ManagedTestJUnit4v2 extends ManagedSeleniumServer {
@Test
public void testSearch() {
// test...
}
}
As a
ManagedSeleniumServer
subclass, this class needs only test methods.
JU
nit will
call the
@BeforeClass
methods declared in superclasses before those of the current
class and will call the
@AfterClass
methods in superclasses after those of the cur-
rent class.
If you aren't going to manage a Selenium server farm for different browsers and
operating systems, using this class as a superclass for tests offers a simple solution to
get you up and running managing the Selenium server within your tests and
VM
.
The drawback to this approach is that
JU
nit starts and stops the Selenium server
for each test class. To avoid this, you could create a test suite with first and last test
classes that start and stop the server, but you'll need to remember to do this for each
suite, and you'll also need to share the Selenium server through what amounts to a
global variable. We take care of this problem next.