WebTest Plugin: Random Number

Do you need a random number generated in a WebTest, just take the code below and add it to a class(.cs) file in your project, recompile once.

Then just right click on your WebTest in the editor and choose Add WebTest Plugin picking this one and you should be good.

Just use the curly brace syntax where your using it in the test just like any context parameter (i.e. {{RandomNumber}} ). You can have it regenerate the random number before every request, or just have it do this only once in the PreWebTest by setting the UpdateBeforeRequest property. If you need more random numbers, you can add more than one instance of this plugin,just remember to set the ContextParameter name to something different .

In the code, if you add the appropriate using System.ComponentModel, then you don’t have to put the full syntax as is listed below for the attributes [System.ComponentModel.xxx] you can use [Description(“Blah”)], etc.

 

 [System.ComponentModel.Description("Creates a Random Number and stores the value in the Context Parameter defined. The value can be created once the test starts, or it can be updated before each web request.")]
    public class RandomNumberContextParameter : WebTestPlugin
    {
        [System.ComponentModel.Description("If True, the random number will be recalculated before each request is made. The default is false.")]
        [System.ComponentModel.DefaultValue(false)]
        public bool UpdateBeforeRequest { get; set; }
 
        [System.ComponentModel.Description("Name of the Context Paramter that will stroe the random number.")]
        [System.ComponentModel.DefaultValue("RandomNumber")]
        public string ContextParameter { get; set; }
 
        [System.ComponentModel.DefaultValue(1)]
        public int MinimumValue { get; set; }
        
        [System.ComponentModel.DefaultValue(101)]
        public int MaximumValue { get; set; }
        
        public RandomNumberContextParameter()
        {
            UpdateBeforeRequest = false;
            ContextParameter = "RandomNumber";
            MinimumValue = 1;
            MaximumValue = 100;
 
        }
        public override void PreWebTest(object sender, PreWebTestEventArgs e)
        {
            if (string.IsNullOrEmpty(ContextParameter))
                throw new ArgumentException(this.ToString() + "Error --> ContextParameter property cannot be null or empty. Please define a context paramater to output the random number.");
            
            if (MinimumValue > MaximumValue + 1)
                throw new ArgumentOutOfRangeException(this.ToString() + "Error --> The property for the MinimumValue must less than or equal to MaximumValue.");
           
            //note the Random.Next() method creates a random number based on the min, and max values, however the max value returnd is one less than specified
            //so we add one here so that it works the way most people would intuitively expect.
            e.WebTest.Context[ContextParameter] = new Random().Next(MinimumValue,MaximumValue + 1).ToString();
                      
        }
        public override void PreRequestDataBinding(object sender, PreRequestDataBindingEventArgs e)
        {
            //if they want a new random number generated for each new request
            if (UpdateBeforeRequest)
                e.WebTest.Context[ContextParameter] = new Random().Next(MinimumValue, MaximumValue + 1).ToString();
        }
 
    }