05 Best Practices in VSTS Scripting - QAI Global...

23
Best Practices in VSTS Scripting 1

Transcript of 05 Best Practices in VSTS Scripting - QAI Global...

Page 1: 05 Best Practices in VSTS Scripting - QAI Global Servicesminisites.qaiglobalservices.com/stc2012/Paper_ Best_Practice/05... · Best Practices in VSTS Scripting 1. Abstract/Business

Best Practices in VSTS Scripting

1

Page 2: 05 Best Practices in VSTS Scripting - QAI Global Servicesminisites.qaiglobalservices.com/stc2012/Paper_ Best_Practice/05... · Best Practices in VSTS Scripting 1. Abstract/Business

Abstract/Business CaseThis document is aimed at people who are new to scripting in VSTS. Theintention of this document is to explain the best practices in scripting withVSTS.Visual Studio Team System is a collection of integrated tools which isdeveloped by Microsoft Corporation for managing the softwaredevelopment process. VSTS can be used for performance testing of softwareapplication developed in .NET. Scripting using VSTS tool is simple and easyas it will automatically do the correlations and validation for executing arecorded script. We can opt any of the language for scripting in VSTS from

2

recorded script. We can opt any of the language for scripting in VSTS fromthe available languages like Visual C-Sharp, Visual Basic, Visual C++.In thisdocument we will cover some basic ideas for scripting in C-Sharp and tohow to handle the common errors during scripting. When we record a webperformance test script using VSTS, the extension of the recorded file willbe .webtest. The .webtest file that is generated can be customized veryminimally. Therefore in order to do more customizations in the recordedscript the .webtest file has to be converted into a coded format. Theconverted code script will be generated in C-Sharp and the extension of thefile will be ‘.cs’. One can easily customize such scripts with minimumknowledge in C-Sharp. This document describes about the common objectsand areas which user should know to start with scripting in VSTS.

Page 3: 05 Best Practices in VSTS Scripting - QAI Global Servicesminisites.qaiglobalservices.com/stc2012/Paper_ Best_Practice/05... · Best Practices in VSTS Scripting 1. Abstract/Business

Introduction / Background And Problem

Statement

• This document gives an idea about some of the important topics needed to understand and start scripting in VSTS. It has the concepts of Parameterization and Correlation.

• If an application is developed on .Net technologies VSTS can be

3

• If an application is developed on .Net technologies VSTS can be used to do Performance Testing as it is a cheaper tool compared to Load Runner.

Page 4: 05 Best Practices in VSTS Scripting - QAI Global Servicesminisites.qaiglobalservices.com/stc2012/Paper_ Best_Practice/05... · Best Practices in VSTS Scripting 1. Abstract/Business

PROPOSED SOLUTION

STEP 1: CREATING A SOLUTION AND PROJECT:

• To create a solutions go to • Start -> All Programs -> Microsoft Visual Studio 2010• Click on New Project, give the details and save.• Once you save the project, solution for the project will be

4

• Click on New Project, give the details and save.• Once you save the project, solution for the project will be created and solution name will be displayed in the solution explorer window on the left side. A solution can have one or more projects and each project can have one or more webtests.• Solution is nothing but a single VSTS instance containing the entire test according to our needs. • The next step after creating a solution and Project is to start scripting.

Page 5: 05 Best Practices in VSTS Scripting - QAI Global Servicesminisites.qaiglobalservices.com/stc2012/Paper_ Best_Practice/05... · Best Practices in VSTS Scripting 1. Abstract/Business

PROPOSED SOLUTION(CONTD..)

STEP 2: SCRIPTING

I. CREATING A WEBTEST:

• For creating a new Web Performance test, right click on the project name under solution explorer and select ‘Add -> Web

5

project name under solution explorer and select ‘Add -> Web Performance test’ from the menu. This will open a browser where you can record your application. This browser window will have a Web Test Recorder frame on the left side for recording the actions. Once you finish recording, click stop button and this will create the recorded webtest.

• A recorded webtest has Form Post Parameters and Query String Parameters

Page 6: 05 Best Practices in VSTS Scripting - QAI Global Servicesminisites.qaiglobalservices.com/stc2012/Paper_ Best_Practice/05... · Best Practices in VSTS Scripting 1. Abstract/Business

PROPOSED SOLUTION(CONTD..)

STEP 2: SCRIPTING

