Pages

Automate SOAP UI Using QTP

Greetings!
This post is all about automating the request and responses on soap UI.
This piece of code can help the QTP professsionals to automate the Soap UI part.
How it works? (Read it as steps:)
                1) "call_service_soap_ui" is the function that is called.
                2) Manual:         We generally use a request xml in soap ui. Here we modify the values according to our requirements. This is based on the filteration criteria.
                    Automation: This part of the xml is extracted and the tags and values are edited . These values are passed in the input parameters of the above said function.
                   The function that picks/ extracts the required part of request xml is "get_xml_request_string".
                   This need not be called explicitly. It is already called from "call_service_soap_ui"
                3) Manual:          Once edited we run it on the SOAP UI to get the request xml part.
                     Automation: The same function does it.
Note:
                1) The image available on this page can help you understand from where you can get the soapaction string and other tags and values. Remember to add a tag value pair only if you have a value to filter.
                2) In This line "Set NodeList1 = Root.getElementsByTagName("con:request") ", "con:request" should not contain "<" or ">" symbols.
                This should be the name of the node that contains the xml text used as request xml on the UI.
                3) This block of code can be used as it is . Only the text highlighted in RED font need to be replaced with valid values based on your project.
‘************************************************************************************
‘Created By:                       Lakshmi
‘Input Parameters:          Servicename - This should be the name of the service
                                               tags- list of tags based on which filteration needs to be done.
                                               These should be seperated by a ","(coma)
                                               values   - list of values for the above tags seperated by coma.
                                               These should be in the same order as the tags mentioned above.
                                               These tag value pairs are nothing but the filteration criteria.
‘Output Parameters:      N/A
‘************************************************************************************
Function call_service_soap_ui(servicename,tags,values)
requestfile="C:\Request.xml" ‘<File path of the request xml of the project>
requeststring=get_xml_request_string(requestfile,servicename) ‘ this will fetch only the particular request xml text part
If tags<>"" and values<>""Then
                Dim tag_s,valu_s
                tag_s=split(tags,",")
                valu_s=split(values,",")
                For i=0 to ubound(tag_s)
                requeststring=replace(requeststring,tag_s(i) & ">?</" & tag_s(i),tag_s(i) & ">" & valu_s(i) & "</" & tag_s(i))
                Next
End If
Dim sWebServiceURL, sContentType, sSOAPAction, sSOAPRequest
Dim oDom, oXmlHttp
Dim sResponse
sWebServiceURL =Environment.Value("webserviceurl")
sContentType ="text/XML" ‘Web Service Content Type
sSOAPAction = "<soap action name without the service name... see second image to understand from where you can get this value>" &  servicename
‘Request Body
sSOAPRequest =requeststring
‘Create objects to DOMDocument and XMLHTTP
Set oDom = CreateObject("MSXML2.DOMDocument")
Set oXmlHttp = CreateObject("MSXML2.XMLHTTP")
‘Load XML
oDom.async = False
oDom.loadXML sSOAPRequest
‘Open the webservice
oXmlHttp.open "POST", sWebServiceURL, False
‘Create headings
oXmlHttp.setRequestHeader "Content-Type", sContentType
oXmlHttp.setRequestHeader "SOAPAction", sSOAPAction
‘Send XML command
oXmlHttp.send oDom.xml
‘Get XML Response
sResponse = oXmlHttp.ResponseText
‘Close object
Set oXmlHttp = Nothing
Dim objFSO,outFile,objFile
Set objFSO=CreateObject("Scripting.FileSystemObject")
‘ How to write file
outFile="C:\Response.xml"‘ this should be the path of the output file.
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.Write sResponse
objFile.Close
End Function
‘************************************************************************************
‘************************************************************************************
‘Created By:                       Lakshmi
‘Input Parameters:          Servicename - This should be the name of the service
                                               filepath                - list of tags based on which filteration needs to be done.
                                               These should be seperated by a ","(coma)
‘Purpose of the function:
                                               - This function is used to get the content of the node that contains the specific tagname (Eg: <con:request> is used in this example) .
                                               -  This is the node that contains the request xml data code/data see first image to understand
                                               - Once these nodes are fetched in an array, they are iterated to find the node containing this particular servicename.
                                               - The content of that specific node is returned as the return value (Output parameter) of the function.                                                                                                                                             
‘************************************************************************************
Function get_xml_request_string(filepath,servicename)
                Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
                objXMLDoc.async = False
                objXMLDoc.load(filepath)
               
                Set Root = objXMLDoc.documentElement
                Set NodeList1 = Root.getElementsByTagName("con:request")
                arsize=NodeList1.length-1
                For i=0 to arsize
                                If instr(NodeList1(i).nodetypedvalue,"<v1:" & servicename & ">")>0 Then
                                                                get_xml_request_string= NodeList1(i).nodetypedvalue
                                                                Exit for
                                End If                   
                Next
End Function

3 comments:

  1. You can find the soap action name in the area where you find "This is soap action" in bold and red color in the picture. Pick that value and use it in your code.

    ReplyDelete
  2. Is it required to create new test in 'API Test' to write this code?. Please let me know

    ReplyDelete
  3. How can I pass .pfx Client certificate along with Soap Request?

    ReplyDelete