In Listing 19-14, the class extends the SeleneseTestBase class, which provides many handy methods
to ease the implementation of a test case. In the setup() method, an instance of the WebDriver interface
is prepared, with the FirefoxDriver class, which will invoke the Firefox browser installed on the testing
machine for test case execution. Drivers also exist for Chrome, IE, HtmlUnit, and so on. If your web
application needs cross-browser support, you can run the same test again with different web drivers to
ensure the application behaves consistently across different browsers. Then, an instance of the
WebDriverBackedSelenium class is constructed, which is Selenium's support for using WebDriver API to
drive the browser interaction.
Next, take a look at the loginAs() and logout() methods, which will be reused by multiple test cases
for login and logout actions. For example, in the loginAs() method, it will open the home page (i.e.,
http://localhost:8080/ch19/contacts), populate the form fields j_username and j_password, and then
click the Login button. In the form, the elements should have the correct names assigned. For example,
take a look at the login form in the view file menu.jspx in Listing 19-15.
Listing 19-15. The Login Form
<form name="loginForm" action="${loginUrl}" method="post">
<table>
<caption align="left">Login:</caption>
<tr>
<td>User Name:</td>
<td><input type="text" name="j_username"/></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="j_password"/></td>
</tr>
<tr>
<td colspan="2" align="center"><input name="login" type="submit" value="Login"/>
</td>
</tr>
</table>
</form>
The form fields with names assigned are highlighted in bold. So, when the line
selenium.type("j_username", userName) is executed, the form field with the name j_username will be
populated with the supplied user name, and so on.
The testAddContact() method is the test case for a normal add contact operation. After login, the
new contact URL is called (using the open() method), and then the contact information is entered, and
the Save button is clicked. Afterward, the verifyTrue() method is called to verify that the add operation
completed successfully with the contact information present on the page.
The testAddContactWithEmptyForm() simulates the case that a user clicks the Save button without
entering any contact information. As a result, the save operation should fail, and the verifyTrue()
method will be called to verify that the error message is displayed in the page.
To run the test, just right-click the test class and run it as a JUnit test. Make sure that the web
application is deployed and the tc Server is up and running. Then you will see that the test cases will
invoke a copy of Firefox automatically, and the login, contact information input, and verification logic
will be executed accordingly.
From this simple example, you can see how Selenium can help automate the user interaction with
the web application frontend with cross-browser compatibility. For more details, please refer to
Selenium's online documentation (http://seleniumhq.org/docs).
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home