[Testing Technology-Performance Testing-Practice 1] Use the system that comes with Loadrunner to learn scripting and scripting enhancement.

1. Start Web Tours Server

  • Hp software->start hp web tours server
  • Hp software->hp web tours application

2. Script recording
2.1. Settings before recording

Open Virtual User Generator

Create a script, select Web - HTTP/HTML as the protocol, enter the script name, select the script storage path, and click create to create a blank script.

Set the recording options, click the record button, and after setting the basic information, click Recording Options in the lower right corner to set the recording options:

HTTP/HTML Level setting: The script recorded based on HTML-based script is more concise. The script recorded based on URL-based script will load each html element with the web_url function. The script is lengthy and difficult to associate. In actual use, browser-based applications (not using the HTTP security protocol) are recommended to select HTML-based script; non-browser applications/use HTTPS security protocol, it is recommended to use URL-based script.
Encoding settings (to prevent Chinese garbled characters in recording): HTTP Properties -> Advanced, check Support charset (UTF-8)
and other default options to
start recording: After the recording options are set, click Start Recording to start recording.

2.2. Start recording
Record the complete login->check flight->select flight->pay->check the result->exit the login process

2.2.1. Login
Before logging in, insert a login transaction: tran01_login, then enter the user/password (jojo/bean), and click Login:

 After the login is complete, end the login transaction:

 

2.2.2. Find flights

First click Flights on the page to enter the search flight interface, and then insert the transaction: tran02_findFlight, after entering the flight information, click the continue button to enter the next step

 

After clicking the button, end the tran02_findFlight transaction.

2.2.3. Select flight

Insert transaction: trans03_selectFlight, select the flight, then click continue to enter the next step

 

End trans03_selectFlight transaction

2.2.4. Payment

Insert transaction: trans04_payment, enter the payment information, then click continue to enter the next step

 End trans04_payment transaction

2.2.5, view the results

Insert transaction: trans05_itinerary, click Itinerary to view the scheduled results

 End trans05_itinerary transaction

2.2.6. Log out

Insert transaction: trans06_signOff, click the sign Off button to log out:

 

End trans06_signOff transaction.

At this point, the entire reservation process is completed, click the end recording button to complete the recording and generate a recording script.

The complete recording script is as follows:

