Friday, January 27, 2012

QTP–XML Webservice Testing – Automation Frame work

 
Pre Request – Knowledge on Webservice, QTP, XML & Xpath
 
In the web world there are lot of free tools available for posting XML request to webservice. But nothing has option to verify the response XML and compare against the Excepted result. It's always difficult when you have huge number of scripts to test. Believe it or not using this automation frame work my execution time is reduced from days to an hour.


In this I am going to provide the sample codes to create the Data driven frame work for testing Webservice with XML request. From this it will be very easy to develop your own framework based on your requirements.

1. Webservice Testing

Request XML or XML with SOAP or Json request will be posted to Big Web Services or RESTful Web Services. The output response will be received as response text or http status. For Webservice testing using XML, you might be posting different set of request xml to suite the condition and verifying the response xml.

2. Automation steps 

Below is the order in which automation execution steps are carried out.
 
Preparing Input Request File
From the input sample XML update the dynamic values like Email, Password, etc. before Posting XML. You can use XML DOM concept to update attribute/tag values in the XML. But it will complicate as you need to write the code for each different XMLs. Best option would be to open XML file as string and replace value with dynamic value and save it in a new file. In the sample XML provide the value as {EmailId} or {Password}, so this can be easily replaced. Also the size of the XML file is not more than 2 Kb.

Preparing Output Response File (Expected response file)
Use the above approach to prepare the Output response file.

Posting XML
Post the prepared XML using XMLHttpRequest method. In the coding section I have covered more in detail.

Save the response file (Actual response file)
Save the Response text in a new file.

Verify the response
Actual response XML can be compare against the expected response file or Using Xpath only few attibutes/tags can be verified in the response. In the coding section I have covered more in detail.
 
3. Folder Structure

Below is the simple folder structure for the framework,
  1. Data - Folder contains data sheet & sample XML file
  2. Driver -QTP scripts are stored
  3. EnvVariable - QTP environment values are stored in the xml file
  4. Functions - Vbscript function supports for QTP actions are stored as vbs file
  5. OR - Any Object properties required for browser actions are stores as tsr file.
  6. Results - HTML Reported & XML files are stored
Before start writing the code lets discuss about the data sheet. Once you define the data sheet how it will be going to look like, then it will be very easy to write the code.


4. Storing Data 

Store the Execution environment (QA, UAT, etc..) details like Webservice URL, http authentication user credentials, etc which are different for each environment (QA, UAT, etc..) as QTP environmental variable or in the Data sheet. If you are going with data sheet approach, then create the separate sheet for that with all the details. I will leave this here to use the convenient one.
 
Preparing Data sheet
Create Sheet in data file with the test script details, here is the one of the sample and name the sheet name as TSData,
 

Create a data sheet called TSxmlData to put all the details request to XML data. here is the sample one I used,
 
Input_XML - Request XML if it's small or save the xml file and provide the file name
Input_XML_Changes - Dynamic data or Static data needs to be changed in the input request XML
Output_XML - Response XML if it's small or save the xml file and provide the file name (optional value)
Output_XML_Changes - Dynamic/Static data to be changed in the input request XML (optional value)
ExpectedResult - Compare the OutputXML with Expected XML (or) verify the few values in the XML against the Xpath
Store - Values needs to be stored from the response for further user. (ex - User Id got after registration can be used during login request)
 

Create another Data sheet if you are going to verify the Expected request with against the Xpath value. XML Spy is good tool to edit XML and get Xpath value. Sample sheet looks like this,




5. Sample Scripts

For retrieving data from TSxmlData sheet I prefer opening the Excel sheet with DB connection. For single test script if you need to post more than one request, then this method will be very useful.


DB connection is created, number of test script for the given Sno is returned, all the column values are stored in the array "sXMLData"


