Loadrunner simulates JSON interface requests for testing

This article is transferred from: http://blog.sina.com.cn/s/blog_6db2c206010133kn.html

1. Create a loadrunner script

1.Insert - New step -选择Custom Request - web_custom_request

2. Fill in the corresponding parameters

3. Generate the script and modify it as follows (the slash \ translation is required before the quotation marks " in the parameters)

 

Action()

{

web_custom_request("web_custom_request",

"URL=http://urlhead/CpcService.ashx?Method=checkXXXed",

"Method=POST",

"Resource=0",

"RecContentType=application/json",

"Referer=",

"Mode=HTTP",

"EncType=application/json",

"Body={\"user\":{\"uid\":\"C8-9C-DC-70-BD-B2\"}}",

LAST);

return 0;

}

 

4. Capture page return value

返回格式{"IsError":0,"ErrorMsg":"","user_tags":["0"]}

Move the cursor to the top of the script, insert the association point insert-new step-service-web_reg_save_param

At the top of the script, insert and modify the script as follows

 

web_reg_save_param("user_tags",

"LB=user_tags\":[\"",

"RB=\"]}",

LAST);

At the end of the script, add the output log to verify whether the acquisition was successful

lr_message ("user_tags:%s", lr_eval_string("{user_tags}"));

 

5. To automate functional testing of our interface, write the verification script as follows:

if (atoi(lr_eval_string("{user_tags}"))==0)

lr_output_message("user_tags:%s,succeed!",(lr_eval_string("{user_tags}")));

else if(atoi(lr_eval_string("{user_tags}"))==1)

lr_output_message("user_tags:%s,failed!",(lr_eval_string("{user_tags}")));

else

lr_output_message("user_tags:%s,unknow!",(lr_eval_string("{user_tags}")));

 

6. Parameterized scripts

 

Running the script, the important log output is as follows:

Action.c(24): [C8-9C-DC-70-BD-B2]0,succeed!

Action.c(26): [C8-9C-DC-70-BD-B3]1,failed!

 

---------------------------------------END---------------------------------------

 

Second, the difference between web_custom_request and web_submit_data

The web_custom_request method can send POST and GET type requests;

web_submit_data can only send POST type requests;

All requests sent by the web_submit_data method can be implemented using web_custom_request 

web_custom_request can implement requests that web_submit_data cannot. For example, in the case of "query all emails and delete them", we use associations to capture the corresponding identifiers of all emails into an array. If you use web_submit_data to complete this deletion request, it requires a lot of Only one web_submit_data request can be completed, but using web_custom_request can be completed through one request, by writing your own code to spell the Body value of the POST request of the web_custom_request method.

 

1. web_submit_data

The data format submitted in the request: "Name=attribute name,", "Value=attribute value"

E.g:

"Name=username″,"Value=12044″, ENDITEM,

"Name=password″,"Value=123456″, ENDITEM, 

"Name=typeId″,"Value=1″, ENDITEM,

If an attribute you want to submit contains multiple values ​​(such as batch deletion), a single web_submit_data cannot be processed, and can only be processed through multiple web_submit_data.

 

2. web_custom_request

Submitted data (body) format: "Body=attribute name=attribute value&attribute name=attribute value&..."

 

The following is a typical web_submit_data and web_custom_request request. You can see that the data (body) submitted in web_custom_request exists in this way, as follows:

web_submit_data("searchRecvOrgsname",
  "Action=http://{url}/searchRecvOrgsname",
  "Method=POST",
  "TargetFrame=",
  "RecContentType=text/html",
  "Referer=http://{url}/login_wj;jsessionid={jsessionid}",
  "Snapshot=t18.inf",
  "Mode=HTML",
  ITEMDATA,
  "Name=orgsId", "Value={orgsId}", ENDITEM,
  "Name=code", "Value={order_end_station_code}", ENDITEM,
  LAST);

 web_custom_request("searchVehiclePopUp",
  "URL=http://{url}/searchVehiclePopUp",
  "Method=POST",
  "TargetFrame=",
  "Resource=0",
  "RecContentType=text/html",
  "Referer=http://{url}/login_wanjia;jsessionid={jsessionid}",
  "Snapshot=t19.inf",
  "Mode=HTML",
  "EncType=application/x-www-form-urlencoded; charset=UTF-8",
  "Body=&orgsId={orgsId}&order_start_station_id={order_start_station_id}&targetcode=order_truck_no&targetname=order_truck_name&targetid=order_truck_id",
  LAST);

The POST request in two cases will be recorded by LoadRunner as web_custom_request:

1. The above-mentioned batch submission of multiple data requests with the same attribute name
2. The POST request with the header attribute x-requested-by value of XMLHttpRequest

Another thing to note about these two methods of implementing requests is that if the attribute value in the body of web_custom_request contains some special characters, it must be URL-encoded, otherwise the web server will return a 500 error. A typical example is if the body contains ViewState, there are often special characters such as "=" in ViewState, which must be encoded by URL. LoadRunner provides such an encoding conversion function:

web_convert_param(“vs1″, “SourceEncoding=HTML”,  “TargetEncoding=URL”, LAST);

 

3. Detailed explanation of web_custom_request function

A. Syntax:

int web_custom_request( const char *RequestName, ,
[EXTRARES, ,] LAST );

B. Return value: Return LR_PASS (0) for success, LR_FAIL (1) for failure.

C. Parameters:

(1) RequestName: The name of the step, the name displayed in the tree view in VuGen.

(2) List of Attribute: List of attributes. The supported attributes are as follows:

a.URL: The page address.

b.Method: The submission method of the page, POST or GET.

c.TargetFrame: The name of the Frame where the current link or resource is located. See the List of Attributes parameter of the same name.

In addition to the frame name, the following parameters can be specified:

_BLANK: Opens a blank window.

_PARENT: Replace the newly changed Frame with its parent.

_SELF: Replace the latest changed Frame.

_TOP: Replace the entire page.

d.EncType: encoding type.

e. RecContentType: The content type of the response header. See the List of Attributes parameter of the same name.

f. Referer: See the parameter of the same name in List of Attributes.

g. Body: Request body. See the List of Attributes parameter of the same name.

h. RAW BODY: See the parameter of the same name in List of Attributes.

i. BodyFilePath: The path to the file passed as the request body. It cannot be used with the following attributes: Body, or other Body attributes or Raw Body attributes including BodyBinary, BodyUnicode, RAW_BODY_START or Binary=1.

j. Resource, ResourceByteLimit, Snapshot, Mode: See the parameters of the same name in List of Attributes.

k. ExtraResBaseDir: See the parameter of the same name in List of Attributes.

l. UserAgent: User Agent, which is the name of an HTTP header used to identify an application, usually a browser, which presents the interaction between the user and the server.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327058979&siteId=291194637