Freigeben über


WebTestPlugin: SetLoopTerminatingValue

WebTestPlugin - SetLoopTerminatingValue

I created this Visual Studio 2010 webtestplugin which allows you to set the terminating value property of a conditional loop in a Declarative Web Test. As an example you might want to load the terminating value from a data source or other Context Parameter. Add a class to your test project and put this code in the .cs file,be sure to add a using for System.ComponentModel.

The idea for this came from a question posted on the forum here: https://social.msdn.microsoft.com/Forums/en-US/vstswebtest/thread/fc77f2f6-3e4d-488e-9a01-d87f06ed92e3

 

 [Description("WebTestPlugin that sets a Conditional Loop terminating value dynamically in the PreWebTest event to a specified Context Parameter value. If using a datasource, be sure to select All Columns in the datasource properties in case the value your using is not bound in the test.")]
 public class SetLoopTerminatingValue : WebTestPlugin
 {
 [Description("ContextParameter specified in the Conditional Rule to hold the loop counter value. This is used to confirm which conditional rule you want this plugin to work against.")]
 public string ConditionalRuleContextParameter { get; set; }
 [Description("ContextParameter that holds the value you want to set the TerminatingValue property to in the conditional loop. If this is from a data source the syntax will resemble the following: DataSourc1.DataFile#csv.LoopTerminateValue")]
 public string ContextParameterTerminatingValue { get; set; }
 public override void PreWebTest(object sender, PreWebTestEventArgs e)
 {
 DeclarativeWebTest dwt = e.WebTest as DeclarativeWebTest;
 foreach (var conditionalRule in dwt.ConditionalRuleReferences)
 {
 //make sure this is the right conditional rule (they may have more than one) by checking the ContextParameterName that
 //the loop counter is using. This was specified by the user so they should be able to get this easily. 
 if (conditionalRule.Properties["ContextParameterName"] == ConditionalRuleContextParameter)
 {
 //make sure the context parameter they provided exists before setting terminating value
 if (e.WebTest.Context.ContainsKey(ContextParameterTerminatingValue))
 {
 conditionalRule.Properties["TerminatingValue"] = e.WebTest.Context[ContextParameterTerminatingValue].ToString();
 }
 else
 {
 //let them know this does not exist, and this will fail the test execution.
 throw new ArgumentException("Context Parameter for the Terminating Value does not exist. Please check for the existense of context parameter specified as '" + ContextParameterTerminatingValue + "'");
 }
 }
 }
 }
 }