Java Reference
In-Depth Information
assertTextPresent
. HtmlUnit itself uses
WebAssert
notNull
extensively to guard
against null parameters. Make sure to check the
WebAssert
class before you write
code that may duplicate its functionality.
If a method you need is absent, you should consider creating your own assert class
for additional
HTML
assertions. You should also consider creating an application-
specific assertion class to reuse across your unit tests. Remember, code duplication is
the enemy.
12.3.2
Testing for a specific web browser
HtmlUnit, as of version 2.7, supports the browsers shown in table 12.1.
Table 12.1
HtmlUnit-supported browsers
Web browser and version
HtmlUnit
BrowserVersion
constant
Firefox 2 (deprecated)
BrowserVersion.FIREFOX_2
Firefox 3
BrowserVersion.FIREFOX_3
Internet Explorer 6
BrowserVersion.INTERNET_EXPLORER_6
Internet Explorer 7
BrowserVersion.INTERNET_EXPLORER_7
Internet Explorer 8
BrowserVersion.INTERNET_EXPLORER_8
By default,
WebClient
emulates Internet Explorer 7. In order to specify which browser
to emulate, you provide the
WebClient
constructor with a
BrowserVersion
. For exam-
ple, for Firefox 3 use
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3);
12.3.3
Testing more than one web browser
You'll probably want to test your application with the most common version of
Internet Explorer and Firefox. For our purposes, we define our test matrix to be all
HtmlUnit-supported web browsers.
Listing 12.2 uses the
JU
nit
Parameterized
feature to drive the same test with all
browsers in our text matrix.
Listing 12.2
Testing for all HtmlUnit-supported browsers
[...]
@RunWith(value = Parameterized.class)
public class
JavadocPageAllBrowserTest {
B
C
private
BrowserVersion browserVersion;
@Parameters
public static
Collection<BrowserVersion[]> getBrowserVersions() {
return
Arrays.asList(new BrowserVersion[][]{
{BrowserVersion.FIREFOX_2},
D