Wednesday, March 21, 2018

QTP/UFT API testing for REST - GET/POST/PUT/DELETE

QTP/UFT API testing 

XML base API respond testing 

1. HTTP Method  - GET

parameters need to passed .

Description  - 
1.Creat a object from "MSXML.DOMDocument
2.Select the node by passing element path 


' Get a XML string , XML string path, and a expected value and validate expected value with the     value with given path's value<br>
    ' Returs void<br>

     ' @param URL
     '            : url of the service

     ' @param HTTPMethod
     '            : Request method GET/POST/SOAP
     ' @param parameters
     '            : request parameters

     ' @param elementPath
     '            : XPATH for xml node

      '@param expectedVal
      '           : String expected value

      '@return void

Command for read XML response and verify the value with given value by user 

public Function CheckXMLValue(URL,HTTPMethod,parameters,elementPath,expectedVal)

Dim xmldom,intlen,x,AttName,restReq,result,FullURL
Set xmldom = CreateObject("MSXML.DOMDocument")

xmldom.async = "False"

   If UCase(parameters) <> "NULL" Then
       FullURL = URL&"?"&parameters
       Else
           FullURL = URL
   End If

    xmldom.Load(FullURL)

    Set nodelist = xmldom.selectNodes(elementPath)

        If nodelist.length>0 Then
            For each x in nodelist
                AttName = x.Text
                If AttName = expectedVal Then
                // Passed to the report
  
                Else
                // Passed to the report
  
                End If
  
        Next
       Else
        // Passed to the report
        End If

      
End Function



JSON string base API respond testing 

parameters need to passed .

   ' Get a JSON text ,element path, and a expected value and validate expected value with the value with given path's value<br>
     ' Returs void<br>
     ' @param URL
     '            : url of the service
     ' @param HTTPMethod
     '            : Request method GET/POST/SOAP
     ' @param parameters
     '            : request parameters
     ' @param async
     '            : False / True
     ' @param userName
     '            : user name if reqired login
     ' @param password
     '            : password if reqired login
     ' @return true, if check element present

      '@param expectedVal
      '           : String expected value

      '@return void

Command for read JSON response and verify the value with given value by user 

Public Function CheckJSONValue(URL,HTTPMethod,parameters,async,userName,password,elementPath,expectedVal)

    'Dim restReq,url,html,window,ParseJson,result
    Dim html,restReq,window,ParseJson,nodePath
    Set restReq = CreateObject("Microsoft.XMLHTTP")
    If UCase(parameters) <> "NULL"  Then
        url = URL&"?"&parameters
        Else
        url = URL
    End If

        if(UCase(userName) <> "NULL" or UCase(password) <> "NULL"Then

         restReq.open UCase(HTTPMethod), url, false, userName, Password
         Else
             restReq.open UCase(HTTPMethod), url, false
         End If
        restReq.send
        If restReq.ResponseText <> "" Then
            Set html = CreateObject("htmlfile")
            Set window = html.parentWindow
            window.execScript "var json = " & restReq.ResponseText, "JScript"
            Set ParseJson = window.json
            nodePath="ParseJson."&elementPath
            Dim oNodePath
            oNodePath = eval(nodePath)
            result =  oNodePath
            If result = expectedVal Then
           // Passed to the report
            Else
              // Passed to the report
            End If
        Else
    // Passed to the report
  
    End If
        'results.[0].address_components.[0].long_name
End Function


2.HTTP Mehode - POST /PUT/DELETE


Public Function cCallWebService(URL,HTTPMethod,input,async,requestHeader)

    
    If Ucase(HTTPMethod) = "POST" or Ucase(HTTPMethod) = "PUT" Then
   
    Dim splitArray,splitReqHeader,rReqHeader
     Dim rReqHeaderPart1 ,rReqHeaderPart2
    Set objXmlHttpMain = CreateObject("Msxml2.ServerXMLHTTP")  
    objXmlHttpMain.open Ucase(HTTPMethod),URL, async 
   

    splitArray=Split(RequestHeader,"$")   //Content-Type = application/json $ Content-Type = application/json
    arrayL=ubound(splitArray)
    
   If UCase(requestHeader) <> "NULL" Then
   
    For i=0 to arrayL
    splitReqHeader = Split(splitArray(i),"=")  

   
    rReqHeaderPart1  = Trim(splitReqHeader(0))
    rReqHeaderPart2 = Trim(splitReqHeader(1))


    objXmlHttpMain.setRequestHeader rReqHeaderPart1,rReqHeaderPart2

    Next
   
   End If

    objXmlHttpMain.send input
    
      If objXmlHttpMain.status = 200 Then
         // Passed to the report 
      Else
        // Passed to the report 
      End If
      
      ElseIf Ucase(HTTPMethod) = "DELETE" Then
        Call cCallDeleteWebService(URL,HTTPMethod,input,async)
       End If
      

 End Function


// For delete function 
Public Function cCallDeleteWebService(URL,HTTPMethod,input,async)

    Dim longurl
          
    If input <> "NULL" Then
    longurl  = URL&"\"&input
    Else
    longurl  = URL
    End If
   
    Set objXmlHttpMain = CreateObject("Msxml2.ServerXMLHTTP")  
    objXmlHttpMain.open Ucase(HTTPMethod),longurl, async 
    objXmlHttpMain.send
    
      If objXmlHttpMain.status = 200 Then
         // Passed to the report 
      Else
           // Passed to the report 
      End If

 End Function

No comments:

Post a Comment

MS VISUAL STUDIO CODEDUI TEST FRAMEWORK AND TEST EXECUTION

MS VISUAL STUDIO CODEDUI TEST FRAMEWORK AND TEST EXECUTION  1.0 Pre-Requisites for the execution 1.1 Software Requirements ...