Search code examples
visual-studiowebtest

How do I proccess a string of numbers as string in a VS webtest datasource?


I have a problem. I am using a Visual studio web performance test and I have a csv file with the data that I need to send in a string body of a web request. The issue is that when the web test retrieve the data from accountID it takes the data as int instead of a string. So if the account number is 000005 or 000016 the test put a 5 and a 15 ignoring the zeroes on the left. That give me an error on my web service.

Is there a way to force the web test to see all the data as strings? Thanks

Below you can see an example of the csv file. The data is not a lot and is only 2 columns so I don’t want to create a database for that

AccountsNames, AccountID
Nombre1, 00001
Nombre3, 00002
Nombre4, 00003
Nombre5, 00004
Nombre6, 00005
Nombre7, 00006
Nombre8, 00007

Solution

  • What I end up doing is creating a Web Request plug in that gets the data source from the webtest context and editing the value.

    Here is a code example:

    namespace WebTestProjectToPlayArround
    {
        [DisplayName("Add Padding Zeroes")]
        [Description("")]
        public class EditContextParameter : WebTestRequestPlugin
        {
    
    
            [DisplayName("DataSourceContext")]
            [Description("")]
            public string DataSourceContextParam { set; get; }
    
            [DisplayName("ContextParamToStoreValue")]
            [Description("")]
            public string ContextParam { set; get; }
    
    
            public override void PreRequest(object sender, PreRequestEventArgs e)
            {
                base.PreRequest(sender, e);
                var value = (string) e.WebTest.Context[DataSourceContextParam];
                string newValue = "";
                object contextParam;
                string contextName = value.Replace("{","").Replace("}", "");
                if (e.WebTest.Context.TryGetValue(contextName, out contextParam))
                {
                    newValue = contextParam.ToString();
                    newValue = newValue.PadLeft(10, '0');
                }
                e.WebTest.Context[ContextParam] = newValue;
            }
        }
    

    }