I. CREATING A WEBTEST:• The values which have to be passed as a part of the request come as

Form Post Parameters. For example, the user name we are giving in a login window will add as a form post parameter in that request. We

6

login window will add as a form post parameter in that request. We can also add form parameters explicitly by right clicking and selecting ‘Add Form Post Parameter’ option. The same can be added as follows in C#.

Request1.Method = "POST";FormPostHttpBody request1Body = new FormPostHttpBody();

Request1Body.FormPostParameters.Add("username", “manoj_manu”);These values can be parameterized either using a DB or a CSV.

Page 7: 05 Best Practices in VSTS Scripting - QAI Global Servicesminisites.qaiglobalservices.com/stc2012/Paper_ Best_Practice/05... · Best Practices in VSTS Scripting 1. Abstract/Business

PROPOSED SOLUTION(CONTD..)STEP 2: SCRIPTING

I. CREATING A WEBTEST:• On the other hand the changing (dynamic) parts of a request will come as a query string parameter in VSTS. Suppose when you click on a link, the url is coming as follows.

https://www.example.com/ems/kr?s=plqrr345-mnkren-00dechi_10.100.100.01in this request, the part ‘s=plqrr345-mnkren-00dechi_10.100.100.01’ is a dynamicvalue. When we record script in VSTS, this part of the code will add as a query

7

value. When we record script in VSTS, this part of the code will add as a querystring parameter as part of that request. We can also add a query string explicitlyto our request by right clicking and selecting ‘Add URL Query String Parameter’ option.The same will appear in C# coded script as

request2.QueryStringParameters.Add("s",”plq rr345-mnkren-00dechi_10.100.100.01” , false, false);

Since it is a dynamic value, it has to be correlated.

Page 8: 05 Best Practices in VSTS Scripting - QAI Global Servicesminisites.qaiglobalservices.com/stc2012/Paper_ Best_Practice/05... · Best Practices in VSTS Scripting 1. Abstract/Business

PROPOSED SOLUTION(CONTD..)STEP 2: SCRIPTING

ii. PARAMETERIZATION:• To parameterize in VSTS first a DB or a CSV file has to be added to the script.

• To add a DB to the webtest click on the DB icon give the DB details proceed and select the table needed.

8

and select the table needed.

• To add a CSV to the webtest it has to be saved first in a location with the data. Then select ‘Data -> Add New Data Source’ option from the menu or click on the ‘Add Data Source’ DB icon on top which is adjacent to Run Test button. Next, you need to browse to the location where the CSV is stored. Once you add the CSV to your webtest, a message box will appear asking whether you need to save it as a part of your solution or not. Select ‘Yes’, otherwise the CSV will be saved local to your machine and script will throw error when you migrate or execute it on another machine (controller).

Page 9: 05 Best Practices in VSTS Scripting - QAI Global Servicesminisites.qaiglobalservices.com/stc2012/Paper_ Best_Practice/05... · Best Practices in VSTS Scripting 1. Abstract/Business

PROPOSED SOLUTION(CONTD..)

STEP 2: SCRIPTING

After adding the DB/CSV go to the where the value has to be

parameterized and expand the form post parameters. Select the form

post parameter value where the parameterized value has to be

inserted. The solution explorer in the right hand corner will have the

9

inserted. The solution explorer in the right hand corner will have the

added CSV/DB file. Select the appropriate CSV/DB file and expand until

you find the field name mentioned in the CSV/DB. Select the field

name and it will parameterize your value.

iii. THINK TIMES:Think time for each URL can be set in the right side corner

window called the solution explorer which shows the properties

of that particular URL

Page 10: 05 Best Practices in VSTS Scripting - QAI Global Servicesminisites.qaiglobalservices.com/stc2012/Paper_ Best_Practice/05... · Best Practices in VSTS Scripting 1. Abstract/Business

PROPOSED SOLUTION(CONTD..)

STEP 3: SCRIPTING

iv. VALIDATION RULES:• 9 types of validation rules are available in VSTS. The most commonly used validation rule is ‘find Text’. Its function is to look for a text that we have given in the script and confirm if the page has loaded correctly.

10

we have given in the script and confirm if the page has loaded correctly.

• To add a validation Rule right click and on the URL and select add validation rule.

Page 11: 05 Best Practices in VSTS Scripting - QAI Global Servicesminisites.qaiglobalservices.com/stc2012/Paper_ Best_Practice/05... · Best Practices in VSTS Scripting 1. Abstract/Business

