loadrunner interface testing: GET, POST (3 types of parameters format), cookie and token processing, encryption interfaces, webservice, socket, file upload interface, a file download interface & Chinese garbled treatment
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 = \" " , ". 1 = Ord" , 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 function returns a value == 0, they are equal if (strcmp (lr_eval_string ( "{ msg}"), "success") == two strings 0) {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", "URL=http://localhost:8080/pinter/com/register", "Body={\"userName\":\"test\",\"password\":\"1234\",\"gender\":1,\"phoneNum\":\"110\",\"email\":\"[email protected]\",\"address\":\"Beijing \ "}" , LAST); utf-8 format data // the server returns from utf-8 is converted to 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 is successful") == 0) {
the lr_end_transaction ( "POST-2" , LR_PASS); the else} {the 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, 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
#include "lrs.h"
Action()
{
lrs_set_recv_timeout2(0,0);
lr_start_transaction("tcp");
// 创建一个tcp链接
lrs_create_socket ("socket0", "TCP", "RemoteHost=127.0.0.1:8888", LrsLastArg);
// 发送数据
lrs_send ("socket0", "buf0", LrsLastArg ); // 接受数据 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