Set DBConnection = CreateObject("ADODB.Connection")
' Set the Path
DBPath = "Excel File Path"
DBConnectionString = "Data Source=" & DBPath & ";Extended Properties=Excel 8.0;"
'Establish the connection to the DB
With DBConnection
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = DBConnectionString
.Open
End with
SQLToExecute= "SELECT COUNT(Sno) FROM [TSxmlData$] WHERE Sno='"& ""&"'"
'Execute the query to get the no of TC to execute
Set DBRecordSet = DBConnection.Execute(SQLToExecute)
PL1DBResult = DBRecordSet.Fields.Item(0)
'Query to return the values for the TC
SQLToExecute="SELECT * FROM [TSxmlData$] WHERE Sno='" & Environment.Value("sSno") &"'"
Set DBRecordSet = DBConnection.Execute(SQLToExecute)
DBRecordSet.MoveFirst
For DBLoop=1 to PL1DBResult
For i=0 to 7
If DBRecordSet.Fields.Item(i) <>" NULL" Then
sXMLData(i+1)=DBRecordSet.Fields.Item(i)
Else
sXMLData(i+1)=""
End If
Next
'''''
Next

Below code Opens the Sample XML file, modifies the value and save the file in different name.

'Open the File and read the content
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(sInputXML, ForReading)
InputFileContent = f.ReadAll
f.close
''''''Loop the value recieved from Excel sheet to change the XML
' Changing the value in the XML
Select Case lcase(Trim(XMLName))
Case "email"
InputFileContent=Replace(InputFileContent,"{Email}",XMLName)
End Select
'Save the modified file as different file
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(sSavedXML , ForWriting, True)
f.Write(InputFileContent)
f.close
set f= nothing
set fso=nothing
Below code Post the XML to the Webservice

  


' Read the request file
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(InputRequest, ForReading)
sInFileContent = f.ReadAll
f.close
Set http = CreateObject("MSXML2.ServerXMLHTTP")
' Set the method, Webservice URL and asynchronous
http.open "POST", sWebserviceURL,False
' Set the Header
http.setRequestHeader "content-type","application/xml"
' Set the HTTPS Authentication encryped in base 64 format
http.setRequestHeader "Authorization", "Basic KJIOJ"
' Post the XML
http.send sInFileContent
' Response Message Recieved
OutputResponse=http.ResponseText
' Save the Resposne File
Set f = fso.OpenTextFile(sOutputXML , ForWriting, True)
f.Write(OutputResponse)
f.close
set f= nothing
set fso=nothing



Below code Compares the Actual Response XML with Expected Response XML. You can compare only the XML structure (Attributes & Elements) or Full XML content (Attributes, Elements & it's value).


Set sXMLdoc1 = XMLUtil.CreateXML()
sXMLdoc1.LoadFile ActualFileName
' Set First document
Set sXMLdoc2 = xmlutil.CreateXML()
sXMLdoc2.LoadFile ExpectedFileName
' Compare the XMLs
If sCompValue="CompareXMLs" Then
'Compares the Attribute, Elements & it's value
sRes = sXMLdoc1.Compare(sXMLdoc2,sCompDoc,micXMLNone+ micXMLAttributes+micXMLCDataSections+micXMLValues)
ElseIf sCompValue="CompareStructure" Then
'Compares only the Attribute & Elements
sRes = sXMLdoc1.Compare(sXMLdoc2,sCompDoc,micXMLNone+micXMLAttributes)
End If
If sRes = 1 then
'Actual XML is matching with Expected XML
Else
'Actual XML is Not matching with Expected XML
End If

Below code opens the response XML as DOM object and verifies the Xpath values.


'Open the resposne xml as DOM object
Set sxmlfile = CreateObject("Microsoft.XMLDOM")
sxmlfile.async = False
sxmlfile.Load (sFileName)
'Set the property as Xpath
Call sxmlfile.setProperty("SelectionLanguage", "XPath")
'Get the Actual Xpath value
Set objNOde = sxmlfile.SelectSingleNode(sXPath)
sReasonCode = objNOde.nodeTypedvalue
'Compare the value with Expected value
If sCheck(i)<>sReasonCode Then
'Fail
Else
'Pass
End If
'Put this in a loop to verify for all the Xpath values


In Case if you want to store any value recieved in the response (like User ID), use the above Xpath concept to retrieve the value and store it in QTP environment variable for further use.

Hope this helps you to develop Automation frame work for Webservice Testing. Any queries related to this, post your comments I will try to help.

Blogger Labels: Webservice,Automation,Frame,Request,Knowledge,Xpath,tools,option,response,scripts,Believe,execution,hour,Data,From,framework,requirements,SOAP,Json,Services,RESTful,output,text,status,suite,Input,File,Email,Password,concept,XMLs,Best,EmailId,Also,size,Post,XMLHttpRequest,method,Save,Actual,Verify,Folder,Structure,sheet,Driver,EnvVariable,environment,Functions,Vbscript,Object,properties,Results,HTML,Once,authentication,user,credentials,Create,script,TSData,TSxmlData,Input_XML,Input_XML_Changes,Dynamic,Static,Output_XML,Output_XML_Changes,ExpectedResult,Compare,OutputXML,Values,registration,tool,Sample,Excel,connection,column,DBConnection,CreateObject,ADODB,Path,DBPath,Source,Establish,Provider,Microsoft,OLEDB,Open,SQLToExecute,SELECT,COUNT,WHERE,Execute,DBRecordSet,Fields,Item,Query,Value,MoveFirst,DBLoop,NULL,Else,Opens,FileSystemObject,OpenTextFile,InputFileContent,ReadAll,Loop,Case,Trim,XMLName,Replace,True,Write,Read,InputRequest,ServerXMLHTTP,False,Header,Authorization,Basic,KJIOJ,Message,OutputResponse,ResponseText,Resposne,Compares,Attributes,Elements,Full,XMLUtil,CreateXML,LoadFile,ActualFileName,ExpectedFileName,CompareXMLs,Attribute,ElseIf,CompareStructure,XMLDOM,Load,Call,SelectionLanguage,SelectSingleNode,Fail,Pass,Hope,sXMLData,setRequestHeader,sCompValue,sRes,sCompDoc,micXMLAttributes,sxmlfile,objNOde,sReasonCode

14 comments:

  1. hi Rajkumar this is mitul and my email is
    mitulmjani@gmail.com
    I just joined new project and here most of all the application has many Request Web forms

    For Exam

    send some input like Address,State,Zip,Financial Institute inorder to find FI institutions means banks based on that request

    so how i am gone the start my Automation
    how do i start my approach on that because Dev is going on

    please reply me at email

    ReplyDelete
  2. Hi Mitul,

    For that you can use QTP or Selenium or any other automation tools. This frame work is for posting the XML to webservice.

    kr
    Raj

    ReplyDelete
  3. Hi RAJ,
    im testing the soap UI through QTP using web service Wizard Im getting http Authentication error .How to pass Http authentication details and i need to pass it before sending the request .

    thanks in advance ,

    Regards,
    Pavan

    ReplyDelete
  4. Hi,

    Any Example to test Restful Webservice in Json format.

    ReplyDelete
  5. HI,

    I need to retrive xml data from database.

    below is my code

    Dim connection, recordset, Executequery, readrecord
    Set connection=createobject("adodb.connection")
    Set recordset=createobject("adodb.recordset")
    connection.open "Driver={Microsoft ODBC for Oracle};Server=scngstg;Uid=ANFGH_APPL;Pwd=aapt654x!q5625;"
    If connection.State=1 Then
    msgbox "connection opened"
    Else
    msgbox "connection failed"
    End If
    'Executequery="select MESSAGE ,MESSAGE_HISTORY_OBJID from PLT_MESSAGE_HISTORY where MESSAGE_HISTORY_OBJID=1281"
    'recordset.Open Executequery, connection
    Executequery="select MESSAGE from ANF_PLT_MESSAGE_HISTORY where MESSAGE_HIISTORY_OBJID=14282 "
    connection.CommandTimeout=60
    set XMLDataObject=connection.Execute(Executequery)

    End Function


    When i exectuing the query i am getting Unspecified error. kindly help me how to retirve xml data from database(coloumns).

    ReplyDelete
    Replies
    1. I don't see anywhere in your code which says you are receiving an XML object or XML data from the database. You are executing a SQL query against the database and that is all you are doing.

      And regarding the general/unknown error, you might need to debug and see in which line it's throwing the exception and try to find probable cause and answers for it. Are you sure you have the right driver for your ODBC? How about the correct connection string?

      Good luck.

      Delete
  6. Hi Raj,

    I am looking for an automation tool to test XML services over HTTPS. I have around 52 XSDs and each having around 800 test cases. Could you please help em to find out a best tool which does not require scripting and able to fire the xml request over the http service and get the response back.

    Regards

    Ambadi

    ReplyDelete
  7. Your information about qtp is really interesting. Also I want to know the latest new techniques which are implemented in qtp. Can you update it in your website?

    ReplyDelete

  8. Thanks for sharing this information. Java is one of the popular object oriented programming language used for many of the multinational corporation. So learning Java Training is really helpful to make a bright future.

    ReplyDelete
  9. Really nice post. SEO is one of the digital marketing techniques used for improve the website ranking in search engine result page. To know more details please call 9003623340.
    Regards..
    SEO Training in Chennai

    ReplyDelete
  10. Really awesome blog. Software testing is a method of executing the application or program with the intent of searching the software errors. Testing Training in Chennai offering this course at reasonable cost.

    ReplyDelete
  11. Hi, Your blog is really very informative and useful for me. Thanks for sharing this valuable blog.
    Regards..
    Unix Training Chennai

    ReplyDelete