性能测试学习第八天_脚本编写
设置关联
打开测试服务器,在http://localhost:1080/WebTours/页面点击administration,勾选set login form‘s action tag to an error page。用于分配动态session id
之后录制登录退出的脚本,进行验证回放,发现回放失败了
提示需要设置关联,所以我们在脚本关联结果中,进行录制快照中的文本与回放快照中的文本进行关联,如下图
关联之后的脚本如下
Action() { //Correlation comment - Do not change!Original value='36533009' Name ='CorrelationParameter_1' web_reg_save_param_ex( "ParamName=CorrelationParameter_1", "LB=118546.0", "RB=z", SEARCH_FILTERS, "Scope=Body", "RequestUrl=*/nav.pl*", LAST); //Correlation comment - Do not change!Original value='VVpHftVzzzH' Name ='CorrelationParameter_2' web_reg_save_param_ex( "ParamName=CorrelationParameter_2", "LB=H", "RB=D", SEARCH_FILTERS, "Scope=Body", "RequestUrl=*/nav.pl*", LAST); //Correlation comment - Do not change!Original value='Afcf' Name ='CorrelationParameter_3' web_reg_save_param_ex( "ParamName=CorrelationParameter_3", "LB=H", "RB=>\n<table border", SEARCH_FILTERS, "Scope=Body", "RequestUrl=*/nav.pl*", LAST); web_url("WebTours", "URL=http://localhost:1080/WebTours/", "Resource=0", "RecContentType=text/html", "Referer=", "Snapshot=t2.inf", "Mode=HTML", LAST); lr_think_time(9); web_submit_data("login.pl", "Action=http://localhost:1080/WebTours/login.pl", "Method=POST", "RecContentType=text/html", "Referer=http://localhost:1080/WebTours/nav.pl?in=home", "Snapshot=t3.inf", "Mode=HTML", ITEMDATA, "Name=userSession", "Value=118546.0{CorrelationParameter_1}zAQHH{CorrelationParameter_2}DzfizpcH{CorrelationParameter_3}", ENDITEM, "Name=username", "Value=jojo", ENDITEM, "Name=password", "Value=bean", ENDITEM, "Name=JSFormSubmit", "Value=on", ENDITEM, "Name=login.x", "Value=46", ENDITEM, "Name=login.y", "Value=14", ENDITEM, LAST); web_image("SignOff Button", "Alt=SignOff Button", "Snapshot=t4.inf", LAST); return 0; }
html录制模式和url录制模式
1.html模式录制的脚本
Action() { web_url("WebTours", "URL=http://localhost:1080/WebTours/", "Resource=0", "RecContentType=text/html", "Referer=", "Snapshot=t2.inf", "Mode=HTML", LAST); lr_think_time(11); web_submit_form("login.pl", "Snapshot=t3.inf", ITEMDATA, "Name=username", "Value=jojo", ENDITEM, "Name=password", "Value=bean", ENDITEM, "Name=login.x", "Value=83", ENDITEM, "Name=login.y", "Value=9", ENDITEM, LAST); web_image("Search Flights Button", "Alt=Search Flights Button", "Snapshot=t4.inf", LAST); lr_think_time(9); web_submit_form("reservations.pl", "Snapshot=t5.inf", ITEMDATA, "Name=depart", "Value=London", ENDITEM, "Name=departDate", "Value=05/18/2016", ENDITEM, "Name=arrive", "Value=Paris", ENDITEM, "Name=returnDate", "Value=05/19/2016", ENDITEM, "Name=numPassengers", "Value=2", ENDITEM, "Name=roundtrip", "Value=<OFF>", ENDITEM, "Name=seatPref", "Value=None", ENDITEM, "Name=seatType", "Value=Coach", ENDITEM, "Name=findFlights.x", "Value=9", ENDITEM, "Name=findFlights.y", "Value=11", ENDITEM, LAST); web_submit_form("reservations.pl_2", "Snapshot=t6.inf", ITEMDATA, "Name=outboundFlight", "Value=240;108;05/18/2016", ENDITEM, "Name=reserveFlights.x", "Value=58", ENDITEM, "Name=reserveFlights.y", "Value=3", ENDITEM, LAST); web_submit_form("reservations.pl_3", "Snapshot=t7.inf", ITEMDATA, "Name=firstName", "Value=Joseph", ENDITEM, "Name=lastName", "Value=Marshall", ENDITEM, "Name=address1", "Value=234 Willow Drive", ENDITEM, "Name=address2", "Value=San Jose/CA/94085", ENDITEM, "Name=pass1", "Value=Joseph Marshall", ENDITEM, "Name=pass2", "Value=", ENDITEM, "Name=creditCard", "Value=", ENDITEM, "Name=expDate", "Value=", ENDITEM, "Name=saveCC", "Value=<OFF>", ENDITEM, "Name=buyFlights.x", "Value=36", ENDITEM, "Name=buyFlights.y", "Value=8", ENDITEM, LAST); lr_think_time(5); web_image("Itinerary Button", "Alt=Itinerary Button", "Snapshot=t8.inf", LAST); web_image("SignOff Button", "Alt=SignOff Button", "Snapshot=t9.inf", LAST); return 0; }
2.url模式录制的脚本
Action() { web_url("WebTours", "URL=http://localhost:1080/WebTours/", "Resource=0", "RecContentType=text/html", "Referer=", "Snapshot=t2.inf", "Mode=HTTP", LAST); web_concurrent_start(NULL); web_url("header.html", "URL=http://localhost:1080/WebTours/header.html", "Resource=0", "RecContentType=text/html", "Referer=http://localhost:1080/WebTours/", "Snapshot=t3.inf", "Mode=HTTP", LAST); web_url("welcome.pl", "URL=http://localhost:1080/WebTours/welcome.pl?signOff=true", "Resource=0", "RecContentType=text/html", "Referer=http://localhost:1080/WebTours/", "Snapshot=t6.inf", "Mode=HTTP", LAST); web_concurrent_end(NULL); web_concurrent_start(NULL); web_url("hp_logo.png", "URL=http://localhost:1080/WebTours/images/hp_logo.png", "Resource=1", "RecContentType=image/png", "Referer=http://localhost:1080/WebTours/header.html", "Snapshot=t4.inf", LAST); web_url("webtours.png", "URL=http://localhost:1080/WebTours/images/webtours.png", "Resource=1", "RecContentType=image/png", "Referer=http://localhost:1080/WebTours/header.html", "Snapshot=t5.inf", LAST); web_concurrent_end(NULL); web_concurrent_start(NULL); web_url("home.html", "URL=http://localhost:1080/WebTours/home.html", "Resource=0", "RecContentType=text/html", "Referer=http://localhost:1080/WebTours/welcome.pl?signOff=true", "Snapshot=t7.inf", "Mode=HTTP", LAST); web_url("nav.pl", "URL=http://localhost:1080/WebTours/nav.pl?in=home", "Resource=0", "RecContentType=text/html", "Referer=http://localhost:1080/WebTours/welcome.pl?signOff=true", "Snapshot=t8.inf", "Mode=HTTP", LAST); web_concurrent_end(NULL); web_url("mer_login.gif", "URL=http://localhost:1080/WebTours/images/mer_login.gif", "Resource=1", "RecContentType=image/gif", "Referer=http://localhost:1080/WebTours/nav.pl?in=home", "Snapshot=t9.inf", LAST); lr_think_time(12); web_submit_data("login.pl", "Action=http://localhost:1080/WebTours/login.pl", "Method=POST", "RecContentType=text/html", "Referer=http://localhost:1080/WebTours/nav.pl?in=home", "Snapshot=t10.inf", "Mode=HTTP", ITEMDATA, "Name=userSession", "Value=118546.109518023zAQHHVfpfAiDDDDDDzfizpcQDAf", ENDITEM, "Name=username", "Value=jojo", ENDITEM, "Name=password", "Value=bean", ENDITEM, "Name=JSFormSubmit", "Value=off", ENDITEM, "Name=login.x", "Value=68", ENDITEM, "Name=login.y", "Value=15", ENDITEM, LAST); web_concurrent_start(NULL); web_url("nav.pl_2", "URL=http://localhost:1080/WebTours/nav.pl?page=menu&in=home", "Resource=0", "RecContentType=text/html", "Referer=http://localhost:1080/WebTours/login.pl", "Snapshot=t11.inf", "Mode=HTTP", LAST); web_url("login.pl_2", "URL=http://localhost:1080/WebTours/login.pl?intro=true", "Resource=0", "RecContentType=text/html", "Referer=http://localhost:1080/WebTours/login.pl", "Snapshot=t15.inf", "Mode=HTTP", LAST); web_concurrent_end(NULL); web_concurrent_start(NULL); web_url("flights.gif", "URL=http://localhost:1080/WebTours/images/flights.gif", "Resource=1", "RecContentType=image/gif", "Referer=http://localhost:1080/WebTours/nav.pl?page=menu&in=home", "Snapshot=t12.inf", LAST); web_url("in_home.gif", "URL=http://localhost:1080/WebTours/images/in_home.gif", "Resource=1", "RecContentType=image/gif", "Referer=http://localhost:1080/WebTours/nav.pl?page=menu&in=home", "Snapshot=t13.inf", LAST); web_url("itinerary.gif", "URL=http://localhost:1080/WebTours/images/itinerary.gif", "Resource=1", "RecContentType=image/gif", "Referer=http://localhost:1080/WebTours/nav.pl?page=menu&in=home", "Snapshot=t14.inf", LAST); web_url("signoff.gif", "URL=http://localhost:1080/WebTours/images/signoff.gif", "Resource=1", "RecContentType=image/gif", "Referer=http://localhost:1080/WebTours/nav.pl?page=menu&in=home", "Snapshot=t16.inf", LAST); web_concurrent_end(NULL); web_url("Search Flights Button", "URL=http://localhost:1080/WebTours/welcome.pl?page=search", "Resource=0", "RecContentType=text/html", "Referer=http://localhost:1080/WebTours/nav.pl?page=menu&in=home", "Snapshot=t17.inf", "Mode=HTTP", LAST); web_concurrent_start(NULL); web_url("nav.pl_3", "URL=http://localhost:1080/WebTours/nav.pl?page=menu&in=flights", "Resource=0", "RecContentType=text/html", "Referer=http://localhost:1080/WebTours/welcome.pl?page=search", "Snapshot=t18.inf", "Mode=HTTP", LAST); web_url("reservations.pl", "URL=http://localhost:1080/WebTours/reservations.pl?page=welcome", "Resource=0", "RecContentType=text/html", "Referer=http://localhost:1080/WebTours/welcome.pl?page=search", "Snapshot=t19.inf", "Mode=HTTP", LAST); web_concurrent_end(NULL); web_concurrent_start(NULL); web_url("in_flights.gif", "URL=http://localhost:1080/WebTours/images/in_flights.gif", "Resource=1", "RecContentType=image/gif", "Referer=http://localhost:1080/WebTours/nav.pl?page=menu&in=flights", "Snapshot=t20.inf", LAST); web_url("home.gif", "URL=http://localhost:1080/WebTours/images/home.gif", "Resource=1", "RecContentType=image/gif", "Referer=http://localhost:1080/WebTours/nav.pl?page=menu&in=flights", "Snapshot=t21.inf", LAST); web_concurrent_end(NULL); web_url("button_next.gif", "URL=http://localhost:1080/WebTours/images/button_next.gif", "Resource=1", "RecContentType=image/gif", "Referer=http://localhost:1080/WebTours/reservations.pl?page=welcome", "Snapshot=t22.inf", LAST); lr_think_time(9); web_submit_data("reservations.pl_2", "Action=http://localhost:1080/WebTours/reservations.pl", "Method=POST", "RecContentType=text/html", "Referer=http://localhost:1080/WebTours/reservations.pl?page=welcome", "Snapshot=t23.inf", "Mode=HTTP", ITEMDATA, "Name=advanceDiscount", "Value=0", ENDITEM, "Name=depart", "Value=London", ENDITEM, "Name=departDate", "Value=05/18/2016", ENDITEM, "Name=arrive", "Value=Paris", ENDITEM, "Name=returnDate", "Value=05/19/2016", ENDITEM, "Name=numPassengers", "Value=2", ENDITEM, "Name=seatPref", "Value=None", ENDITEM, "Name=seatType", "Value=Coach", ENDITEM, "Name=.cgifields", "Value=roundtrip", ENDITEM, "Name=.cgifields", "Value=seatType", ENDITEM, "Name=.cgifields", "Value=seatPref", ENDITEM, "Name=findFlights.x", "Value=46", ENDITEM, "Name=findFlights.y", "Value=10", ENDITEM, LAST); web_submit_data("reservations.pl_3", "Action=http://localhost:1080/WebTours/reservations.pl", "Method=POST", "RecContentType=text/html", "Referer=http://localhost:1080/WebTours/reservations.pl", "Snapshot=t24.inf", "Mode=HTTP", ITEMDATA, "Name=outboundFlight", "Value=240;108;05/18/2016", ENDITEM, "Name=numPassengers", "Value=2", ENDITEM, "Name=advanceDiscount", "Value=0", ENDITEM, "Name=seatType", "Value=Coach", ENDITEM, "Name=seatPref", "Value=None", ENDITEM, "Name=reserveFlights.x", "Value=36", ENDITEM, "Name=reserveFlights.y", "Value=12", ENDITEM, LAST); web_submit_data("reservations.pl_4", "Action=http://localhost:1080/WebTours/reservations.pl", "Method=POST", "RecContentType=text/html", "Referer=http://localhost:1080/WebTours/reservations.pl", "Snapshot=t25.inf", "Mode=HTTP", ITEMDATA, "Name=firstName", "Value=Joseph", ENDITEM, "Name=lastName", "Value=Marshall", ENDITEM, "Name=address1", "Value=234 Willow Drive", ENDITEM, "Name=address2", "Value=San Jose/CA/94085", ENDITEM, "Name=pass1", "Value=Joseph Marshall", ENDITEM, "Name=pass2", "Value=", ENDITEM, "Name=creditCard", "Value=", ENDITEM, "Name=expDate", "Value=", ENDITEM, "Name=oldCCOption", "Value=", ENDITEM, "Name=numPassengers", "Value=2", ENDITEM, "Name=seatType", "Value=Coach", ENDITEM, "Name=seatPref", "Value=None", ENDITEM, "Name=outboundFlight", "Value=240;108;05/18/2016", ENDITEM, "Name=advanceDiscount", "Value=0", ENDITEM, "Name=returnFlight", "Value=", ENDITEM, "Name=JSFormSubmit", "Value=off", ENDITEM, "Name=.cgifields", "Value=saveCC", ENDITEM, "Name=buyFlights.x", "Value=28", ENDITEM, "Name=buyFlights.y", "Value=6", ENDITEM, LAST); web_url("bookanother.gif", "URL=http://localhost:1080/WebTours/images/bookanother.gif", "Resource=1", "RecContentType=image/gif", "Referer=http://localhost:1080/WebTours/reservations.pl", "Snapshot=t26.inf", LAST); web_url("Itinerary Button", "URL=http://localhost:1080/WebTours/welcome.pl?page=itinerary", "Resource=0", "RecContentType=text/html", "Referer=http://localhost:1080/WebTours/nav.pl?page=menu&in=flights", "Snapshot=t27.inf", "Mode=HTTP", LAST); web_concurrent_start(NULL); web_url("itinerary.pl", "URL=http://localhost:1080/WebTours/itinerary.pl", "Resource=0", "RecContentType=text/html", "Referer=http://localhost:1080/WebTours/welcome.pl?page=itinerary", "Snapshot=t28.inf", "Mode=HTTP", LAST); web_url("nav.pl_4", "URL=http://localhost:1080/WebTours/nav.pl?page=menu&in=itinerary", "Resource=0", "RecContentType=text/html", "Referer=http://localhost:1080/WebTours/welcome.pl?page=itinerary", "Snapshot=t29.inf", "Mode=HTTP", LAST); web_concurrent_end(NULL); web_url("in_itinerary.gif", "URL=http://localhost:1080/WebTours/images/in_itinerary.gif", "Resource=1", "RecContentType=image/gif", "Referer=http://localhost:1080/WebTours/nav.pl?page=menu&in=itinerary", "Snapshot=t30.inf", LAST); web_concurrent_start(NULL); web_url("cancelreservation.gif", "URL=http://localhost:1080/WebTours/images/cancelreservation.gif", "Resource=1", "RecContentType=image/gif", "Referer=http://localhost:1080/WebTours/itinerary.pl", "Snapshot=t31.inf", LAST); web_url("cancelallreservations.gif", "URL=http://localhost:1080/WebTours/images/cancelallreservations.gif", "Resource=1", "RecContentType=image/gif", "Referer=http://localhost:1080/WebTours/itinerary.pl", "Snapshot=t32.inf", LAST); web_concurrent_end(NULL); web_url("SignOff Button", "URL=http://localhost:1080/WebTours/welcome.pl?signOff=1", "Resource=0", "RecContentType=text/html", "Referer=http://localhost:1080/WebTours/nav.pl?page=menu&in=itinerary", "Snapshot=t33.inf", "Mode=HTTP", LAST); web_concurrent_start(NULL); //并发记录 web_url("home.html_2", "URL=http://localhost:1080/WebTours/home.html", "Resource=0", "RecContentType=text/html", "Referer=http://localhost:1080/WebTours/welcome.pl?signOff=1", "Snapshot=t34.inf", "Mode=HTTP", LAST); web_url("nav.pl_5", "URL=http://localhost:1080/WebTours/nav.pl?in=home", "Resource=0", "RecContentType=text/html", "Referer=http://localhost:1080/WebTours/welcome.pl?signOff=1", "Snapshot=t35.inf", "Mode=HTTP", LAST); web_concurrent_end(NULL); //并发执行 return 0; }
添加事务(Transaction)
事务定义:事务是一个点,我们为了衡量某个操作的性能,在操作的开始和结束位置插入这样一个范围,即定义了一个transaction
添加事务原因:从性能测试角度出发,要知道不同操作所花费的时间,就用到了事务,我们在操作之前插入一个事务开始标识,在操作完成后插入一个事务结束标识,这样我们就知道这个操作所花费的时间。
事物的作用:LR运行到该事务开始点时开始计时,直到运行到该事务的结束点,计时结束。这个事务的运行时间会在LR的运行结果中反映。即,事务是一个计时标志。通常事务时间所反映的是一个操作过程的响应时间。
事务的函数构造:添加事务可以 插入-开始事务(Ctrl+T)/结束事务(Ctrl+D),或者点击
lr_start_transaction("login"); web_url("WebTours", "URL=http://localhost:1080/WebTours/", "Resource=0", "RecContentType=text/html", "Referer=", "Snapshot=t2.inf", "Mode=HTML", LAST); lr_end_transaction("login", LR_AUTO);
其中LR_AUTO:自动返回检测到的状态
运行结果:
Action.c(6): 在 HTML“http://localhost:1080/WebTours/nav.pl?in=home”中找到资源“http://localhost:1080/WebTours/images/mer_login.gif” [MsgId: MMSG-26659] Action.c(6): web_url("WebTours") 已成功,6449 个正文字节,1608 个标头字节 [MsgId: MMSG-26386] Action.c(15): 通知: 事务 "login" 以 "Pass" 状态结束 (持续时间: 7.1189 浪费的时间: 3.3532)。 Action.c(19): 将表单提交到“http://localhost:1080/WebTours/login.pl”,目标帧=“body” [MsgId: MMSG-27978]
Duration表示事务的完成响应时间,wasted time包括事务中函数自身执行所消耗的时间
参数化设置
参数化详解可以参考,写的很详细http://www.cnblogs.com/fnng/archive/2012/06/22/2558900.html
http://www.cnblogs.com/candle806/archive/2011/07/18/2109607.html
1.参数化之间的关联
主要用于用户名和密码的对应,如果,我们已经对用户名进行了参数化username,如果让用户名和密码进行对应,则需要设置密码参数和用户名关联
之后选择123456右键选择替换为参数,参数名称为passwd,选择右下角属性
从文件路径下,选择刚刚创建的username.dat,然后添加列,点击用记事本编辑,输入对应的密码,保存后页面如下
关闭后,点击完成,即完成了用户名和密码的参数化与对应
脚本设置完参数化,脚本运行的每一遍所取的参数化的值都不一样,那么这个值按照个什么情况来取呢?会有很多种方式
Select next row【选择下一行】:
顺序(Sequential):按照参数化的数据顺序,一个一个的来取。
随机(Random):参数化中的数据,每次随机的从中抽取数据。
唯一(Unique):为每个虚拟用户分配一条唯一的数据
Update value on【更新时的值】:
每次迭代(Each iteration) :每次迭代时取新的值,假如50个用户都取第一条数据,称为一次迭代;完了50个用户都取第二条数据,后面以此类推。
每次出现(Each occurrence):每次参数时取新的值,这里强调前后两次取值不能相同。
只取一次(once) :参数化中的数据,一条数据只能被抽取一次。(如果数据轮次完,脚本还在运行将会报错)
内容检查
勾选运行时设置下首选项中的 启用图像和文本检查
录制完脚本后 任务->内容检查,会显示脚本快照,选择一个要检查的页面,右键点击添加文本检查
如图可以看出添加检查点的函数为web_reg_find
确定后,脚本中添加了如下内容
web_reg_find("Search=Body", "Text=Find Flight", LAST);
日志解析
1.回放日志
2.录制日志
3.生成日志
错误处理函数
运行时设置(F4)
VuGen提供了错误处理函数void lr_continue_on_error(int value);
value是脚本运行时出错设置的取值,具体取值及相应含义如下表:
在使用表中参数时,常量名和常量值都可以作为参数
常量名称 | 常量值 | 含义 |
LR_ON_ERROR_NO_OPTIONS | 0 | 取消出错继续执行设置 |
LR_ON_ERROR_CONTINUE | 1 | 发生错误继续运行脚本 |
LR_ON_ERROR_SKIP_TO_NEXT_ACTION | 2 | 发生错误跳至下个Action运行 |
LR_ON_ERROR_SKIP_TO_NEXT_ITERATION | 3 | 发生错误跳至下个迭代运行 |
LR_ON_ERROR_END_VUSER | 4 | 发生错误结束当前用户 |
常用的是0和1,通常成对使用,适合压力较大且在夜间自动运行的测试场景,这类场景通常会在运行时设置中开启出现错误时仍继续选项
关键业务发生错误时需停止执行,通过lr_continue_on_error(0)实现
非关键业务发生错误时无需停止运行,通过lr_continue_on_error(1)实现
移动APP脚本录制
安装一个LR11 Patch4补丁,为了协议中有mobile app这个协议
之后,电脑和手机一个网段