Action()
{

	//打开WebTours主页
	web_url("index.htm", 
		"URL=http://127.0.0.1:1080/WebTours/index.htm", 
		"Resource=0", 
		"RecContentType=text/html", 
		"Referer=", 
		"Snapshot=t1.inf", 
		"Mode=HTML", 
		LAST);

	lr_think_time(13);

	//登录
    lr_start_transaction("tran01_login");
    
	web_submit_data("login.pl", 
		"Action=http://127.0.0.1:1080/cgi-bin/login.pl", 
		"Method=POST", 
		"RecContentType=text/html", 
		"Referer=http://127.0.0.1:1080/cgi-bin/nav.pl?in=home", 
		"Snapshot=t4.inf", 
		"Mode=HTML", 
		ITEMDATA, 
		"Name=userSession", "Value=133951.489509923ziQAVcQpHtVzzzzHtVDtApHHitf", ENDITEM, 
		"Name=username", "Value=jojo", ENDITEM, 
		"Name=password", "Value=bean", ENDITEM, 
		"Name=JSFormSubmit", "Value=on", ENDITEM, 
		"Name=login.x", "Value=51", ENDITEM, 
		"Name=login.y", "Value=10", ENDITEM, 
		LAST);

	lr_end_transaction("tran01_login",LR_AUTO);

	lr_think_time(13);

	//查询航班
	lr_start_transaction("tran02_findFlight");

	web_submit_data("reservations.pl", 
		"Action=http://127.0.0.1:1080/cgi-bin/reservations.pl", 
		"Method=POST", 
		"RecContentType=text/html", 
		"Referer=http://127.0.0.1:1080/cgi-bin/reservations.pl?page=welcome", 
		"Snapshot=t9.inf", 
		"Mode=HTML", 
		ITEMDATA, 
		"Name=advanceDiscount", "Value=0", ENDITEM, 
		"Name=depart", "Value=Denver", ENDITEM, 
		"Name=departDate", "Value=05/28/2022", ENDITEM, 
		"Name=arrive", "Value=Frankfurt", ENDITEM, 
		"Name=returnDate", "Value=05/29/2022", ENDITEM, 
		"Name=numPassengers", "Value=1", ENDITEM, 
		"Name=seatPref", "Value=Window", ENDITEM, 
		"Name=seatType", "Value=First", ENDITEM, 
		"Name=.cgifields", "Value=roundtrip", ENDITEM, 
		"Name=.cgifields", "Value=seatType", ENDITEM, 
		"Name=.cgifields", "Value=seatPref", ENDITEM, 
		"Name=findFlights.x", "Value=65", ENDITEM, 
		"Name=findFlights.y", "Value=5", ENDITEM, 
		LAST);

	lr_end_transaction("tran02_findFlight",LR_AUTO);

	lr_think_time(21);

	//选择航班
	lr_start_transaction("trans03_selectFlight");

	web_submit_data("reservations.pl_2", 
		"Action=http://127.0.0.1:1080/cgi-bin/reservations.pl", 
		"Method=POST", 
		"RecContentType=text/html", 
		"Referer=http://127.0.0.1:1080/cgi-bin/reservations.pl", 
		"Snapshot=t10.inf", 
		"Mode=HTML", 
		ITEMDATA, 
		"Name=outboundFlight", "Value=013;592;05/28/2022", ENDITEM, 
		"Name=numPassengers", "Value=1", ENDITEM, 
		"Name=advanceDiscount", "Value=0", ENDITEM, 
		"Name=seatType", "Value=First", ENDITEM, 
		"Name=seatPref", "Value=Window", ENDITEM, 
		"Name=reserveFlights.x", "Value=52", ENDITEM, 
		"Name=reserveFlights.y", "Value=9", ENDITEM, 
		LAST);	
	
	

	lr_end_transaction("trans03_selectFlight",LR_AUTO);

	lr_think_time(26);

	//支付
	lr_start_transaction("trans04_payment");
	
	web_submit_data("reservations.pl_3", 
		"Action=http://127.0.0.1:1080/cgi-bin/reservations.pl", 
		"Method=POST", 
		"RecContentType=text/html", 
		"Referer=http://127.0.0.1:1080/cgi-bin/reservations.pl", 
		"Snapshot=t11.inf", 
		"Mode=HTML", 
		ITEMDATA, 
		"Name=firstName", "Value=Jojo", ENDITEM, 
		"Name=lastName", "Value=Bean", ENDITEM, 
		"Name=address1", "Value=123", ENDITEM, 
		"Name=address2", "Value=456", ENDITEM, 
		"Name=pass1", "Value=Jojo Bean", ENDITEM, 
		"Name=creditCard", "Value=123456", ENDITEM, 
		"Name=expDate", "Value=1122", ENDITEM, 
		"Name=saveCC", "Value=on", ENDITEM, 
		"Name=oldCCOption", "Value=on", ENDITEM, 
		"Name=numPassengers", "Value=1", ENDITEM, 
		"Name=seatType", "Value=First", ENDITEM, 
		"Name=seatPref", "Value=Window", ENDITEM, 
		"Name=outboundFlight", "Value=013;592;05/28/2022", ENDITEM, 
		"Name=advanceDiscount", "Value=0", ENDITEM, 
		"Name=returnFlight", "Value=", ENDITEM, 
		"Name=JSFormSubmit", "Value=off", ENDITEM, 
		"Name=.cgifields", "Value=saveCC", ENDITEM, 
		"Name=buyFlights.x", "Value=42", ENDITEM, 
		"Name=buyFlights.y", "Value=10", ENDITEM, 
		LAST);

	lr_end_transaction("trans04_payment",LR_AUTO);

	lr_think_time(16);

	//查询预定结果
	lr_start_transaction("trans05_itinerary");

	web_url("welcome.pl", 
		"URL=http://127.0.0.1:1080/cgi-bin/welcome.pl?page=itinerary", 
		"Resource=0", 
		"RecContentType=text/html", 
		"Referer=http://127.0.0.1:1080/cgi-bin/nav.pl?page=menu&in=flights", 
		"Snapshot=t12.inf", 
		"Mode=HTML", 
		LAST);

	lr_end_transaction("trans05_itinerary",LR_AUTO);

	lr_think_time(19);

	//退出登录
	lr_start_transaction("trans06_signOff");

	web_url("welcome.pl_3", 
		"URL=http://127.0.0.1:1080/cgi-bin/welcome.pl?signOff=1", 
		"Resource=0", 
		"RecContentType=text/html", 
		"Referer=http://127.0.0.1:1080/cgi-bin/nav.pl?page=menu&in=itinerary", 
		"Snapshot=t13.inf", 
		"Mode=HTML", 
		LAST);

	lr_end_transaction("trans06_signOff",LR_AUTO);

	return 0;
}

