4. Edit webtest
3. Add extraction rules and custom Extraction Rules
Add extraction rules
1. When a part of data must be captured from a specific page and used on another page, Extraction Rules must be used. You can use the Extraction Rules to copy strings from the response and store the strings in context variables for any subsequent requests. By displaying the details pane, you can check the context in the Web Test viewer.
2. webtest provides six Extraction Rules:
Custom Extraction Rules
You can create your own Extraction Rules by deriving from the extractionrule class.
1. Create a class library project for custom Extraction Rules
2. Similarly, you must add a reference to Microsoft. visualstudio. testtools. webtesting in the class library.
3. Create a slave Class. Implementation And Member. Create the myextractionrule class. An example is provided on msdn.Code:
Using system;
Using system. Collections. Generic;
Using Microsoft. visualstudio. testtools. webtesting;
Using system. Globalization;
Namespace classlibrary2
{
Public class myextractionrule: extractionrule
{
Private string name;
Public string name
{
Get {return name ;}
Set {name = value ;}
}
Public override string rulename
{
Get {return "myextractionrulename ";}
}
Public override string ruledescription
{
Get {return "myextractionruledescription ";}
}
Public override void extract (Object sender, extractioneventargs E)
{
If (E. Response. htmldocument! = NULL)
{
Foreach (htmltag tag in E. Response. htmldocument. getfilteredhtmltags (New String [] {"input "}))
{
If (string. Equals (tag. getattributevalueasstring ("name"), name, stringcomparison. invariantcultureignorecase ))
{
String formfieldvalue = tag. getattributevalueasstring ("value ");
If (formfieldvalue = NULL)
{
Formfieldvalue = string. empty;
}
E. webtest. Context. Add (this. contextparametername, formfieldvalue );
E. Success = true;
Return;
}
}
}
E. Success = false;
E. Message = string. Format (cultureinfo. currentculture, "not found: {0}", name );
}
}
}
4. Build
5. Add a reference to the test project
6. Display custom Extraction Rules in the "add Extraction Rules" dialog box.
7. Download myextractionrule demo