Some basic functions in webtest

11

Some basic functions in webtestand corresponding functions in coded script

Page 12: 05 Best Practices in VSTS Scripting - QAI Global Servicesminisites.qaiglobalservices.com/stc2012/Paper_ Best_Practice/05... · Best Practices in VSTS Scripting 1. Abstract/Business

PROPOSED SOLUTION(CONTD..)

Adding Comment to a Webtest

• You can add comment to a webtest as mentioned below. • Right click on the url where the comment has to be placed and select ‘Insert Comment’ option. We cannot comment an unwanted url in our webtest but can be done in a coded script. Hence the webtesthas to be converted to the coded format. For this click on the generate code icon.

12

has to be converted to the coded format. For this click on the generate code icon.

• For adding comment in a coded script it has to be done in C# as follows.

this.AddCommentToResult("this is a comment");

• We can also comment an unwanted portion of the script by inserting that portion in between ‘/*’ and ‘*/’ or can be commented using ‘//’ in the beginning of the code.

Page 13: 05 Best Practices in VSTS Scripting - QAI Global Servicesminisites.qaiglobalservices.com/stc2012/Paper_ Best_Practice/05... · Best Practices in VSTS Scripting 1. Abstract/Business

PROPOSED SOLUTION(CONTD..)

Adding Transaction

• In order to find the response time for a particular request, we need to insert that request inside a transaction. Transaction can be added in a webtest by Right clicking on the url and selecting the ‘Insert Transaction’ option. The transaction name, starting url and ending

13

Transaction’ option. The transaction name, starting url and ending url for that particular transaction has to be specified carefully.

• A transaction in C# code can be specified within a begin transaction and end transaction statements as follows.

Page 14: 05 Best Practices in VSTS Scripting - QAI Global Servicesminisites.qaiglobalservices.com/stc2012/Paper_ Best_Practice/05... · Best Practices in VSTS Scripting 1. Abstract/Business

PROPOSED SOLUTION(CONTD..)Adding Transaction• In order to find the response time for a particular request, we need to insert that request inside a transaction. Transaction can be added in a webtest by Right clicking on the url and selecting the ‘Insert Transaction’ option. The transaction name, starting url and ending url for that particular transaction has to be specified carefully.

14

• A transaction in C# code can be specified within a begin transaction and end transaction statements as follows.

this.BeginTransaction("Transaction Name");

• /* Request and body of the request */

this.EndTransaction("Transaction Name");

Page 15: 05 Best Practices in VSTS Scripting - QAI Global Servicesminisites.qaiglobalservices.com/stc2012/Paper_ Best_Practice/05... · Best Practices in VSTS Scripting 1. Abstract/Business

PROPOSED SOLUTION(CONTD..)

Inserting A Request

• We can insert a request in to the recorded script by right clicking on a urlbelow the URL where the request needs to be placed. From the appearing menu, select ‘Insert Request’ option. This will create an http dummy request where you can place your original request. You can also simply copy and paste a url wherever you want.

• WebTestRequest object is used to create a request in C# code. Its usage is as

15

• WebTestRequest object is used to create a request in C# code. Its usage is as follows.WebTestRequest request1 = new WebTestRequest("https://www.google.com"); where ‘request1’ is the name of the request. You can give any name for the request but giving same name for different requests will throw error.

• A request has several required parameter associated with it. Think time for a particular request can be give as Request1.ThinkTime = 5;

Page 16: 05 Best Practices in VSTS Scripting - QAI Global Servicesminisites.qaiglobalservices.com/stc2012/Paper_ Best_Practice/05... · Best Practices in VSTS Scripting 1. Abstract/Business

PROPOSED SOLUTION(CONTD..)

Correlation• The changing (dynamic) parts of a request will come as a query string parameter in VSTS. Suppose when you click on a link, the url is coming as follows.

• https://www.example.com/ems/kr?s=plqrr345-mnkren-00dechi_10.100.100.01 in this request, the part ‘s=plqrr345-mnkren-00dechi_10.100.100.01’ is a dynamic value. When we record script in VSTS, this part of the code will add as a query string parameter as part of that

16

this part of the code will add as a query string parameter as part of that request. We can also add a query string explicitly to our request by right clicking and selecting ‘Add URL QueryString Parameter’ option. The same will appear in C# coded script as