3. Script playback

Click Replay to play back the script:

Script playback was successful.

4. Script enhancement
4.1. Parameter association
Function: associate the dynamic value returned by the server to the client.

After the playback is successful, you can log in to WebTours to check whether a scheduled record has been added. If there is an increase, it means that the script is OK and the booking can be made successfully. However, after logging in, I found that the number of reservations has not increased. So, the script needs to be rechecked.

The first is to check if the login was successful. You can see that in the web_submit_data method of login, the submitted parameters include userSession, which is dynamically generated. When userSession requests the server for the first time, the server returns it, so it is necessary to associate parameters with this parameter.

There are two ways to get userSession:

Request the page in the browser, check the response with the F12 tool, and find userSession in the response message:

 You can see that in the nav.pl?in=home request, the returned message contains userSession:

<input type="hidden" name="userSession" value="133944.089590928ziiQHtQpVAiDDDDDtVDDiptQtzf"/>
  • In Loadrunner, right-click anywhere in the previous step of the request -> Show snapshot, and then use Ctrl+F to find the value in Snapshot, which can also be found:

 

 After finding the userSession, you can start writing the associated function. The associated function is web_reg_save_param(), as follows:

//设置userSession参数关联
web_reg_save_param("userSession",							//参数名
                   "LB=name=\"userSession\" value=\"",		//左边界:参数值的左边字符,如果带有双引号,需要用\进行转义
                   "RB=\"/>",								//右边界:参数值的右边字符,根据左右边界可以唯一确定参数值
                   LAST);	

Note that the association needs to be written above the operation function. After the association function is written, the parameter values ​​in the script need to be replaced, and the submission parameters at login are replaced as follows:

web_submit_data("login.pl", 
                "Action=http://127.0.0.1:1080/cgi-bin/login.pl", 
                "Method=POST", 
                "RecContentType=text/html", 
                "Referer=http://127.0.0.1:1080/cgi-bin/nav.pl?in=home", 
                "Snapshot=t42.inf", 
                "Mode=HTML", 
                ITEMDATA, 
                "Name=userSession", "Value={userSession}", ENDITEM, 		//参数关联,参数名需要用{}括起来
                "Name=username", "Value=jojo", ENDITEM, 
                "Name=password", "Value=bean", ENDITEM, 
                "Name=JSFormSubmit", "Value=on", ENDITEM, 
                "Name=login.x", "Value=42", ENDITEM, 
                "Name=login.y", "Value=12", ENDITEM, 
                LAST);

After the association is completed, replay it, and then log in to the website to check. You can see that there is an additional record in the Itinerary. Every time you play back, there will be an additional record, indicating that the association is successful, and the script can complete the login normally.

4.2. Parameterization
In this script, all parameters are fixed. For example, when booking a ticket, it is Denver->Frankfurt every time. If you want to use a different departure/arrival city pair for each booking, you need Parameterize the response data. For specific operations, refer to: https://blog.csdn.net/weixin_42115131/article/details/124965795

The set up script is as follows:
 

