Performance testing to learn the next day ----- loadrunner Daquan commonly used functions and interface testing

Daquan commonly used functions:

 

1, C language web transfer parameters Parameter
  lr_save_string ( "aaa", "paramthe string "aaa" or a string variable, a parameter LR into param} {
  lr_save_int (123, "param"); digital 123, into the LR parameter param

2, web transfer parameter C language parameter
  lr_eval_string ( "{param}")the value of the parameter extracted in the {param}, other functions may be nested in the

3, string manipulation functions
  strcmp: string comparison functions, if the two values are equal, returns 0
  strcpy: string copy function, can copy a string to a character array
  strcat: string concatenation function, it can be a string concatenation to a character array

4, code conversion function
  lr_convert_string_encoding (lr_eval_string ( "{msg}
  the LR parameters {msg}, conversion from utf-8 encoded system_local coding (LR local encoding), and finally LR saved to the parameter {afterEncodeMsg}

5, the operating parameter array
  lr_paramarr_random ( "param_arry"): param_arry randomly from a parameter array value, does not require additional attention param_arry} {
  int size;
  size = lr_paramarr_len ( "param_arry"): Get the length of the parameter array param_arry and save variable size to the C language's
  int value;
  value = lr_paramarr_idx ( "param_arry",. 1): obtaining a first parameter array param_arry in value, and stores the value in the C language variables

. 6, the web_url () : GET interface, usually generated by recording

. 7, web_custom_request () : POST interface, test http webservice interface or interfaces, body string is json

8, web_submit_data () : POST interface, you can test file upload type interface. Suitable functions get and post requests, mainly for submission form form for more post, and is not a context dependencies

      web_submit_form functions: data submitted, but require a context-dependent

. 9, atoi (lr_eval_string ( "{param}")) : The parameters {param} LR is converted into digital

10, web_reg_save_param : correlation function, before the request must be written, to find the appropriate value based on the value of the left and right boundary, with strcmp () using

11, web_reg_find : text checkpoint function, must be written before the request, the text of the page to check whether there is a pre-registration function, must be placed before the request. Fitting 5. The atoi () using

12, the log function

  lr_error_message ( "UserSession:% S", lr_eval_string ( "UserSession {}")); // check whether the acquired value, error marked red print debugging easy viewing.

    lr_output_message ( "login is successful, the user name:% s", lr_eval_string ( " {username}")); // print log function 

    lr_log_message () print log information

13, web_save_timestamp_param : to save the current timestamp as a parameter

14, web_add_cookie function: new cookie

15, web_add_header function, the new header information

16, lr_start_transaction ( "login"); // Name the transaction, beginning of an operation, plus before signing request

   the lr_end_transaction ( 'login', LR_AUTO);

  That added if statement assertions:

  if(atoi(lr_eval_string("{User_Count}"))==1) {

    lr_end_transaction("登录",LR_PASS); }

  else {

    lr_end_transaction("登录",LR_FAIL);

  }

loadrunner interface testing:

1.get request

The Action () 
{
     // Start transaction - GET request 
    lr_start_transaction ( " GET " );

     // correlation function returns information obtaining interface 
    web_reg_save_param ( " MSG " ,
         " an LB = Message \": \ " " ,
         " the RB = \" " ,
         " Ord =. 1 " , 
        LAST);

     // GET request 
    the web_url ( " GET " , 
     " the URL = HTTP: // localhost: 8080 / Pinter / COM / getSku ID NUM = {}? " , 
    LAST);

    // strcmp function used to determine whether the two strings are equal, if the return value of the function ==0, representing the two strings are equal
     IF (strcmp (lr_eval_string ( " {} MSG " ), " Success " ) == 0) {         
        the lr_end_transaction ( " GET " , LR_PASS); 
    } the else {         
        the lr_end_transaction ( " GET " , LR_FAIL) ; 
    } 
    return 0; 
}

2.post request - in the form of parameters in key = value

Action()
{    
    lr_start_transaction("post-1");
web_reg_save_param(
"msg", "LB=message\":\"", "RB=\"", "Ord=1", LAST); web_custom_request("post-1", "Method=POST", "URL=http://localhost:8080/pinter/com/login", "Body=userName=admin&password=1234", LAST ); if (strcmp(lr_eval_string("{msg}") ,"success") == 0) { lr_end_transaction("post-1", LR_PASS); }else{ lr_end_transaction("post-1", LR_FAIL); } return 0; }

3.post request - in the form of parameters json

Action()
{    
    lr_start_transaction("post-2");

    web_reg_save_param("msg",
        "LB=message\":\"",
        "RB=\"",
        "Ord=1",
        LAST);

    // json接口都需要添加一个content-type的信息头
    web_add_header("Content-type","application/json");

    web_custom_request("post-2", "Method=POST" , 

    " The URL of = HTTP: // localhost: 8080 / Pinter / COM / the Register " , 

    " Body = {\ "userName \": \ "the Test \", \ "password \": \ "1234 \", \ " gender \ ": 1, \" phoneNum \ ": \" 110 \ ", \" email \ ": \" [email protected] \ ", \" address \ ": \" Beijing \ "} " , 

    LAST );

     data utf-8 format // the server returns from UTF- . 8 is converted to the system_locale, and saves the converted data to a new web parameters afterMsg 
    lr_convert_string_encoding (lr_eval_string ( " {} MSG " ), LR_ENC_UTF8, LR_ENC_SYSTEM_LOCALE, " afterMsg " ); 
IF (strcmp (lr_eval_string ( "afterMsg} { " ), " registration successful " ) == 0) {
lr_end_transaction(
"post-2", LR_PASS); }else{ lr_end_transaction("post-2", LR_FAIL); } return 0; }

4.post request: parameter format of data = {}

Action()
{
    lr_start_transaction("post-3");

    web_reg_save_param("msg",
        "LB=message\":\"",
        "RB=\"",
        "Ord=1",
        LAST);
web_custom_request(
"post-3", "Method=POST", "URL=http://localhost:8080/pinter/com/buy", "Body=param={\"skuId\":123,\"num\":10}", LAST ); if (strcmp(lr_eval_string("{msg}") ,"success") == 0) { lr_end_transaction("post-3", LR_PASS); }else{ lr_end_transaction("post-3", LR_FAIL); } return 0; }

The interface requires cookie

 Two solutions:

  A program: loadrunner automatically processing Cookie, just in front of the interface to increase log, subsequent requests will automatically bring Cookie;

  Scheme II: cookie acquired by capture, stored in the parameter file, subsequent requests to increase web_add_cookie (), increased cookie parameter information.

6. The need for token interface

  After obtaining the token by capture, stored in the parameter file, subsequent requests to increase web_add_header (), increasing the token parameter information.

7. The encryption interface , such as MD5

   1. loadrunner script needs to call an external function (CMd5)

  1. Add the .h files to an external function in LR

  2, introduced in globals.h add file header files, such as #include "md5.h"
  3, the script can directly call the corresponding function, need to pay attention, C language is C language function returns the parameters, if want to
  use the web function LR, it is necessary to convert

   2.例如:  {"phoneNum":"123434","optCode":"testfan","timestamp":"1211212","sign":"fdsfdsaafsasfas"}

  Wherein, Sign data field is encrypted according to a particular algorithm, the signature algorithm interface is: sign = Md5 (phoneNum + optCode + timestamp), the signing process involves splicing of C language function strings

str char [50 ]; 
web_save_timestamp_param ( " tstamp " , LAST); // save the current timestamp Memset (str, 0, the sizeof (str)); // Clear str array, the memory reset, starting from 0 to the maximum length of the array . If it does not clear, many times the cumulative run time will str
strcat (STR, " 2009/123434 " ); // string concatenation 
strcat (STR,
" testfan " );
strcat (STR,
" tstamp " );

lr_save_string (CMD5 (STR),
" md5sign " ); // C language conversion parameters type parameters for the web
web_add_header("Content-type","application/json");
web_custom_request("sign", 
"Method=POST",
"URL=http://localhost:8080/pinter/com/buy",
"Body={\"phoneNum\":\"123434\",\"optCode\":\"testfan\",\"timestamp\":\"{tStamp}\",\"sign\":\"{md5sign}\"}",
LAST );

8.webservice Interface:  You can use direct http request to test the protocol post

   Use web_custom_request function: http: //ws.webxml.com.cn/WebServices/MobileCodeWS.asmx wsdl?
   Interface Description: http: //ws.webxml.com.cn/WebServices/MobileCodeWS.asmx
   points

       1, Content-added of the type header: text / xml
       2, the request message to xml format, directly on the body, the message content can be viewed interface documentation, or import wsdl address soupUI tool, you can see the request message
       3, bejson compression using a request message, to quote escaping;

        

       4, using the function web_custom_request

Action()
{
    web_add_header("Content-type","text/xml");

    web_custom_request("webservice", "Method=POST", 
    "URL=http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx", 
    "Body=<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\"><mobileCode>139{num}</mobileCode><userID></userID></getMobileCodeInfo></soap:Body></soap:Envelope>", 
    LAST );
    return 0;
} 

 9.socket Interface

# The include "lrs.h" 
Action () 
{ 
    lrs_set_recv_timeout2 (0, 0); 
    
    lr_start_transaction ( " tcp " );

     // create a tcp link 
    lrs_create_socket ( " SOCKET0 " , " TCP " , " RemoteHost = 127.0.0.1: 8888 " , LrsLastArg);

     // send data 
    lrs_send ( " SOCKET0 " , " BUF0 " , LrsLastArg);

     // accept data 
    lrs_receive ( " SOCKET0 " ,"buf1", LrsLastArg );

    // 关联函数
    //lrs_save_param ("socket0",NULL,"RecivedData", 3,7);
    
    lrs_save_searched_string("socket0",NULL,"RecivedData","LB/BIN=|",NULL, 2,2,2);

    if (strcmp(lr_eval_string("<RecivedData>") , "SUCCESS") == 0) {        
        lr_end_transaction("tcp", LR_PASS);
    }else{        
        lr_end_transaction("tcp", LR_FAIL);
    }

    // 关闭链接
    lrs_close_socket ("socket0");
    return 0;
}

10. File Upload Interface

Action()
{    
    web_reg_save_param("msg",
        "LB=",
        "RB=",
        "Ord=1",
        "Search=Body",
        LAST);

    //web_add_header("Content-type","multipart/form-data");
    web_submit_data("Attachments", 
    "Action=http://localhost:8080/pinter/file/api/upload", 
    "Method=POST", 
    "EncType=multipart/form-data", 
    "TargetFrame=", 
    "RecContentType=text/html", 
    "Snapshot=t5.inf", 
    "Mode=HTML", 
    ITEMDATA, 
        "Name=file", 
        "Value=C:\\username.dat", 
        "File=yes", 
    ENDITEM, 
    LAST ); 
    return 0;
}

11. File download interface

Action()
{
    int size;
lr_start_transaction(
"download");
web_url(
"get", "URL=http://localhost:8080/pinter/file/api/download?id={fileId}", LAST );
  size
= web_get_int_property(HTTP_INFO_DOWNLOAD_SIZE); lr_output_message("下载的字节数总大小为:%d", size); if (size > atoi(lr_eval_string("{filesize}"))) { lr_end_transaction("download", LR_PASS); }else{ lr_end_transaction("download", LR_FAIL); } return 0; }

 

Chinese garbled treatment

Record generated script is garbled: - Set -Option-Advance- check utf-8 when recording
◼ interface has returned data is garbled, because the domestic server uses UTF-8 encoding, but their use is GBK loadrunner Encoding.
  Use lr_convert_string_encoding do transcoding
  lr_convert_string_encoding (lr_eval_string ( "the recv {}"), LR_ENC_UTF8, LR_ENC_SYSTEM_LOCALE, "afterEncode");
. 1, submitted from the client to the server Chinese distortion
  LR_ENC_SYSTEM_LOCALE → LR_ENC_UTF8
  Note: Chinese after data conversion, use strcpy removing the tail function empty string
2, Chinese distortion received from the server
  LR_ENC_UTF8 → LR_ENC_SYSTEM_LOCALE



Guess you like

Origin www.cnblogs.com/qingyuu/p/11209203.html