VSTS 2008 Web Testing Part 3: Validation Rules
Visual Studio Team System 2008 (both the Testing edition and the Team Suite edition) have a type of test referred to as a Web test. These Web tests are used to test the functionality of Web applications, and are also designed to be used to load test Web applications. In Part 3 of this series on Web tests, we are going to look at how to add validation rules to a Web test. To catch up on current events, check out Part 1 VSTS08 Web Testing
and Part 2 Running a Web Test
What are Validation rules
In a Visual Studio Team System 2008 Web test, a validation rule is used to help verify that a Web application is performing correctly. It does this by checking for the existence of text, tags, or attributes on the page returned by the Web request. Validation rules are also used to verify the amount of time it takes a request to finish. Finally, validation rules can be used to verify the existence of form fields and their associated values.
Adding a validation rule to a Web test
Validation rules are added to a Web test using the Web Test Editor in Visual Studio 2008. Figure 1 displays the Web test we have been working with in Part 1 and Part 2 of this series.
[Click on image for larger view.]
For the Web site we are testing, we want to make sure that once you log into the site, that it displays the message "Welcome <username>", where <username> is the user name used to log into the site. To do this, we are going to add a validation rule to the Web request indicated by the red arrow in Figure 1.
Right-click on the Web request in the Web Test Editor, and from the context menu select "Add Validation Rule". This will open the Add Validation Rule window, as shown in Figure 2.
You have several validation rule options out of the box:
- Form Field - Verifies the existence of a form field with the specified name and value.
- Find Text - Verifies the existence of the specified text in the response.
- Maximum Request Time - Verifies that the request finishes within a specified amount of time.
- Required Attribute Value - Verifies the existence of a specified HTML tag that contains an attribute with a specified value.
- Required Tag - Verifies the existence of specified HTML tag in the response.
- Response URL - Validates that the response URL after redirects are followed is the same as the recorded response URL. QueryString parameters are ignored.
For this scenario, we want to make sure the required phrase is in the response from the Web server, so we have selected the Find Text validation rule. The right side of Figure 2 shows the properties for the Find Text validation rule:
- Level - This property is important when used in conjunction with load testing. The possible values are Low, Medium and High.
- Find Text - This is the text we are looking for in the response.
- Ignore Case - Determines whether case matters or not.
- Use Regular Expression - Allows you to use a regular expression in the Find Text field.
- Pass If Text Found - Determines whether the test passes or fails based on whether the text is found or not found.
For this scenario, we are looking for the words "Welcome Alok". We are not using a regular expression, and case does not matter. The test will pass if the words are found. Click the OK button to close the Add Validation Rule window. Figure 3 shows the Web test in the Web Test Editor, with the addition of the validation rule.
Running The Web Test
Now that we have added a validation rule to our Web test, it is time to run the Web test and view the results. In the Web Test Editor, click the Run Test button to run the Web test. Figure 4 shows the results of running the Web test.
By selecting the request to which we added the validation rule and then selecting the Details tab, we can see the results from the validation rule. For this test, our validation rule passed, indicating that it found the "Welcome Alok" phrase in the response object.
Making the Validation Rule Fail
Now let's intentionally make the validation rule fail, to show what happens when a Web test fails. Return to the Web test in the Web Test Editor and select the validation rule we created earlier. In the Properties window for the validation rule, change the Find Text field to be "Welcome Fred". Save the Web test and then run it again. This time, the test will fail, as shown in Figure 5.
Notice at the top of the test is a red X, indicating the test has failed. A red X also indicates which Web request failed. If we select the Web request and click the Details tab, we can see that the validation rule failed, because the text "Welcome Fred" did not appear in the HTML response object.
In this series of columns on Web testing, we have touched the surface on what you can do with a Web test. We have shown you how to create and run a Web test, as well as how to add validation rules to a test. For basic testing, this will get you started. Once you start delving into Web testing more, you will want to look at how to utilize extraction rules, and even how to create your own custom validation and extraction rules.
Mickey Gousset spends his days as a principal consultant for Infront Consulting Group. Gousset is lead author of "Professional Application Lifecycle Management with Visual Studio 2012" (Wrox, 2012) and frequents the speaker circuit singing the praises of ALM and DevOps. He also blogs at ALM Rocks!. Gousset is one of the original Team System/ALM MVPs and has held the award since 2005.