Freigeben über


WebTestPlugin: AppendValueToContextParameter

Here is a plugin that allows you to append any context parameter value to any other context parameter. This plugin is a WebTestPlugin that overrides the PreWebTest event, so this will happen before the WebTest executes any requests. You can easily make a variation of this plugin as a WebTestRequestPlugin, and override the PreRequestDataBinding event so that this could append any context param to any other, but would do it before the request you have bound it to. In additon to appending context parameters, you can also just type in a hard coded string value to append and that will also work.

 

  [System.ComponentModel.Description("WebTestPlugin, Overrides PreWebTest and appends either a context parameter or hard coded string to a specified target Context Parameter. As an example, this plugin could append the built in $WebTestIteration value to an existing datasource column value.")] 
 public class AppendValueToContextParameter : WebTestPlugin
 { 
 [System.ComponentModel.DefaultValue("DataSource1.SomeDataFile#csv.FieldName")]
 [System.ComponentModel.Description("The target context parameter that you would like to append the source context param value")]
 public string ContextParamTarget { get; set; }
 
 [System.ComponentModel.DefaultValue("$WebTestIteration")]
 [System.ComponentModel.Description("The source context parameter, or a hard coded string value you would like appended to the target context param")]
 public string ContextParamOrValueSource { get; set; }
 
 public AppendValueToContextParameter()
 {
 ContextParamOrValueSource = "$WebTestIteration";
 ContextParamTarget = "DataSource1.SomeDataFile#csv.FieldName";
 }
 
 public override void PreWebTest(object sender, PreWebTestEventArgs e)
 {
 //confirm the target context param exists 
 if (e.WebTest.Context.ContainsKey(ContextParamTarget))
 {
 //is the ContextParmOrValueSource a Context Parameter or a hard coded value?
 if (e.WebTest.Context.ContainsKey(ContextParamOrValueSource))
 {
 e.WebTest.Context[ContextParamTarget] += e.WebTest.Context[ContextParamOrValueSource].ToString();
 }
 else //hard coded value
 {
 e.WebTest.Context[ContextParamTarget] += ContextParamOrValueSource;
 } 
 }
 else
 {
 throw new ArgumentException("Required Context Parameter Target was not found");
 }
 }
 }