DeclarativeWebTest & DeclarativeWebTestSerializer

In Orcas, all of the API needed to programmatically create Declarative WebTests (non-coded webtests that show up in the webtest editor UI) has been exposed. If you have a need to programmatically generate webtests you can now do this using the DeclarativeWebTest and DeclarativeWebTestSerializer classes. Perhaps something in your web application has changed that affects a large group of your existing Web Tests, rather than modify the tests by hand you could write some code to do this for you.

 

DeclarativeWebTestSerializer loads the contents of a .webtest file into an instance of the DeclarativeWebTest class and can also save an instance of the DeclarativeWebTest class back out to a .webtest file.

 

DeclarativeWebTest exposes all of the properties, requests, and rules of the loaded webtest so they can be manipulated in whatever way necessary and then resaved.

Here's an example of modifying an existing declarative webtest in a C# console application:

static void Main(string[] args)

{

    DeclarativeWebTest decWebTest = DeclarativeWebTestSerializer.Open(@"c:\test.webtest");

   

    //Add a Request to this WebTest

    WebTestRequest newRequest = new WebTestRequest("https://newRequest/default.aspx");

    decWebTest.Items.Add(newRequest);

    //Set ExpectedHttpStatus to 404 on the 1st Request

    WebTestRequest reqToModify = null;

    foreach (WebTestItem item in decWebTest.Items)

    {

        if (item is WebTestRequest)

        {

            reqToModify = item as WebTestRequest;

            break;

        }

    }

    if (reqToModify != null)

    {

        reqToModify.ExpectedHttpStatusCode = 404;

    }

    //Save Test

    DeclarativeWebTestSerializer.Save(decWebTest, @"c:\test.webtest");

}