About web test plugin
-
Dienstag, 13. März 2012 13:11
Hi,
1- I have 3 webtests "W1","W2", and "W3".
2- Then i created a web test called "Main".
3- In the "Main" webtest i called the 3 web tests "W1", "W2", and "W3".
4- In the "Main" webtest, i added a web test plugin.
I found the plugin called or run 4 times; one before each web test, but i need the plugin to be run once before the "Main" webtest only.
Thanks
Alle Antworten
-
Dienstag, 13. März 2012 15:37
Hi MHSK,
The behavior described is by design when you set the Inherit Web Test Settings = True. This allows each webtest that is called to inherit all the plugins, validation rules and properties from the parent test. Since the VUser Context flows through by reference - down to these subtests, then you can easily add a Context Parameter in the plugin when it runs in the "Main" test, then just check for this value in the plugin so that it bypasses that processing in the sub tests "W2, W3" in your case.
As an Example, here is a real plugin that generates a random number, so note the creation of the context parameter called "RandomNumberGenerated". And the check for the existence of this parameter and return if its there, see the comments below.
[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 (e.WebTest.Context.ContainsKey("RandomNumberGenerated")) { //we have already processed this in the main test, so if we are in a sub test, just return return; } 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(); e.WebTest.Context["RandomNumberGenerated"] = "Created"; } 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(); } }http://blogs.msdn.com/rogeorge
- Als Antwort vorgeschlagen smetah Freitag, 16. März 2012 01:42
-
Mittwoch, 14. März 2012 13:13
Thanks rogeorge,
but how i run this plugin for each test iteration of the "Main" webtest, so if i have 10 records in the data source, then the web test plugin run 10 times?
Thanks
-
Mittwoch, 14. März 2012 16:14
Hi MHSK,
That is a good point, the code would only run once as it stands currently for the first iteration of each VUser. So, its easy to modify to get all the behavior your looking for, so to summarize based on what you have provided thus far:
- Plugin executes on the PreWebTest event.
- Plugin should execute only in the PreWebTest event for the Parent test, not for any sub tests called by the parent.
- Plugin should execute for each iteration of the VUser.
This is solved by storing the $WebTestIteration that we last ran the plugin for, and checking in the PreWebTest event if the iteration has changed. The following snippet (note this is not related to the first sample, but should give you the basic idea).
public override void PreWebTest(object sender, PreWebTestEventArgs e) { if (e.WebTest.Context.ContainsKey("PreWebTestPluginIteration")) { //if the current $WebTestIteration equals the value we stored in PreWebTestPluginIteration //we dont need to run this time. On the next iteration the value will increment and allow if ((int.Parse(e.WebTest.Context["PreWebTestPluginIteration"].ToString())) == (int.Parse(e.WebTest.Context["$WebTestIteration"].ToString()))) { e.WebTest.AddCommentToResult(this.ToString() + "-> Current Iteration Already Processed for PreWebTest, Skipping"); return; } } e.WebTest.Context["Today"] = DateTime.Now.ToString(DateTimeFormat); e.WebTest.AddCommentToResult("** ** TodayContextParam Created ** ** " + e.WebTest.Context["Today"].ToString()); e.WebTest.Context["PreWebTestPluginIteration"] = e.WebTest.Context["$WebTestIteration"];
http://blogs.msdn.com/rogeorge
- Bearbeitet rogeorgeMicrosoft Employee Mittwoch, 14. März 2012 16:19
- Als Antwort markiert MHSK Donnerstag, 15. März 2012 10:03
-
Donnerstag, 15. März 2012 10:04Thanks rogeorge.

