Wednesday, March 21, 2018

LeanFT API REST Automation Testing for HTTP method - GET/POST/PUT/DELETE

LeanFT API Automation Testing for HTTP Method


01 - Call to web service and verify the response (check the response code is equal to 200)


Explanation of parameter values



 * @ url  - url of the service
  * 
  * @ method - Request method GET/POST/PUT/DELETE
  *  
  * @ header  - headers to be added to the request
  * 
  * @ parameter - Required parameter /parameters 
  * 
  * @inputStrinf - reqired for POST/PUT/DELETE
  *  
      * @username - user name if the authentication type is not NoAuth
      * 
  * @password- password if the authentication type is not NoAuth
  * 
  */

Method Implemented 



public final int callHttpWebService(String url,
            String method,
            String headers,            
            String parameters,
            String inputString,
            String username,
            String password
            ) throws Exception {
           
   
   
    boolean isAssert = true;
        String step = String.format("CALL HTTP WEB SERVICE : URL : %s ", url);
        String result = "FAILED";  // Default value
        String message = "CALL HTTP WEB SERVICE with headers : (%s), parameters : (%s) and auth %s "; // Default value
        int ResponseCode = 0;
        if (method.toUpperCase() == "GET")
    {
    HttpWebService request = new HttpWebService();
    ResponseCode = request.HttpURLConnectionRunner(url, headers, parameters,username,password);
    }
        else if (method.toUpperCase() == "POST" || method.toUpperCase() == "PUT" )
        {
        HttpWebService request = new HttpWebService();
    ResponseCode = request.testpost(url,method.toUpperCase(),headers,parameters,inputString,username,password);
        }
        
   
    if(ResponseCode == 200)
    {
    result = "PASSED";
        message = "callHttpWebService success with responseCode "+ResponseCode;
        message = String.format(message, headers, parameters);
    }
    else
    {
    result = "FAILED";
            message = "callHttpWebService Failed with responseCode "+ResponseCode;
            message = String.format(message, headers, parameters);
    }
    //Pass value to report
    reportresult(isAssert, step, result, message);
    //String[] myStringArray = {url,method,headers,parameters,"+ResponseCode: "+ResponseCode};
    return ResponseCode;
   
   
   
    }

02 - Store the response text.


Explanation of parameter values



 @ url  - url of the service
  * 
  * @ method - Request method GET/POST/PUT/DELETE
  *  
  * @ header  - headers to be added to the request
  * 
  * @ parameter - Required parameter /parameters 
  * 
  */

Method Implemented 



 public final String StoreHttpResponse(
    String url,
            String headers,            
            String parameters,
            String username,String password
            ) throws Exception {
   
    HttpWebService request = new HttpWebService();
    int ResponseCode = request.HttpURLConnectionRunner(url,headers,parameters,username,password);
    String ResponseText = "";
    boolean isAssert = true;
        String step = String.format("STORE HTTP RESPONSE : URL : %s ", url);
        String result = "FAILED";  // Default value
        String message = "STORE HTTP RESPONSE : (%s), parameters : (%s) and auth %s "; // Default value
        String errorMsg = "%";
        
            if(ResponseCode == 200)
    {
            ResponseText = request.getresponse(url,headers,parameters,username,password);
            result = "PASSED";
                message = "StoreResponse successful. ResponseText is "+ResponseText;
                message = String.format(message, headers, parameters);
                       //Pass value to report
                reportresult(isAssert, step, result, message);
                return ResponseText; 
    }
            else
            {
            return null;
            }
               
    }

03 - verify the requested JSON value (according to the user given path) an given expected value 


Explanation of parameter values


  * @ JsonValueToVerify - JSON string path 
  * supportable for Json Key and Array value 
  * @ ExpectedValue - Expect value for verification 
  * 
  * @ url  - url of the service
  * 
  * @ header  - headers to be added to the request
  * 
  * @ parameter - Required parameter /parameters 
  * 
  * @username - user name if the authentication type is not NoAuth
          *
  * @password- password if the authentication type is not NoAuth
*/


Method Implemented 



 public final void verifyHttpResponseText(String JsonValueToVerify,String ExpectedValue,
    String url,
            String headers,            
            String parameters,
            String username,String password
            ) throws Exception {
   
    boolean isAssert = true;
        String step = String.format("VERIFY HTTP RESPONSE TEXT : URL : %s ", url);
        String result = "FAILED";  // Default value
        String message = "VERIFY HTTP RESPONSE TEXT : (%s), parameters : (%s) and auth %s "; // Default value
        HttpWebService request = new HttpWebService();
        
        int ResponseCode = request.HttpURLConnectionRunner(url,headers,parameters,username,password);
        
        if(ResponseCode == 200)
    {
        String Response  = request.VerifyresponseText(JsonValueToVerify,ExpectedValue,url, headers, parameters,username,password);
                
        if(Response.trim().toString().equals(ExpectedValue))
    {
            result = "PASSED";
                message = "Verify Text successful. Expected Value: "+ExpectedValue+" Actual Value : "+Response;
                message = String.format(message, headers, parameters);
                       //Pass value to report
                reportresult(isAssert, step, result, message);
                
    }
            else
            {
            result = "FAILED";
                message = "Expected Text not matched with  Expected Value: "+ExpectedValue+" Actual Value : "+Response;
                message = String.format(message, headers, parameters);
                       //Pass value to report
                reportresult(isAssert, step, result, message);
            }
    }
               
    }

}

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

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 ...