70-565 - Web Tests

Filed under: , , by:

The most common automated tests that developers write are of course Unit Tests, but Visual Studio offers more than Unit Testing. Unit tests are perfect for testing your business and data access layer, but not really good for testing presentation layer. Making sure that your business objects return valid results is one but making sure that your web application can correctly display the results, or authenticate a user, or process a request with other requests being processed at the same time, or redirect to other pages as required (ie. error page), etc. is actually what really should be tested because that's the layer of the application a user interacts with and by making sure that your business object can return a correct result a developer cannot guarantee that the application can correctly display that result. That's why there are Web Tests, which are part of Visual Studio 2008 Test Edition and Team Suite Edition. The benefits of using Web Tests are:
You can create Web tests for use in an extensive set of testing purposes:

  • Create functional tests that exercise your Web applications.
  • Create data-driven tests.
  • Create and run tests that can test the performance of your applications.
  • Use .NET languages for test authoring, debugging, and test extensibility.
Web tests automatically handle these aspects of HTTP:
  • Hidden field correlation including VIEWSTATE
  • Redirects
  • Dependent requests
  • Authentication
  • Security through HTTPS/SSL
Here is the example of a Web Test, which uses a simple Web Site with 3 text boxes and a button to sum entered numbers:
After having created your web site, you add a regular test project to which you can add a Web Test as shown below:
By creating a new Web test in Visual Studio, you record a new Web Test, which launches Internet Explorer and activates the integrated Web Test Recorder, which is used to record the actions you perform while you browse a Web site. As you move through the site, recorded requests are added to the Web test as shown below:
After you have recorded a Web test, you can change the test and add properties to the test by using the Web Test Editor. Below is shown available properties you can add to a recorded Web Test:
For example, you can add validation and extraction rules. Validation rules help verify that a Web application is working correctly by validating the existence of text, tags, or attributes on the page returned by a Web request. Validation rules can also verify the amount of time it takes a request to finish, and the existence of form fields and their values. On the other hand, extraction rules help verify that a Web application is working correctly by extracting data from the responses to Web requests and storing results in the test context as name value pairs, which can be used later for further custom validation.Below is shown a window with options available for extraction rule:

A Web Test can be converted to a coded Web test, which is a .NET class that generates a sequence of WebTestRequests. It can also be created manually, but it is suggested practice to convert a recorded Web test to a coded Web test. After a Web test has been converted from recorded to coded, you can edit that code like any other source code. Additionally, you can add looping and branching constructs, dynamically change the number of requests in the test, and dynamically generate the set of URLs that the test hits. Here is an example of the coded Web Test:
And finally, you can run the tests and verify that all rules have passed like shown below:
Not only do we get information whether the test have passed or failed, but also detailed information on request and response in a readable form for a developer to make sure the generated page is correct. Although Web tests are great for automated tests verifying that pages have been generated the way it is expected, but the real power is when they are combined with Load Tests to simulate a real user load on your Web site to test the functionality of Web application under load (performance and stress tests) before going live, but that topic will be covered in the next post.

0 comments: