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);
            }
    }
               
    }

}

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