4

I have created custom extraction rule and the property i need the properties to be data driven i.e. I want to attach a data source (CSV file). how do I go about it ?

Followings are the screen shots and code snippets that describes the problem

Code snippet for custom extraction rule class

public class CustomeExtractionClass : ExtractionRule
{
    public string Name
    {
        get;
        set;
    }

    public override void Extract(object sender, ExtractionEventArgs e)
    {
          // Code to extract text/values from Response Based On NAME(i.e. Property) value 
             received from UI
    }
}

UI for Name property

Note : Textbox next to Name property

enter image description here

How do I make it Data driven ? just like the one we get while inserting FormPost parameters... here is the example

enter image description here

Note the dropdown button bottom right which pulls up the attached data sources... I want the Name property values to be attached to the same data source.... how do i go about it ???

Mithun Ganatra
  • 453
  • 4
  • 21

2 Answers2

3

Finally after spending many hours I was able to get the values from the CSV file into my custom extraction rule. I could not bind the CSV file column to the Extraction rule property, however i got the work around. here is what i was missing

[DataBinding("DataSource1", "mycsvfile#csv", "ProcessInstanceID", "MyProcessInstanceID")]

here are the steps to achieve it

Step 1 : Add data Source to WebTest (Skip if already added)

Step 2 : Generate Code from WebTest (Skip if already done)

Step 3 : Bind datasource (i.e. CSV file) columns by adding following lines of code just above the declaration of your webtest class.

[DataSource("DataSource1", "Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\customextractionrule\\mycsvfile.csv", Microsoft.VisualStudio.TestTools.WebTesting.DataBindingAccessMethod.Sequential, Microsoft.VisualStudio.TestTools.WebTesting.DataBindingSelectColumns.SelectOnlyBoundColumns, "mycsvfile#csv")]
[DataBinding("DataSource1", "mycsvfile#csv", "ProcessInstanceID", "MyProcessInstanceID")]
public class WebTest2Coded : WebTest
{

Note : in above code "MyProcessInstanceID" is the name of context parameter which will be created by visual studio and value of CSV file column will be assigned to this context parameter. you can give any name you wish.

Step 4 : Access the value of the Context parameter in your custom extraction rule

public override void Extract(object sender, ExtractionEventArgs e)
    {
        string ProcessIDinCSvFile =  e.WebTest.Context["MyProcessInstanceID"].ToString());
Mithun Ganatra
  • 453
  • 4
  • 21
  • Can you please clarify: is the web test data driven? Or, are you trying to data drive just the extraction rule. The Microsoft documentation I found for `DataBindingAttribute` suggests that is is an internal class used to assist with creating tests and not intended for normal code. Where, or how, did you find out about using this attribute? – AdrianHHH Oct 17 '14 at 07:48
  • 2
    @Adrian : Yes the web test is data driven. also there are other columns which i need to use for extraction rule. I spent hours find information on MSDN Kb Article also tried many permutation and combinations and all the methods & properties of ExtractionEventArgs but it was of no use. Finally I found it when I generated code from my web test. I added DataBinding attribute and I hit Home run.... – Mithun Ganatra Oct 17 '14 at 11:14
  • @AdrianHHH : As per your suggestion I have created a new question. Can you please help here is a link to question : http://stackoverflow.com/questions/29605846/can-visual-studio-2012-load-testing-capture-performance-counters-of-linux-server – Mithun Ganatra Apr 14 '15 at 06:10
  • @MithunGanatra Do you know if there is a way to loop through the bound data? I would really like to crate a lookup table for values to use based on current state. – Brettski Feb 05 '16 at 04:04
  • @Brettski : Can you please elaborate your problem statement ? – Mithun Ganatra Feb 26 '16 at 09:00
0

To set the context parameter to the text within the field just enter the full name of the data source field enclosed in doubled curly braces into the field. The correct form of text is shown circled in red in an image in the question, namely {{DataSource1.mycsvfile#csv.ProcessInstanceID}}.

Data source fields can also be accessed directly from the context by code such as

string theText = e.WebTest.Context["DataSource1.mycsvfile#csv.ProcessInstanceID"].ToString();

If the Name field of the extraction rule is set to the name of a content parameter (ie not to its value) then the current value can be read out and a new value inserted with code like

string theText = e.WebTest.Context[Name].ToString();
e.WebTest.Context[Name] = "The new string";
AdrianHHH
  • 13,492
  • 16
  • 50
  • 87
  • 2
    Thanks for the reply.... how ever the proposed solution doesn't work. I tried adding datasource field to Name property by using "{{DataSource1.mycsvfile#csv.ProcessInstanceID}}" and that dint work for me. also tried adding context parameter to webtest and tried to bind the value of context parameter to datasource field by suppling "{{DataSource1.mycsvfile#csv.ProcessInstanceID}}" but that dint work as well. it seems that Context parameter and Name property for extraction rule accepts only string values. – Mithun Ganatra Oct 13 '14 at 09:29
  • I found you question not very clear so wrote a reasonable answer. The comment saying "*that dint work for me*" is also very vague. What did happen and what did you expect? I find that adding calls of `e.WebTest.AddCommentToResult(...)` can be very useful for understanding what is happening. – AdrianHHH Oct 13 '14 at 13:09
  • 2
    what I meant is when I use "{{DataSource1.mycsvfile#csv.ProcessInstanceID}}" I dont get values in CSV file.... I was expectung the value to be returned which is something like "Abc1234".... but value at runtime is "{{DataSource1.mycsvfile#csv.ProcessInstanceID}}".. Here is a more description... I want Name property of Custom Extraction Rule to be Data Binded... – Mithun Ganatra Oct 13 '14 at 14:05
  • You are asking about an "extraction rule" without explaining how you are extracting something that is in the data source. Have you tried getting the value from the context as shown in the second block of code in the answer? – AdrianHHH Oct 13 '14 at 14:51
  • 2
    here is a problem statement. – Mithun Ganatra Oct 15 '14 at 11:12
  • 2
    my web request returns table with multiple rows. each row has two columns Process Instance Id and LockId (i.e. this lockId is generated at server dynamically). all my subsequent requests depends on this lockId and Process Instance Id. I have process instance Id with me and I have stored it in CSV file. Now to get lockId i created extraction rule, but i need to tell my extraction rule that I need lockid for which process Instance Id. so I need to bind my extraction rule property with CSV file column. hope this gives you clear picture of my question. – Mithun Ganatra Oct 15 '14 at 11:28
  • 2
    once again thanks for replying and really appreciate your time & effort for helping me. – Mithun Ganatra Oct 15 '14 at 11:28