Tuesday, April 8, 2014

REST WebService in JAVAprotocol

import lrapi.lr;
import java.io.*;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class Actions
{
    private String xmlStr = null;
    private String finalFileName = null;
    private String xmlDocument = null;
    private String Formatted_msg = null;

public int init() throws Throwable {
return 0;
}//end of init


public int action() throws Throwable {
   FileReader fr = null;
   File f=new File("SalesTxn.xml");

   int fileLength = (int) f.length();
   char[] cbuf = new char[fileLength];

 
   try {
     fr = new FileReader(f);
     fr.read(cbuf);
   } catch (FileNotFoundException e) {
     lr.error_message("Could not find data file: " + f);
     lr.abort();
   } catch (IOException e) {
     lr.error_message("Error reading file: " + f);
     lr.abort();
   } finally{
   try{
if(fr!=null){
   fr.close();
}
   }catch(IOException ioe){
     lr.error_message("Error closing file reader: " + ioe);
     lr.abort();
   }
}

 
   new String(cbuf);
            String xmlDocument = String.valueOf(cbuf);
      xmlDocument = xmlDocument.replace("<Body>TESTING</Body>","<Body>TEST<pBody></Body>");
            Formatted_msg =  xmlDocument;
//    System.out.println("Formatted_msg:\n" +Formatted_msg);

       try {
            String request="https://webapps-qa.homedepot.com/MMCJECCInterface/rest/eccInterfaceServices/sendMessageDetails";
            URL url = new URL(request);
            URLConnection conn = url.openConnection();

   conn.setDoOutput(true);
   conn.setRequestProperty("Content-Type", "application/xml");
   OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
           
lr.start_transaction("SOCC_01_ProText_ECC_01_Request");
   writer.write(Formatted_msg);
   writer.flush();
 
   String line;
   BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
 
   while ((line = reader.readLine()) != null) {
// System.out.println("Sales Response:"+ line);
if (line.contains("SUCCESS")) {
   lr.end_transaction("SOCC_01_ProText_ECC_01_Request", lr.PASS);
}else{
   lr.end_transaction("SOCC_01_ProText_ECC_01_Request", lr.FAIL);
}
   }
   writer.close();
   reader.close();        


           //System.out.println("\nDone");

        } catch (Exception e) {
            e.printStackTrace();
        }

//       System.out.println("\nDone");

return 0;
}//end of action


public int end() throws Throwable {
return 0;
}//end of end
}

Monday, September 23, 2013

Webservices in HTTP

Action()
{
    web_add_header("SR_API_KEY","xxx-4c88-8ddc-31cfaa113735");
    web_set_sockets_option("INITIAL_BASIC_AUTH","1");
    web_set_user("xxx_inqaaccess", "xxxYUi987", "axxtindex-xxb-perf.test.xxx.com");
   
    web_reg_save_param("Response",
        "LB=",
        "RB=",
        LAST);

 lr_start_transaction("retrievePolicyIdByPolicyNum");

 web_custom_request("retrievePolicyIdByPolicyNum",
 "Method=POST",
 "Mode=HTML",
 "RecContentType=text/xml",
 "EncType=text/xml; charset=utf-8",
 "URL=http://agrmtindex-esb-perf.test.xxx.com/AgreementInquiry/",
  "Body=<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.AgreementInquiry.dc3.sf/\n">
   "<soapenv:Header/>\n"
   "<soapenv:Body>\n"
   "<ser:retrievePolicyIdByPolicyNum>\n"
   "<policyInput>\n"
   "<agreSrcSysCd>1</agreSrcSysCd>\n"
   "<agreementAccessKey>34349</agreementAccessKey>\n"
   "<productLine>L</productLine>\n"
   "</policyInput>\n"
   "</ser:retrievePolicyIdByPolicyNum>\n"
   "</soapenv:Body>\n"
   "</soapenv:Envelope>\n"
 "LAST");

   lr_end_transaction("retrievePolicyIdByPolicyNum", LR_AUTO);
   lr_output_message("***Response is : \n%s", lr_eval_string("{Response}"));
   lr_output_message("***");
   return 0;
}

Thursday, April 19, 2012

REST Service - POST

This LoadRunner script can be used to POST 'register a new user'

Action()
{
char *request_json_base;
char *request_json;

// save web service url to param {URL}
char *URL = https://xxx.xom.xxx/;
lr_save_string(URL, "URL_Param");

request_json_base= "userId=PerfTestUser12@gmail.com&"
       "
password=Pwd@123&"
       "
passwordVerify=Pwd@123&"
       "firstName=firstNamexx&"
       "lastName=lastnamexx&"
       "storeId=99999&"
       "zipCode=28117";


request_json = lr_eval_string(request_json_base);
lr_save_string(request_json, "REQUEST_JSON_PARAM");


// set http headers
web_add_header("Accept", "application/json");
web_add_header("Content-Type","application/x-www-form-urlencoded");

// validate response
web_reg_find("Text=success", LAST);


// send JSON request
lr_start_transaction("UE1_01_POST_Register_New_User");

web_custom_request("post",
  "URL={URL_Param}",
  "Method=POST",
  "TargetFrame=",
  "Resource=0",
  "Referer=",
  "Mode=HTTP",
  "Body={REQUEST_JSON_PARAM}",
  LAST);


lr_end_transaction("UE1_01_POST_Register_New_User", LR_AUTO);
return 0;
}

Monday, April 16, 2012