UKVSTS Web Test Plug In for VS 2010 - Setting Context Parameter with C# statement

Locked UKVSTS Web Test Plug In for VS 2010 - Setting Context Parameter with C# statement

  • Friday, August 17, 2012 1:29 PM
     
     

    I've been to the UKVSTS page and tried following the directions but I do not see the references mentioned when I try to do an Add Reference as shown in Step 2. I'm on a Win 7 box using VS 2010 Ultimate.

    All I want to do is set a Context Parameter to the current DateTime + 2 days as the example shows on the UKVSTS. I am aware that I can likely do this in a Coded test but would prefer to do this like the example shown on the UKVSTS page (i.e. RequestDeliveryDate=<%=DateTime.Now.AddDays(2)%>.

    I've downloaded and installed the MSI. The second step refers to selecting the proper DLL and only mentions VS2005 and VS2008. Do I need an updated MSI to do this or is this functionality already built into VS2010? What am I missing?

     

All Replies

  • Friday, August 17, 2012 2:01 PM
     
     Answered

    You didn't put any links into your post, but if you want to do what I think you want to do (pardon the pun), you can use a web test plugin and accomplsih that. I did the reverse and retrieved the content of a context parameter in code within a WebTest RequestPlugin.

    Thanks!
    Ken

  • Friday, August 17, 2012 2:54 PM
     
     Answered Has Code

    Hi RavDodier,

    Here is a sample plugin that you can use to do what you want.

    Add a class to your test project, in that class add the WebTestPlugin class code below:

    Remember to add your using's to the class file:

    using System;
    using Microsoft.VisualStudio.TestTools.WebTesting;
    using System.ComponentModel;

        [Description("WebTestPlugin that adds a specified number of days to the current date and stores the new date in a Context Parameter that you specify. Overrides PreWebTest")]
        public class AddDaysToCurrentDate : WebTestPlugin
        {
            public AddDaysToCurrentDate()
            {
                DaysToAdd = 1;
                ContextParameterName = "FutureDate";
            }
            public override void PreWebTest(object sender, PreWebTestEventArgs e)
            {
                //TODO: Adjust the date format to obtain output needed. Search C# DateTime.Now Formats for examples and msdn documentation
                //Example: FutureDate=Saturday, August 18, 10:49:21, 2012	
                //e.WebTest.Context[ContextParameterName] = DateTime.Now.AddDays(DaysToAdd).ToString("dddd, MMMM dd, HH:mm:ss, yyyy");
                e.WebTest.Context[ContextParameterName] = DateTime.Now.AddDays(DaysToAdd).ToString();
            }
            [Description("Number of days to add to the current date. Default value is 1.")]
            [DefaultValue(1)]
            public double DaysToAdd { get; set; }
            [Description("Context Parameter that will hold the new date value. Default value is 'FutureDate'.")]
            [DefaultValue("FutureDate")]
            public string ContextParameterName { get; set; }
        }


    http://blogs.msdn.com/rogeorge


  • Friday, August 17, 2012 5:56 PM
     
     

    The UKVSTS page made it sound like it allowed you to essentially define a Context Parameter value with the output from any valid C# statement.

    Though I appreciate the reply, a generic way to define the value of a Context Parameter with the output of any C# statement would be desirable. Am I reading more into the UKVSTS functionality than actually exists?

    FYI - This is the UKVSTS page I referenced -

    http://ukvsts.codeplex.com/wikipage?title=UKVSTS%20Web%20Test%20Plug%20In&ProjectName=ukvsts

  • Friday, August 17, 2012 8:30 PM
     
     

    Ask your question over there at UKVSTS for specifics on that plugin.

    solution I replied with is the  common pattern for extending your tests in VS, once you understand the plugin model you can get much more out of your tests.


    http://blogs.msdn.com/rogeorge

  • Friday, August 17, 2012 9:43 PM
     
     

    Thanks rogeorge. Yes, I do need to get to understand the Web Plugin model better.

    I did ask the question on the UKVSTS page but it is a very old thread and so far I've gotten no response. Was hoping someone here might know.

    If I do get an answer and get this working I will cross post it here as this seems like a very elegant solution versus creating a number of single purpose web plugins.

  • Monday, August 20, 2012 2:42 AM
    Moderator
     
     

    Hi RayDodier,

    Thank you for posting in the MSDN forum.

    Based on your description, I downloaded and installed the MSI. But when I referred to selecting the proper DLL, I either don’t  see the references mentioned.

    In addition, if you want to set the context parameter by C# statement in Web Test Editor, I think that VS2010 should not support this. But you can do that in Coded Web Test. Also as Regeorge and Kabaker551111 suggested, a web test request plugin is a good choice for setting a context parameter to the current DateTime +2 days.

    Best regards,


    Amanda Zhu [MSFT]
    MSDN Community Support | Feedback to us

  • Tuesday, August 28, 2012 4:12 PM
     
     

    So I did a web test plugin and combined the AddDaysToDate and RandomNumber examples floating around into one project. One thing that wasn't really clear was how to use the Context value in a Web test.

    Unlike Context Parameters, I could not find a way using the VS 2010 GUI to select and associate (bind) the Context Value of say "RandowNumber" to a value in a Form Post field. In other words, there is no drop-down when I click on a form post parameter Value that allows me to select the name of the Context value I set in the WebTestPlugin.

    I finally bound it manually by just wrapping the Context name in double curly braces. For example, I clicked on a form post parameter like -

    tw#local#holdExpirationDate

    ...and in the Properties for this I typed in a Value of {{FutureDate}} to use the output of the AddDaysToDate method in my WebTestPlugin.

    Figured I would post this for any other WebTestPlugin newbies that stumbled on this.

  • Friday, August 31, 2012 12:03 PM
     
     

    It appears that the source code for that plugin is available at the web page. You could download and compile it and add it to your project to try it out and see if it works.

    Thanks!
    Ken