Wednesday 14 July 2010

Add extraction rules to extract dynamic values from server response

Visual Studio implements extensible script correlation mechanism which is highly reusable within and across performance testing projects. This post will explore build-in correlation rules available in Visual Studio.

Extraction Rules are used in VSTS to extract value from server response and then pass that value to the server in another request. Based on the type of extraction rule (e.g. form field, tag value) and its parameters (e.g. form field name, tag name) rule will extract required value from the server response.

The most commonly used Extraction Rules build into VSTS are:
  • Extract Text
  • Extract Hidden Fields
  • Extract Form Field
  • Extract Attribute value
  • Tag inner text
The extracted value will be placed in VSTS context parameter with a name specified in Context Parameter Name parameter of the extraction rule.

Although there are no riles in naming context parameters it is usually a good practice to call the parameter with a name related to the extracted entity.


Extract Text
This is the most flexible rule which can extract any value which is located between two known boundary values.

Fragment HTML code:
<span class="" style="font-size:110%;"><strong class="selflink">Glass</strong> science topics</span>

If the aim is to extract Query String parameter ai as marked in bold the extraction rule’s Start and End with parameters could be set as:

Configured VSTS rule:


This parameter can be used to replace hard-coded values by replacing recorded value with parameter name surrounded by double curly brackets {{ai}}

Context parameter name: {{ai}}


Extract Hidden Fields
This rule will extract all hidden fields from the HTML and place them in the context parameter collection called $HIDDENX where the X is parameter name suffix. The usual convention is to use a number as the suffix so that the parameter array name will look like $HIDDEN33 if 33 is used as the context parameter name. The hidden field value can be then accessed by putting a dot and hidden field name after the hidden field collection name such as {{$HIDDEN33.fieldName}}

Configured VSTS rule:


Fragment HTML code:
<input name="start" value="firstElement" type="hidden">
For the above HTML code and Hidden field extraction rule with a context parameter name 33 hidden field value “firstElement” will be extracted to context parameter $HIDDEN33.start and can be used in the script such as {{$HIDDEN33.start}}

Context parameter name: {{$HIDDEN33.FieldName}} where FieldName is name of the html form hidden field.


Extract Form Field
This rule will extract value from the web form field. This can be useful while recording a script to edit personal details. This rule can be added to the page which starts data update process and then extracted values can be used while submitting profile changes. In this way tester has greater flexibility as some data keen be kept unchanged (using this extraction rule) and some can be updated using parameterization.

Fragment HTML code:
<b>Subject:</b><br /><input type="text" size="34" name="Contact_Address" value="15 Alexandra Palace Way"/>
Configured VSTS rule:


Context parameter name: {{Subject}}


Extract Attribute value
Rule to extract html tag attribute value. This rule can be used to extract value of the selected drop-down box. In the following example HTML code (which defines two drop-down items) the second option is preselected. Based on the tag name and using attribute/value as the filter it is possible to configure that rule to return value “en” of the selected drop-down item.

Fragment HTML code:
<option value="de" lang="de">Deutsch</option><option value="en" lang="en" selected="selected">English</option>
Configured VSTS rule:


Context parameter name: {{Language}}


Tag inner text
This rule will extract inner text of the HTML tag identified by its attribute/value and ordinal position.

Fragment HTML code:
<span class="" style="font-size:110%;"><strong class="selflink">Glass</strong> science topics</span>
Configured VSTS rule:


If the tag found by VSTS based on the search criteria contains child tags then VSTS will return inner text concatenated with inner text of all child tags. For the above fragment HTML code the result string will be “Glass science topics”

Although VSTS comes with a number of extraction rules already build into the tool it is possible (and certainly not unusual) to define custom rules which behaves in the same way as the build in ones.

Context parameter name: {{SelectedLanguage}}

For the full guide to developing VSTS WebTests please refer to the Using Fiddler with VSTS.

10 comments:

  1. I think some of your screen shots don't match up with your scenarios. Otherwise your post is helpful as a guide

    ReplyDelete
  2. Thanks for the supportive words. I think you might be looking on the wrong screen-shot without realising it.

    ReplyDelete
  3. Hello,
    I have a query regarding Extraction Rule.

    If I want to apply extraction rule on the part of the "String Body" then how do I apply it.

    For example in "String Body" suppose Lalita, Name is tag which I want to parameterised
    I added extraction rule for it:
    Select a Rule: Extraction Text
    Starts with:
    Ends with:
    ContextParameter Name: ExtractedName

    Now the above created Extracted Text I want to apply it for next web request's "String Body". How do I use it

    Environment: VSTS 2008
    Project: Silverlight based project
    Email Id: [email protected]

    ReplyDelete
  4. You will have to right click on the StringBody which you want to parametrize and go into properties and then edit it by replacing the text which you want to be parametrized with the parameter (which in this case will be {{ExtractedName}})

    ReplyDelete
  5. hiiii Maciej,

    my mail-id is:

    [email protected]
    [email protected]

    i have a query regarding vs2010 web test.
    i want to send a mail in an automation way,for that i have recorded a scenario and while recording i gt logged in to the yahoomail.com after logged in,compose a message and send the mail to the others account and get logged out.

    this scenario i have recorded with vs2010 web test but while playing it back i am getting error 401,403,500 like and the message was not sent to others account in an automation way.

    i will be very thankful to u if u cud help me on this .
    i am a new bie to the web test and for this scenarios i have tried the extraction rule also but still it is not working.

    any help in simple words would be appreciated .
    thanks
    suraj.

    ReplyDelete
  6. I also think you mixed the pics:
    the last screen shot shows the "Extract Attribute value" and not "Extract Tag inner text" dialog from vs2010

    ReplyDelete
  7. hi Maciej,

    I have created an extraction rule as follows :

    ExtractText extractionRule8 = new ExtractText();
    extractionRule8.StartsWith="_ViewOrderConfirmation_lblOrderNumber\">";
    extractionRule8.EndsWith = "";
    .
    .
    .
    .
    extractionRule8.ContextParameterName="ReportId";

    later I am trying to use this context parameter "ReportId" as follows :

    string str=this.Context["ReportId"].ToString();

    When I am trying to debug,I get the following msg :
    The name str does not exist in the current context.
    The name reportid does not exist in the current context.

    Can you tell me whats happening?

    ReplyDelete
  8. Hi anyone able to get the answer for above qn

    ReplyDelete
  9. A IEEE project is an interrelated arrangement of exercises, having a positive beginning and end point and bringing about an interesting result in Engineering Colleges for a particular asset assignment working under a triple limitation - time, cost and execution. Final Year Project Domains for CSE In Engineering Colleges, final year IEEE Project Management requires the utilization of abilities and information to arrange, plan, plan, direct, control, screen, and assess a final year project for cse. The utilization of Project Management to accomplish authoritative objectives has expanded quickly and many engineering colleges have reacted with final year IEEE projects Project Centers in Chennai for CSE to help students in learning these remarkable abilities.


    Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
    Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete