Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » Howto » Downloads
 
Add your own
alternative version

Data Driven Web Testing With Visual Studio 2008 Team System

, 18 Dec 2007
How to create a data driven web test using Visual Studio 2008 Team System
MembershipExample_src.zip
MembershipExample
LocalTestRun.testrunconfig
MembershipExample.vsmdi
MembershipExample
App_Data
ASPNETDB.MDF
aspnetdb_log.LDF
bin
MembershipExample.dll
MembershipExample.pdb
MembershipExample.csproj.user
Properties
WebTestsProject
bin
Debug
LoginUser1.webtest
WebTestsProject.dll
WebTestsProject.pdb
LoginUser1.webtest
MyUsers.csv
Properties
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:2.0.50727.1433
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace WebTestsProject
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.VisualStudio.TestTools.WebTesting;
    using Microsoft.VisualStudio.TestTools.WebTesting.Rules;


    [DataSource("DataSource1", "System.Data.SqlClient", "Data Source=.\\SQLEXPRESS;AttachDbFilename=\"C:\\Documents and Settings\\mgousset\\My " +
        "Documents\\Visual Studio 2008\\Projects\\SampleWebApps\\MembershipExample\\Membership" +
        "Example\\App_Data\\ASPNETDB.MDF\";Integrated Security=True;User Instance=True", Microsoft.VisualStudio.TestTools.WebTesting.DataBindingAccessMethod.Unique, "UserTestData")]
    [DataBinding("DataSource1", "UserTestData", "Password", "DataSource1.UserTestData.Password")]
    [DataBinding("DataSource1", "UserTestData", "UserId", "DataSource1.UserTestData.UserId")]
    public class LoginUser1Coded : WebTest
    {

        public LoginUser1Coded()
        {
            this.Context.Add("WebServer1", "http://localhost:2062");
            this.PreAuthenticate = true;
        }

        public override IEnumerator<WebTestRequest> GetRequestEnumerator()
        {
            // Initialize validation rules that apply to all requests in the WebTest
            if ((this.Context.ValidationLevel >= Microsoft.VisualStudio.TestTools.WebTesting.ValidationLevel.Low))
            {
                ValidateResponseUrl validationRule1 = new ValidateResponseUrl();
                this.ValidateResponse += new EventHandler<ValidationEventArgs>(validationRule1.Validate);
            }

            WebTestRequest request1 = new WebTestRequest((this.Context["WebServer1"].ToString() + "/Default.aspx"));
            request1.ThinkTime = 18;
            ExtractHiddenFields extractionRule1 = new ExtractHiddenFields();
            extractionRule1.Required = true;
            extractionRule1.HtmlDecode = true;
            extractionRule1.ContextParameterName = "1";
            request1.ExtractValues += new EventHandler<ExtractionEventArgs>(extractionRule1.Extract);
            yield return request1;
            request1 = null;

            // Went to the default page
            this.AddCommentToResult("Went to the default page");

            WebTestRequest request2 = new WebTestRequest((this.Context["WebServer1"].ToString() + "/Default.aspx"));
            request2.ThinkTime = 25;
            request2.Method = "POST";
            request2.ExpectedResponseUrl = (this.Context["WebServer1"].ToString() + "/login.aspx?ReturnUrl=%2fDefault.aspx");
            FormPostHttpBody request2Body = new FormPostHttpBody();
            request2Body.FormPostParameters.Add("__EVENTTARGET", "LoginStatus1$ctl02");
            request2Body.FormPostParameters.Add("__EVENTARGUMENT", this.Context["$HIDDEN1.__EVENTARGUMENT"].ToString());
            request2Body.FormPostParameters.Add("__VIEWSTATE", this.Context["$HIDDEN1.__VIEWSTATE"].ToString());
            request2Body.FormPostParameters.Add("__EVENTVALIDATION", this.Context["$HIDDEN1.__EVENTVALIDATION"].ToString());
            request2.Body = request2Body;
            ExtractHiddenFields extractionRule2 = new ExtractHiddenFields();
            extractionRule2.Required = true;
            extractionRule2.HtmlDecode = true;
            extractionRule2.ContextParameterName = "1";
            request2.ExtractValues += new EventHandler<ExtractionEventArgs>(extractionRule2.Extract);
            yield return request2;
            request2 = null;

            // Clicked the Login Link
            this.AddCommentToResult("Clicked the Login Link");

            WebTestRequest request3 = new WebTestRequest((this.Context["WebServer1"].ToString() + "/login.aspx"));
            request3.ThinkTime = 12;
            request3.Method = "POST";
            request3.ExpectedResponseUrl = (this.Context["WebServer1"].ToString() + "/Default.aspx");
            request3.QueryStringParameters.Add("ReturnUrl", "%2fDefault.aspx", false, false);
            FormPostHttpBody request3Body = new FormPostHttpBody();
            request3Body.FormPostParameters.Add("__EVENTTARGET", this.Context["$HIDDEN1.__EVENTTARGET"].ToString());
            request3Body.FormPostParameters.Add("__EVENTARGUMENT", this.Context["$HIDDEN1.__EVENTARGUMENT"].ToString());
            request3Body.FormPostParameters.Add("__VIEWSTATE", this.Context["$HIDDEN1.__VIEWSTATE"].ToString());
            request3Body.FormPostParameters.Add("Login1$UserName", this.Context["DataSource1.UserTestData.UserId"].ToString());
            request3Body.FormPostParameters.Add("Login1$Password", this.Context["DataSource1.UserTestData.Password"].ToString());
            request3Body.FormPostParameters.Add("Login1$LoginButton", "Log In");
            request3Body.FormPostParameters.Add("__EVENTVALIDATION", this.Context["$HIDDEN1.__EVENTVALIDATION"].ToString());
            request3.Body = request3Body;
            if ((this.Context.ValidationLevel >= Microsoft.VisualStudio.TestTools.WebTesting.ValidationLevel.High))
            {
                ValidationRuleFindText validationRule2 = new ValidationRuleFindText();
                //validationRule2.FindText = "Welcome to our website User3!";
                validationRule2.FindText = "Welcome to our website " + this.Context["DataSource1.UserTestData.UserId"].ToString() + "!";
                validationRule2.IgnoreCase = false;
                validationRule2.UseRegularExpression = false;
                validationRule2.PassIfTextFound = true;
                request3.ValidateResponse += new EventHandler<ValidationEventArgs>(validationRule2.Validate);
            }
            yield return request3;
            request3 = null;

            // Signed in as User1
            this.AddCommentToResult("Signed in as User1");

            WebTestRequest request4 = new WebTestRequest((this.Context["WebServer1"].ToString() + "/Default.aspx"));
            yield return request4;
            request4 = null;

            // Clicked the Logout link
            this.AddCommentToResult("Clicked the Logout link");
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Mickey Gousset

Unknown
No Biography provided

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 18 Dec 2007
Article Copyright 2007 by Mickey Gousset
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid