Convert Comments entered in Web Test Recorder into Transactions

This post is going to be one more sample of a Web Test Recorder Plug-in.  The recorder plug-ins are a new feature in VS 2010.  In this sample, we will convert comments that are entered in the recorder into transactions.  I have heard this request from a number of people and thought it would be a useful plug-in.    Here are 2 previous recorder plug-in posts: Correlation plugin  and Plug-in which adds Validation rule to test

 

Follow these steps to create plug-in which converts comments to transactions.

  1. Create a new class library project

  2. Right Click references node and select “Add reference…”

  3. Click the .NET tab and add the Microsoft.Visual Studio.QualityTools.WebTestFramework assembly

  4. Copy the code below and add it to your class file 

     using System.ComponentModel;
    using Microsoft.VisualStudio.TestTools.WebTesting;
    using Microsoft.VisualStudio.TestTools.WebTesting.Rules;
    
    namespace SampleRecorderPlugin
    {
    
        public class CovertRecordedCommentsToTransactions : WebTestRecorderPlugin
        {
            public override void PostWebTestRecording(object sender, PostWebTestRecordingEventArgs e)
            {
                for (int i = 0; i < e.RecordedWebTest.Items.Count; i++)
                {
                    //get the item and see if it is a comment
                    WebTestItem item = e.RecordedWebTest.Items[i];
                    Comment c = item as Comment;
                    if (c!=null)
                    {
                        //if it is a comment create a transaction
                        TransactionTimer tt = new TransactionTimer();
    
                        //set name of transaction to the comment text
                        tt.Name = c.CommentText;
    
                        //now add all items up until next comment
                        int j = i+1;
                        while (j < e.RecordedWebTest.Items.Count)
                        {
                            WebTestItem nextItem = e.RecordedWebTest.Items[j];                        
    
                            //if the item is not a comment move it into the transaction
                            if (!(nextItem is Comment))
                            {
                                tt.Items.Add(nextItem);
                                e.RecordedWebTest.Items.RemoveAt(j);
                            }
                            else
                            {
                                break;
                            }
                        }
    
                        //add the transaction to the web test
                        e.RecordedWebTest.Items.Insert(j, tt);
    
                        //remove the comment from the web test
                        e.RecordedWebTest.Items.RemoveAt(i);
    
                        //indicate that the web test has been modified
                        e.RecordedWebTestModified = true;
    
                    }
                }
    
            }
        }
    }
    
    
    
  5. Compile the project

  6. Copy the dll to \Program Files\Microsoft Visual Studio 10.0\Common \IDE\PrivateAssemblies\WebTestPlugins

  7. Restart VS and go to your test project.

  8. Add a new Web Performance Test. You will see the following dialog:

  9. Select the CovertRecordedCommentsToTransactions  plug-in.

  10. In your web test add some comments while you are recording.  Here is what test looks like in the recorder.  You can see the Browse, Add To Cart, and Checkout comments

  11. Then hit stop on the recorder

  12. You can see that back in the Web Test Editor these comments have been converted to transactions and the requests have been moved under the transactions.  I have the plug-in removing the comment once it creates the transaction but you can easily change that.

     

 

Let walk through the code.  First I am looping through the web test looking for comments: 

             for (int i = 0; i < e.RecordedWebTest.Items.Count; i++)
            {
                //get the item and see if it is a comment
                WebTestItem item = e.RecordedWebTest.Items[i];
                Comment c = item as Comment;
                if (c!=null)
                {

Once I find a comment I am creating a transaction.

                     //if it is a comment create a transaction
                    TransactionTimer tt = new TransactionTimer();

                    //set name of transaction to the comment text
                    tt.Name = c.CommentText;

Next it will start adding requests into the transaction. It will add all requests up until the next comment or the end of the test.  Once it adds it to the transaction it removes it from the outer test. 

                     //now add all items up until next comment
                    int j = i+1;
                    while (j < e.RecordedWebTest.Items.Count)
                    {
                        WebTestItem nextItem = e.RecordedWebTest.Items[j];                        

                        //if the item is not a comment move it into the transaction
                        if (!(nextItem is Comment))
                        {
                            tt.Items.Add(nextItem);
                            e.RecordedWebTest.Items.RemoveAt(j);
                        }
                        else
                        {
                            break;
                        }
                    }

Finally add the transaction to the web test and remove the comment.  Then set the event args RecordedWebTestModified to true. 

                     //add the transaction to the web test
                    e.RecordedWebTest.Items.Insert(j, tt);

                    //remove the comment from the web test
                    e.RecordedWebTest.Items.RemoveAt(i);

                    //indicate that the web test has been modified
                    e.RecordedWebTestModified = true;
  
 I hope this shows another use of the recorder plug-ins.  Let me know if you have any questions on this feature.