web_submit_data("reservations.pl", 
                "Action=http://127.0.0.1:1080/cgi-bin/reservations.pl", 
                "Method=POST", 
                "RecContentType=text/html", 
                "Referer=http://127.0.0.1:1080/cgi-bin/reservations.pl?page=welcome", 
                "Snapshot=t47.inf", 
                "Mode=HTML", 
                ITEMDATA, 
                "Name=advanceDiscount", "Value=0", ENDITEM, 
                "Name=depart", "Value={depart}", ENDITEM, 					//出发城市参数化
                "Name=departDate", "Value=05/27/2022", ENDITEM, 
                "Name=arrive", "Value={arrive}", ENDITEM, 					//到达城市参数化
                "Name=returnDate", "Value=05/28/2022", ENDITEM, 
                "Name=numPassengers", "Value=1", ENDITEM, 
                "Name=seatPref", "Value=Window", ENDITEM, 
                "Name=seatType", "Value=Business", ENDITEM, 
                "Name=.cgifields", "Value=roundtrip", ENDITEM, 
                "Name=.cgifields", "Value=seatType", ENDITEM, 
                "Name=.cgifields", "Value=seatPref", ENDITEM, 
                "Name=findFlights.x", "Value=63", ENDITEM, 
                "Name=findFlights.y", "Value=8", ENDITEM, 
                LAST);

Parameter values ​​can be set in Parameters.

 

When setting parameters, the Select next row box selects Unique and Sequential with the following differences, taking 10 virtual users to iterate 10 times as an example:

When Sequential is selected, you can see that each virtual user of the same virtual user takes the same value, the second parameter is taken in the next iteration, and the parameters of the sixth iteration have been taken, and the first value is re-circulated:

 When Unique is selected, it means that each virtual user takes a unique value, and each iteration takes a different parameter value for each virtual user. Since only 5 virtual users are set, the last 5 virtual users do not get values:

 After replacing the parameters, re-run the script, set the iteration to 5 times in the runtime setting, and log in to the website after completion to check whether orders for 5 different city pairs have been generated

 

It can be seen that the five generated records are all Denver/Frankfurt. Parameterization did not take effect.

Check the script again, and find that the data submitted by the selected flight after the query flight is completed, outboundFlight is 013;592;05/28/2022, that is, no matter what the query result is, the flight number selected each time is 013, 592$, the value It is the value returned by the server when querying flights, so it is also necessary to associate parameters with the value, and the association method is the same as userSession:
 

web_reg_save_param("outboundFlight",
                   "LB=name=\"outboundFlight\" value=\"",
                   "RB=\">",
                   "Ord=2",					//相同的左右边界共有4个,取第二个
                   LAST);

After the association, replace all occurrences of the parameter in the following script with the associated parameter, then re-execute the script and check the result:

 

It can be seen that 5 tickets with different departure and arrival city pairs have been successfully booked.

4.3. Checkpoint setting
After the booking is successful, the page will return the string "Thank you for booking through Web Tours." Here are two ways to set the checkpoint:

4.3.1, web_reg_find() function
Reference: https://blog.csdn.net/weixin_42115131/article/details/124965795

4.3.2. Save & compare
The principle is consistent with the parameter association, first save the parameters returned by the server:
 

<small><B>Thank you for booking through Web Tours.</B></small>
web_reg_save_param("response",
                   "LB=<small><B>",
                   "RB=</B></small>",
                   LAST);

After payment, compare the server return value with the expected value:

//检查支付是否成功
if(strstr(lr_eval_string("{payment_result}"), "Thank you for booking through Web Tours.")!=NULL){
    //strstr(str1, str2)为c语言函数,如果str2为str1子串,则返回字串出现的位置;不是则返回NULL
    //lr_eval_string()函数作用为返回脚本中的一个参数当前的值,格式为:lr_eval_string("{参数名}");
    lr_end_transaction("trans04_payment",LR_PASS);
    //事务有3种结束状态,LR_AUTO为自动结束,不判断成功失败;LR_FAIL为以失败的状态结束;LR_PASS为以成功的状态结束	   	
    lr_output_message("Success! depart is %s, arrive is %s.", lr_eval_string("{depart}"), lr_eval_string("{arrive}"));
    //lr_output_message为打印函数
}else{
    lr_end_transaction("trans04_payment",LR_PASS);
    lr_output_message("FAIL!!!!!!");
}

After adding the checkpoint, re-run, the result is as follows:

Action.c(149): Success! depart is Portland, arrive is Denver.
Action.c(149): Success! depart is San Francisco, arrive is Frankfurt.
Action.c(149): Success! depart is Seattle, arrive is London.
Action.c(149): Success! depart is Sydney, arrive is Los Angeles.
Action.c(149): Success! depart is Zurich, arrive is Paris.

At this point, the script debugging is completed, and the script can be loaded into the controller to set the scene execution. The full script is as follows:

