Tuesday, November 16, 2010

How to do split data based on delimiter with c Sharp code?

With below code we can split any data based on separator.

string path="C:\\test\\file\\TestResults";
int a = path.IndexOf("\\TestResults");
string ss = path.Substring(0, a - 1);
MessageBox.Show(ss);

Output Result:- C:\\test\\file
or

we can do split data with below code also

string path="C:\\test\\file\\TestResults";
string[] ss = new string[] { "\\TestResults" };

string[] st1 = path.Split(ss, StringSplitOptions.RemoveEmptyEntries);
MessageBox.Show(""+st1[0]);

How to create data driven test using Web Performance test?

Once developed web performance test with CS file, In CS file calling test do below settings for data driven test.
Click on Test Menu 
Click on Edit Test Testings and click on Local (local.testsettings) menu item in sub menu items -->
In Test settings window select Web Test and displayed Web Test pane turn on “One run per data source row” radio button 
Click on Save As button and click on save button in save as window.

Then script will execute based on how many rows of data is exist in the data source file (CSV).

How to add the References to projects ?

Each Built in or User defined Name Space contains few methods, if we want to use those methods first have to add reference to References and add using statement to the code before using Name spacing containing methods in the code.

Below one is the reference adding navigation:-
In the solution Under the specified project right click on “References” node 
Click on “Add Reference…” 
Click on Browse tab in “Add Reference” window
Select required path in look in like C:\Program Files\Reference Assemblies\Microsoft\Framework\.NetFramework\v4.0 and select required DLL and click on “OK” button

Then it displays DLL name under References folder.

In the code under namespace add this DLL name with “using” statement.

Monday, November 15, 2010

How to send Fail Message to Results with C sharp code?

Whenever application behavior is not expected, have to fail report to the test result, report same with below code.

throw new Exception(" Fail message to result");

Data Base Automation with C Sharp code in VSTS 2010 or TFS 2010

SQL Server Data base connection code:-

// Create object for SqlConnection
SqlConnection con=new SqlConnection(@”Network Library=DBMSSOCN;Data Source=XXXX; database=XXXXX;User id=XXXX;Password=XXXX;");
//Connect the SQL Server
con.Open();
// create object for query execution
SqlCommand dbcommand = con.CreateCommand();

dbcommand.CommandText = "Select * from tablename";

//For data reading from query result table create data reader
SqlDataReader dbReader = dbcommand.ExecuteReader();

//get the columns count from query table
int fieldcount = dbReader.FieldCount;

// There is no direct command for record count but with below code find query result table having one or more records.
Boolean result = dbReader.HasRows;

// Close the reader object
dbReader.Close();

//get the DB column Data from reader :-
dbReader[“Field Name”]

How to capture the Response in Web Performance test with C Sharp Code?

Use the “Extractvalues” method before “Yield return rquest1” statement in the generate .CS file and then define method with object and ExtractionEventArgs orguments like below .

Below statement add before the “Yield return rquest1” statement
Request1.ExtractValues +=new EventHandle (request1_ExtractValues);

Method definication :--

Void request1_ExtractValues(object sender,ExtractionEventArgs e)
{
// with below statement capture the response
var response=e.Response.BodyString;

// save the response with below code
XmlDocument doc=new XmlDocument();
doc.LoadXml( response);
// Get the value from required node
XmlNodeList var1=doc.GetElementsByTagName(“Node Name”)
// Get the nodes count
int NodeCount=var1.Count;
// for value capture code
Nodevalue=Var1[0].InnerText;

Send XML Request to Web service in Web Performance Test

In webservice send request three ways.
1. with variables
2. Array format
3. Xml Foramt

Below code using for send XML Request to web service

1. Load XML schema file to reference with below navigation
a. Right click on Project
b. click on Add
c. Click on New Item 
d. in “Add New Item - “ window Select on Resource File and give the name and click on ADD button 
e. It adds “Resources” folder and “ Name.resx” file to the Project 
f. Click on “ .resx” file and in open window Click on beside “Add Resource” arrow and click on “Add Existing File…” menu 
g. Select the required file path in “Add existing file to resources” window and click on “Open”
h. It add to the project and displays under Resources folder

2. In the .CS file code add below code for xml request file
XmlDocument RequestXMLFile=new XmlDocument();
XmlDocument xmlDoc=new XmlDocument();
// get the file from resource folder
String xmlData=resource.xmlfile name;
// Load the xml file
xmlDoc.LoadXml(xmlData);
//Messagebox.Show(xmlDoc.InnerXml);
// below code for xml node names naming space handling purpose

XmlNameTable xnt= xmlDoc.NameTable;
XmlNameSpaceManager nm=new XmlNameSpaceManager(xnt);
Nm.AddNamespace(“var1”,”http;//url”);
// assign the value for xpath
XmlNode nodevar=xmldoc.SelectSingleNode(“/var1:xmlnode1/var1:xmlnode2”,nm);
Nodevar.InnerText=this.Context[“DataSource1.csvfile#csv.columnname”].ToString();
Request1Body.BodyString=xmldoc.InnerXml;
Request1.Body=request1Body;