request2.QueryStringParameters.Add("s",”plq rr345-mnkren-00dechi_10.100.100.01” , false, false);

• Since it is a dynamic value, it has to be correlated. Once we correlate that value with a parameter then the query string will appear as request2.QueryStringParameters.Add("s", this.Context["ParameterValue"].ToString(), false, false);

Page 17: 05 Best Practices in VSTS Scripting - QAI Global Servicesminisites.qaiglobalservices.com/stc2012/Paper_ Best_Practice/05... · Best Practices in VSTS Scripting 1. Abstract/Business

PROPOSED SOLUTION(CONTD..)CorrelationWe can add dependent request to a main request. In order to addd this, right click

on the main request and then select ‘ Add Dependent Request’ option. The same can be implemented in C# script by the following way.

WebTestRequest request1 = new WebTestRequest("https://www.example.com/"); request1.ThinkTime = 3;WebTestRequest request1Dependent1 = new WebTestRequest("http://www.exapmle1.com");

17

WebTestRequest("http://www.exapmle1.com");request1.DependentRequests.Add(request1Dependent1);WebTestRequest request1Dependent2 = new WebTestRequest("http://

www.exapmle2.com ");request1.DependentRequests.Add(request1Dependent2);

All the requests have to be ended using the following syntax.yield return request1;request1 = null;

Page 18: 05 Best Practices in VSTS Scripting - QAI Global Servicesminisites.qaiglobalservices.com/stc2012/Paper_ Best_Practice/05... · Best Practices in VSTS Scripting 1. Abstract/Business

PROPOSED SOLUTION(CONTD..)

Extraction Rule Method 1:

• Recorded script may automatically handle dynamic parameters using extraction rule. But this automatic binding of dynamic values may not be always correct. You can also create an extraction rule of your own if required. If you want to correlate a value in one request, you need to search that value in the previous request. Right click on the previous request and select ‘Add Extraction Rule’ option from the menu. You are required to give the name of context parameter (The parameter name that needs to be extracted), and other mandatory fields for that particular type of extraction rule.

• There are 8 types of extraction rules in which we commonly use 2 types; Extract

18

• There are 8 types of extraction rules in which we commonly use 2 types; Extract HTTP Header and Extract Text. In Extract HTTP Header extraction rule, you are required to give Context Parameter Name and Name of the Header to be captured.

ExtractHttpHeader extractionRule1 = new ExtractHttpHeader(); /* extractionRule1 name of the extraction rule */

extractionRule1.Header = "Set-Cookie"; // Set-Cookie is the name of the http header

extractionRule1.Required = true; /* required = true specifies that this extraction rule is mandate and throws error if the specified header not found in the request */

extractionRule1.ContextParameterName = "CookieValue"; // Name of the Context Parameter

request1.ExtractValues += new EventHandler<ExtractionEventArgs>(extractionRule1.Extract);

Page 19: 05 Best Practices in VSTS Scripting - QAI Global Servicesminisites.qaiglobalservices.com/stc2012/Paper_ Best_Practice/05... · Best Practices in VSTS Scripting 1. Abstract/Business

PROPOSED SOLUTION(CONTD..)Extraction Rule Method 1:• Now suppose your http header is as follows.

Set-Cookie : dp=94; kc=85dferKlf; • Suppose you want to pass a part of the header value (eg. kc cookie value) into your application in the next request. For that you need to capture the http header as mentioned above and below code can be used to extract the value from header.

char[] delimiterChars = { '=', ';' };string[] words = (this.Context["CookieValue"].ToString()).Split(delimiterChars);string dpValue = string.Empty;string kcValue = string.Empty;

19

string kcValue = string.Empty;foreach (string s in words){

System.Console.WriteLine(s);i++;if (i == 2)dpValue = s.ToString();

if (i == 4)kcValue = s.ToString();

}Once the’ kc’ cookie value is extracted, we can add as a form post parameter into your script as follows.

Request2Body.FormPostParameters.Add("kc", kcValue);

Page 20: 05 Best Practices in VSTS Scripting - QAI Global Servicesminisites.qaiglobalservices.com/stc2012/Paper_ Best_Practice/05... · Best Practices in VSTS Scripting 1. Abstract/Business

PROPOSED SOLUTION(CONTD..)Extraction Rule Method 1:• Extract Text extraction rule is another important part correlation which we commonly use in our script. We mandatorily need to give the Context Parameter Name, left boundary and right boundary fields for this extraction rule.

• For example, if we want to extract the text ‘India’ from the below line, we can use as follows.

<

20

<option value="India">India</option>

<option value="SriLanka">SriLanka</option>

ExtractText extractionRule1 = new ExtractText(); // Name of extraction rule

extractionRule1.StartsWith = "<option value=\""; // left boundary of the value to be captured

extractionRule1.EndsWith = "\""; //right boundary of the value to be captured

extractionRule1.IgnoreCase = false; //ignore case during search for matching text

Page 21: 05 Best Practices in VSTS Scripting - QAI Global Servicesminisites.qaiglobalservices.com/stc2012/Paper_ Best_Practice/05... · Best Practices in VSTS Scripting 1. Abstract/Business

PROPOSED SOLUTION(CONTD..)Extraction Rule Method 1:

extractionRule1.UseRegularExpression = false; // If true, uses a regular expression during search

extractionRule1.Required = true; // If true, script will fail if matching value is not found

extractionRule1.ExtractRandomMatch = false; //If true, all matches are found and use a match randomly

extractionRule1.Index = 0; // Indicates the position of occurrence of searching text. 0 indicates first occurrence

extractionRule1.HtmlDecode = true; // If true, it will perform HTML decoding of extracted text

extractionRule1.ContextParameterName = "CountryValue"; // Name of the parameter

request1.ExtractValues += new EventHandler<ExtractionEventArgs>(extractionRule1.Extract);

21

request1.ExtractValues += new EventHandler<ExtractionEventArgs>(extractionRule1.Extract); //

• This will add the extraction rule to the part of request1

• This extracted value can passed as a form post parameter for next requests.

Request2Body.FormPostParameters.Add("Country", this.Context["CountryValue"].ToString());

• Unlike Load Runner, you can use the same name for 2 different extraction rules. This concept is useful when the left boundary or index of the searching text will differ. For example, you would like to use country name value as India and its left boundary either any of the following. Check the below example in that case.

<option selected="selected" value="India">India</option>

Page 22: 05 Best Practices in VSTS Scripting - QAI Global Servicesminisites.qaiglobalservices.com/stc2012/Paper_ Best_Practice/05... · Best Practices in VSTS Scripting 1. Abstract/Business

PROPOSED SOLUTION(CONTD..)Extraction Rule Method 2:<option value="India">India</option>• In the above case the left boundary value can be either of the case. Then we can specify the extraction rule in the following ways so that our code will never fail.

ExtractText extractionRule1 = new ExtractText();extractionRule1.StartsWith = "<option selected=\”selected\” value=\"";

extractionRule1.EndsWith = "\""; extractionRule1.IgnoreCase = false;

22

extractionRule1.IgnoreCase = false; extractionRule1.UseRegularExpression = false; extractionRule1.Required = false; // This should be false. So if this extraction rule is not found, it won’t throw any error.

extractionRule1.ExtractRandomMatch = false; extractionRule1.Index = 0; extractionRule1.HtmlDecode = true; extractionRule1.ContextParameterName = "CountryValue"; // Name of the parameter

request1.ExtractValues += new EventHandler<ExtractionEventArgs>(extractionRule1.Extract);

Page 23: 05 Best Practices in VSTS Scripting - QAI Global Servicesminisites.qaiglobalservices.com/stc2012/Paper_ Best_Practice/05... · Best Practices in VSTS Scripting 1. Abstract/Business

PROPOSED SOLUTION(CONTD..)Extraction Rule Method 2:ExtractText extractionRule2 = new ExtractText();extractionRule2.StartsWith = "<option value=\""; extractionRule2.EndsWith = "\""; extractionRule2.IgnoreCase = false; extractionRule2.UseRegularExpression = false; extractionRule2.Required = false; // This should be false so that if this extraction rule is not found, it won’t throw any error.

extractionRule2.ExtractRandomMatch = false;

23

extractionRule2.ExtractRandomMatch = false; extractionRule2.Index = 0; extractionRule2.HtmlDecode = true; extractionRule2.ContextParameterName = "CountryValue"; // Name of the parameter

request1.ExtractValues += new EventHandler<ExtractionEventArgs>(extractionRule2.Extract);

• This extracted value can passed as a form post parameter for next requests.

Request2Body.FormPostParameters.Add("Country", this.Context["CountryValue"].ToString());