Action()
{

	//设置userSession参数关联
	web_reg_save_param("userSession",			//参数名
		"LB=name=\"userSession\" value=\"",		//左边界:参数值的左边字符,如果带有双引号,需要用\进行转义
		"RB=\"/>",								//右边界:参数值的右边字符,根据左右边界可以唯一确定参数值
		LAST);		
	
	//打开WebTours主页
	web_url("index.htm", 
		"URL=http://127.0.0.1:1080/WebTours/index.htm", 
		"Resource=0", 
		"RecContentType=text/html", 
		"Referer=", 
		"Snapshot=t1.inf", 
		"Mode=HTML", 
		LAST);

	lr_think_time(13);

	//登录
	lr_start_transaction("tran01_login");
	
	web_submit_data("login.pl", 
		"Action=http://127.0.0.1:1080/cgi-bin/login.pl", 
		"Method=POST", 
		"RecContentType=text/html", 
		"Referer=http://127.0.0.1:1080/cgi-bin/nav.pl?in=home", 
		"Snapshot=t4.inf", 
		"Mode=HTML", 
		ITEMDATA, 
		"Name=userSession", "Value={userSession}", ENDITEM, 
		"Name=username", "Value=jojo", ENDITEM, 
		"Name=password", "Value=bean", ENDITEM, 
		"Name=JSFormSubmit", "Value=on", ENDITEM, 
		"Name=login.x", "Value=51", ENDITEM, 
		"Name=login.y", "Value=10", ENDITEM, 
		LAST);

	lr_end_transaction("tran01_login",LR_AUTO);

	lr_think_time(13);

	//查询航班
	lr_start_transaction("tran02_findFlight");
	
	//设置outboundFlight关联函数
	web_reg_save_param("outboundFlight",
       "LB=name=\"outboundFlight\" value=\"",
       "RB=\">",
       "Ord=2",					//相同的左右边界共有4个,取第二个
       LAST);	

	web_submit_data("reservations.pl", 
		"Action=http://127.0.0.1:1080/cgi-bin/reservations.pl", 
		"Method=POST", 
		"RecContentType=text/html", 
		"Referer=http://127.0.0.1:1080/cgi-bin/reservations.pl?page=welcome", 
		"Snapshot=t9.inf", 
		"Mode=HTML", 
		ITEMDATA, 
		"Name=advanceDiscount", "Value=0", ENDITEM, 
		"Name=depart", "Value={depart}", ENDITEM, 
		"Name=departDate", "Value=05/28/2022", ENDITEM, 
		"Name=arrive", "Value={arrive}", ENDITEM, 
		"Name=returnDate", "Value=05/29/2022", ENDITEM, 
		"Name=numPassengers", "Value=1", ENDITEM, 
		"Name=seatPref", "Value=Window", ENDITEM, 
		"Name=seatType", "Value=First", ENDITEM, 
		"Name=.cgifields", "Value=roundtrip", ENDITEM, 
		"Name=.cgifields", "Value=seatType", ENDITEM, 
		"Name=.cgifields", "Value=seatPref", ENDITEM, 
		"Name=findFlights.x", "Value=65", ENDITEM, 
		"Name=findFlights.y", "Value=5", ENDITEM, 
		LAST);

	lr_end_transaction("tran02_findFlight",LR_AUTO);

	lr_think_time(21);

	//选择航班
	lr_start_transaction("trans03_selectFlight");

	web_submit_data("reservations.pl_2", 
		"Action=http://127.0.0.1:1080/cgi-bin/reservations.pl", 
		"Method=POST", 
		"RecContentType=text/html", 
		"Referer=http://127.0.0.1:1080/cgi-bin/reservations.pl", 
		"Snapshot=t10.inf", 
		"Mode=HTML", 
		ITEMDATA, 
		"Name=outboundFlight", "Value={outboundFlight}", ENDITEM, 
		"Name=numPassengers", "Value=1", ENDITEM, 
		"Name=advanceDiscount", "Value=0", ENDITEM, 
		"Name=seatType", "Value=First", ENDITEM, 
		"Name=seatPref", "Value=Window", ENDITEM, 
		"Name=reserveFlights.x", "Value=52", ENDITEM, 
		"Name=reserveFlights.y", "Value=9", ENDITEM, 
		LAST);	

	lr_end_transaction("trans03_selectFlight",LR_AUTO);

	lr_think_time(26);

	//支付
	lr_start_transaction("trans04_payment");
	
    //保存返回的参数,做事务状态检查
	web_reg_save_param("payment_result",
       "LB=<small><B>",
       "RB=</B></small>",
       LAST);
	
	web_submit_data("reservations.pl_3", 
		"Action=http://127.0.0.1:1080/cgi-bin/reservations.pl", 
		"Method=POST", 
		"RecContentType=text/html", 
		"Referer=http://127.0.0.1:1080/cgi-bin/reservations.pl", 
		"Snapshot=t11.inf", 
		"Mode=HTML", 
		ITEMDATA, 
		"Name=firstName", "Value=Jojo", ENDITEM, 
		"Name=lastName", "Value=Bean", ENDITEM, 
		"Name=address1", "Value=123", ENDITEM, 
		"Name=address2", "Value=456", ENDITEM, 
		"Name=pass1", "Value=Jojo Bean", ENDITEM, 
		"Name=creditCard", "Value=123456", ENDITEM, 
		"Name=expDate", "Value=1122", ENDITEM, 
		"Name=saveCC", "Value=on", ENDITEM, 
		"Name=oldCCOption", "Value=on", ENDITEM, 
		"Name=numPassengers", "Value=1", ENDITEM, 
		"Name=seatType", "Value=First", ENDITEM, 
		"Name=seatPref", "Value=Window", ENDITEM, 
		"Name=outboundFlight", "Value={outboundFlight}", ENDITEM, 
		"Name=advanceDiscount", "Value=0", ENDITEM, 
		"Name=returnFlight", "Value=", ENDITEM, 
		"Name=JSFormSubmit", "Value=off", ENDITEM, 
		"Name=.cgifields", "Value=saveCC", ENDITEM, 
		"Name=buyFlights.x", "Value=42", ENDITEM, 
		"Name=buyFlights.y", "Value=10", ENDITEM, 
		LAST);
	
	//检查支付是否成功
	if(strstr(lr_eval_string("{payment_result}"), "Thank you for booking through Web Tours.")!=NULL){
	   	//strstr(str1, str2)为c语言函数,如果str2为str1子串,则返回字串出现的位置;不是则返回NULL
	   	//lr_eval_string()函数作用为返回脚本中的一个参数当前的值,格式为:lr_eval_string("{参数名}");
	   	lr_end_transaction("trans04_payment",LR_PASS);
		//事务有3种结束状态,LR_AUTO为自动结束,不判断成功失败;LR_FAIL为以失败的状态结束;LR_PASS为以成功的状态结束	   	
		lr_output_message("Success! depart is %s, arrive is %s.", lr_eval_string("{depart}"), lr_eval_string("{arrive}"));
		//lr_output_message为打印函数
	}else{
	   	lr_end_transaction("trans04_payment",LR_PASS);
	   	lr_output_message("FAIL!!!!!!");
	}


	lr_think_time(16);

	//查询预定结果
	lr_start_transaction("trans05_itinerary");

	web_url("welcome.pl", 
		"URL=http://127.0.0.1:1080/cgi-bin/welcome.pl?page=itinerary", 
		"Resource=0", 
		"RecContentType=text/html", 
		"Referer=http://127.0.0.1:1080/cgi-bin/nav.pl?page=menu&in=flights", 
		"Snapshot=t12.inf", 
		"Mode=HTML", 
		LAST);

	lr_end_transaction("trans05_itinerary",LR_AUTO);

	lr_think_time(19);

	//退出登录
	lr_start_transaction("trans06_signOff");

	web_url("welcome.pl_3", 
		"URL=http://127.0.0.1:1080/cgi-bin/welcome.pl?signOff=1", 
		"Resource=0", 
		"RecContentType=text/html", 
		"Referer=http://127.0.0.1:1080/cgi-bin/nav.pl?page=menu&in=itinerary", 
		"Snapshot=t13.inf", 
		"Mode=HTML", 
		LAST);

	lr_end_transaction("trans06_signOff",LR_AUTO);

	return 0;
}


 

Guess you like

Origin blog.csdn.net/lzz718719/article/details/130715173
Recommended