性能测试之看懂这一个LoadRunner再无困难的脚本

一个特别的练习需求

LoadRunner自带的训练的飞机票网站的例子中,有一个很有意思的流程,如下:首先登陆机票系统,然后查询从哪里到哪里的机票。然后系统会给你4张推荐票。选择一张机票,后就可以支付然后完成机票的预定和购买了。

我相信所有在用LoadRunner的人对这个流程都不陌生,很熟悉。那么今天测者提出一个问题,我想一个脚本购买推荐的4张飞机票。该如何处理对应的测试脚本呢?

一个飞机票的超级脚本

  1. Action()

  2. {

  3.    int i=0;//循环订票的游标变量

  4.    char temp[255];//临时存储字符串变量

  5.    int count=0;//存储循环关联数组长度变量

  6.    char tempfly[255];//临时存储字符串变量

  7.  

  8.    web_url("WebTours",

  9.        "URL=http://127.0.0.1:1080/WebTours/",

  10.        "Resource=0",

  11.        "RecContentType=text/html",

  12.        "Referer=",

  13.        "Snapshot=t1.inf",

  14.        "Mode=HTTP",

  15.        LAST);

  16.  

  17.    web_concurrent_start(NULL);

  18.  

  19.    web_url("header.html",

  20.        "URL=http://127.0.0.1:1080/WebTours/header.html",

  21.        "Resource=0",

  22.        "RecContentType=text/html",

  23.        "Referer=http://127.0.0.1:1080/WebTours/",

  24.        "Snapshot=t2.inf",

  25.        "Mode=HTTP",

  26.        LAST);

  27.  

  28.    web_url("welcome.pl",

  29.        "URL=http://127.0.0.1:1080/WebTours/welcome.pl?signOff=true",

  30.        "Resource=0",

  31.        "RecContentType=text/html",

  32.        "Referer=http://127.0.0.1:1080/WebTours/",

  33.        "Snapshot=t4.inf",

  34.        "Mode=HTTP",

  35.        LAST);

  36.  

  37.    web_concurrent_end(NULL);

  38.  

  39.    web_concurrent_start(NULL);

  40.  

  41.    web_url("hp_logo.png",

  42.        "URL=http://127.0.0.1:1080/WebTours/images/hp_logo.png",

  43.        "Resource=1",

  44.        "RecContentType=image/png",

  45.        "Referer=http://127.0.0.1:1080/WebTours/header.html",

  46.        "Snapshot=t3.inf",

  47.        LAST);

  48.  

  49.    web_url("webtours.png",

  50.        "URL=http://127.0.0.1:1080/WebTours/images/webtours.png",

  51.        "Resource=1",

  52.        "RecContentType=image/png",

  53.        "Referer=http://127.0.0.1:1080/WebTours/header.html",

  54.        "Snapshot=t5.inf",

  55.        LAST);

  56.  

  57.    web_concurrent_end(NULL);

  58.  

  59.    web_concurrent_start(NULL);

  60.  

  61.    web_url("home.html",

  62.        "URL=http://127.0.0.1:1080/WebTours/home.html",

  63.        "Resource=0",

  64.        "RecContentType=text/html",

  65.        "Referer=http://127.0.0.1:1080/WebTours/welcome.pl?signOff=true",

  66.        "Snapshot=t6.inf",

  67.        "Mode=HTTP",

  68.        LAST);

  69.    web_reg_save_param("sessionuser","LB=<input type=hidden name=userSession value=","RB=>",LAST);//获取用于session值的关联函数

  70.    web_url("nav.pl",

  71.        "URL=http://127.0.0.1:1080/WebTours/nav.pl?in=home",

  72.        "Resource=0",

  73.        "RecContentType=text/html",

  74.        "Referer=http://127.0.0.1:1080/WebTours/welcome.pl?signOff=true",

  75.        "Snapshot=t7.inf",

  76.        "Mode=HTTP",

  77.        LAST);

  78.  

  79.    web_concurrent_end(NULL);

  80.  

  81.    web_concurrent_start(NULL);

  82.  

  83.    web_url("JSFormSubmit.js",

  84.        "URL=http://127.0.0.1:1080/WebTours/JSFormSubmit.js",

  85.        "Resource=1",

  86.        "RecContentType=application/x-javascript",

  87.        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?in=home",

  88.        "Snapshot=t8.inf",

  89.        LAST);

  90.  

  91.    web_url("mer_login.gif",

  92.        "URL=http://127.0.0.1:1080/WebTours/images/mer_login.gif",

  93.        "Resource=1",

  94.        "RecContentType=image/gif",

  95.        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?in=home",

  96.        "Snapshot=t9.inf",

  97.        LAST);

  98.  

  99.    web_concurrent_end(NULL);

  100.  

  101.    lr_start_transaction("login-0");

  102.  

  103.    lr_think_time(9);

  104.    lr_error_message("sessionuser:%s",lr_eval_string("{sessionuser}"));//打印关联到的用户session值

  105.    web_submit_data("login.pl",

  106.        "Action=http://127.0.0.1:1080/WebTours/login.pl",

  107.        "Method=POST",

  108.        "RecContentType=text/html",

  109.        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?in=home",

  110.        "Snapshot=t10.inf",

  111.        "Mode=HTTP",

  112.        ITEMDATA,

  113.        "Name=userSession", "Value={sessionuser}", ENDITEM,

  114.        "Name=username", "Value=test001", ENDITEM,

  115.        "Name=password", "Value=123456", ENDITEM,

  116.        "Name=JSFormSubmit", "Value=on", ENDITEM,

  117.        "Name=login.x", "Value=64", ENDITEM,

  118.        "Name=login.y", "Value=15", ENDITEM,

  119.        LAST);

  120.  

  121.    web_concurrent_start(NULL);

  122.  

  123.    web_url("nav.pl_2",

  124.        "URL=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=home",

  125.        "Resource=0",

  126.        "RecContentType=text/html",

  127.        "Referer=http://127.0.0.1:1080/WebTours/login.pl",

  128.        "Snapshot=t11.inf",

  129.        "Mode=HTTP",

  130.        LAST);

  131.  

  132.    web_url("login.pl_2",

  133.        "URL=http://127.0.0.1:1080/WebTours/login.pl?intro=true",

  134.        "Resource=0",

  135.        "RecContentType=text/html",

  136.        "Referer=http://127.0.0.1:1080/WebTours/login.pl",

  137.        "Snapshot=t16.inf",

  138.        "Mode=HTTP",

  139.        LAST);

  140.  

  141.    web_concurrent_end(NULL);

  142.  

  143.    web_concurrent_start(NULL);

  144.  

  145.    web_url("flights.gif",

  146.        "URL=http://127.0.0.1:1080/WebTours/images/flights.gif",

  147.        "Resource=1",

  148.        "RecContentType=image/gif",

  149.        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=home",

  150.        "Snapshot=t12.inf",

  151.        LAST);

  152.  

  153.    web_url("signoff.gif",

  154.        "URL=http://127.0.0.1:1080/WebTours/images/signoff.gif",

  155.        "Resource=1",

  156.        "RecContentType=image/gif",

  157.        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=home",

  158.        "Snapshot=t13.inf",

  159.        LAST);

  160.  

  161.    web_url("itinerary.gif",

  162.        "URL=http://127.0.0.1:1080/WebTours/images/itinerary.gif",

  163.        "Resource=1",

  164.        "RecContentType=image/gif",

  165.        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=home",

  166.        "Snapshot=t14.inf",

  167.        LAST);

  168.  

  169.    web_url("in_home.gif",

  170.        "URL=http://127.0.0.1:1080/WebTours/images/in_home.gif",

  171.        "Resource=1",

  172.        "RecContentType=image/gif",

  173.        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=home",

  174.        "Snapshot=t15.inf",

  175.        LAST);

  176.  

  177.    web_concurrent_end(NULL);

  178.  

  179.    lr_end_transaction("login-0",LR_AUTO);

  180.  

  181.    lr_start_transaction("book-1");

  182.  

  183.    lr_think_time(11);

  184.  

  185.    web_url("Search Flights Button",

  186.        "URL=http://127.0.0.1:1080/WebTours/welcome.pl?page=search",

  187.        "Resource=0",

  188.        "RecContentType=text/html",

  189.        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=home",

  190.        "Snapshot=t17.inf",

  191.        "Mode=HTTP",

  192.        LAST);

  193.  

  194.    web_concurrent_start(NULL);

  195.  

  196.    web_url("reservations.pl",

  197.        "URL=http://127.0.0.1:1080/WebTours/reservations.pl?page=welcome",

  198.        "Resource=0",

  199.        "RecContentType=text/html",

  200.        "Referer=http://127.0.0.1:1080/WebTours/welcome.pl?page=search",

  201.        "Snapshot=t18.inf",

  202.        "Mode=HTTP",

  203.        LAST);

  204.  

  205.    web_url("nav.pl_3",

  206.        "URL=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=flights",

  207.        "Resource=0",

  208.        "RecContentType=text/html",

  209.        "Referer=http://127.0.0.1:1080/WebTours/welcome.pl?page=search",

  210.        "Snapshot=t19.inf",

  211.        "Mode=HTTP",

  212.        LAST);

  213.  

  214.    web_concurrent_end(NULL);

  215.  

  216.    web_concurrent_start(NULL);

  217.  

  218.    web_url("home.gif",

  219.        "URL=http://127.0.0.1:1080/WebTours/images/home.gif",

  220.        "Resource=1",

  221.        "RecContentType=image/gif",

  222.        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=flights",

  223.        "Snapshot=t20.inf",

  224.        LAST);

  225.  

  226.    web_url("itinerary.gif_2",

  227.        "URL=http://127.0.0.1:1080/WebTours/images/itinerary.gif",

  228.        "Resource=0",

  229.        "RecContentType=text/html",

  230.        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=flights",

  231.        "Mode=HTTP",

  232.        LAST);

  233.  

  234.    web_url("signoff.gif_2",

  235.        "URL=http://127.0.0.1:1080/WebTours/images/signoff.gif",

  236.        "Resource=0",

  237.        "RecContentType=text/html",

  238.        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=flights",

  239.        "Mode=HTTP",

  240.        LAST);

  241.  

  242.    web_url("in_flights.gif",

  243.        "URL=http://127.0.0.1:1080/WebTours/images/in_flights.gif",

  244.        "Resource=1",

  245.        "RecContentType=image/gif",

  246.        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=flights",

  247.        "Snapshot=t21.inf",

  248.        LAST);

  249.  

  250.    web_concurrent_end(NULL);

  251.  

  252.    web_url("button_next.gif",

  253.        "URL=http://127.0.0.1:1080/WebTours/images/button_next.gif",

  254.        "Resource=1",

  255.        "RecContentType=image/gif",

  256.        "Referer=http://127.0.0.1:1080/WebTours/reservations.pl?page=welcome",

  257.        "Snapshot=t22.inf",

  258.        LAST);

  259.    web_reg_save_param("flytype","LB=<input type = radio name=outboundFlight value=","RB=>","ORD=ALL","Savelen=18",LAST);//关联取得出现的4张电子机票

  260. //      web_reg_save_param("seatPref","LB=<input type=\"radio\" name=\"seatPref\" value=\"","RB=\" ","ORD=ALL","SaveLen=6",LAST);

  261. //  web_reg_save_param("seatType","LB=<input type=\"radio\" name=\"seatType\" value=\"","RB=\" ","ORD=ALL","SaveLen=8",LAST);

  262.    lr_error_message("seatPref_1:",lr_eval_string("{seatPref_1}"));

  263.    lr_error_message("seatPref_2:",lr_eval_string("{seatPref_2}"));

  264.    lr_error_message("seatPref_3:",lr_eval_string("{seatPref_3}"));

  265.    lr_error_message("seatType_1:",lr_eval_string("{seatType_1}"));

  266.    lr_error_message("seatType_2:",lr_eval_string("{seatType_2}"));

  267.    lr_error_message("seatType_3:",lr_eval_string("{seatType_3}"));

  268.    web_submit_data("reservations.pl_2",

  269.        "Action=http://127.0.0.1:1080/WebTours/reservations.pl",

  270.        "Method=POST",

  271.        "RecContentType=text/html",

  272.        "Referer=http://127.0.0.1:1080/WebTours/reservations.pl?page=welcome",

  273.        "Snapshot=t23.inf",

  274.        "Mode=HTTP",

  275.        ITEMDATA,

  276.        "Name=advanceDiscount", "Value=0", ENDITEM,

  277.        "Name=depart", "Value=Denver", ENDITEM,

  278.        "Name=departDate", "Value=05/29/2013", ENDITEM,

  279.        "Name=arrive", "Value={endplace}", ENDITEM,

  280.        "Name=returnDate", "Value=05/30/2013", ENDITEM,

  281.        "Name=numPassengers", "Value=1", ENDITEM,

  282.        "Name=seatPref", "Value={seatPerf}", ENDITEM,

  283.        "Name=seatType", "Value={seatType}", ENDITEM,

  284.        "Name=.cgifields", "Value=roundtrip", ENDITEM,

  285.        "Name=.cgifields", "Value=seatType", ENDITEM,

  286.        "Name=.cgifields", "Value=seatPref", ENDITEM,

  287.        "Name=findFlights.x", "Value=80", ENDITEM,

  288.        "Name=findFlights.y", "Value=15", ENDITEM,

  289.        LAST);

  290.    lr_error_message("seatPerf:%s",lr_eval_string("{seatPerf}"));

  291.    lr_error_message("seatType:%s",lr_eval_string("{seatType}"));

  292.    count = atoi(lr_eval_string("{flytype_count}"));

  293.    lr_error_message("lenflytype:%d",count);

  294.    lr_error_message("Flytype_1:%s",lr_eval_string("{flytype_1}"));

  295.    lr_error_message("Flytype_2:%s",lr_eval_string("{flytype_2}"));

  296.    lr_error_message("Flytype_3:%s",lr_eval_string("{flytype_3}"));

  297.    lr_error_message("Flytype_4:%s",lr_eval_string("{flytype_4}"));

  298.    for(i=0;i<count;i++)

  299.    {

  300.        sprintf(temp,"{flytype_%d}",i+1);//格式化flytype数组参数格式,取数组中一个存入temp变量

  301.        lr_error_message("temp:%s",temp);//打印temp变量的内容

  302.        sprintf(tempfly,"%s",lr_eval_string(temp));//将temp变量通过lr_eval_string函数将temp变量存储的对应loadrunner参数的对应内容存入tempfly变量中

  303.        lr_error_message("tempfly:%s",tempfly);//打印tempfly变量的内容

  304.        lr_error_message("lr_eval_string(tempfly):%s",lr_eval_string(tempfly));//通过lr_eval_string函数取得tempfly变量的内容

  305.        lr_save_string(tempfly,"flytypenum");//将tempfly变量内容存到flytypenum参数中,方便以后lr函数使用

  306.        lr_error_message("flytypenum:%s",lr_eval_string("{flytypenum}"));//打印flytypenum内容

  307.        web_submit_data("reservations.pl_3",

  308.            "Action=http://127.0.0.1:1080/WebTours/reservations.pl",

  309.            "Method=POST",

  310.            "RecContentType=text/html",

  311.            "Referer=http://127.0.0.1:1080/WebTours/reservations.pl",

  312.            "Snapshot=t24.inf",

  313.            "Mode=HTTP",

  314.            ITEMDATA,

  315.            "Name=outboundFlight", "Value={flytypenum}", ENDITEM,

  316.            "Name=numPassengers", "Value=1", ENDITEM,

  317.            "Name=advanceDiscount", "Value=0", ENDITEM,

  318.            "Name=seatType", "Value=Coach", ENDITEM,

  319.            "Name=seatPref", "Value=None", ENDITEM,

  320.            "Name=reserveFlights.x", "Value=63", ENDITEM,

  321.            "Name=reserveFlights.y", "Value=17", ENDITEM,

  322.            LAST);

  323.  

  324.        web_submit_data("reservations.pl_4",

  325.            "Action=http://127.0.0.1:1080/WebTours/reservations.pl",

  326.            "Method=POST",

  327.            "RecContentType=text/html",

  328.            "Referer=http://127.0.0.1:1080/WebTours/reservations.pl",

  329.            "Snapshot=t25.inf",

  330.            "Mode=HTTP",

  331.            ITEMDATA,

  332.            "Name=firstName", "Value=test001", ENDITEM,

  333.            "Name=lastName", "Value=test001", ENDITEM,

  334.            "Name=address1", "Value=test001", ENDITEM,

  335.            "Name=address2", "Value=123456", ENDITEM,

  336.            "Name=pass1", "Value=test001 test001", ENDITEM,

  337.            "Name=creditCard", "Value=", ENDITEM,

  338.            "Name=expDate", "Value=", ENDITEM,

  339.            "Name=oldCCOption", "Value=", ENDITEM,

  340.            "Name=numPassengers", "Value=1", ENDITEM,

  341.            "Name=seatType", "Value=Coach", ENDITEM,

  342.            "Name=seatPref", "Value=None", ENDITEM,

  343.            "Name=outboundFlight", "Value={flytypenum}", ENDITEM,

  344.            "Name=advanceDiscount", "Value=0", ENDITEM,

  345.            "Name=returnFlight", "Value=", ENDITEM,

  346.            "Name=JSFormSubmit", "Value=off", ENDITEM,

  347.            "Name=.cgifields", "Value=saveCC", ENDITEM,

  348.            "Name=buyFlights.x", "Value=61", ENDITEM,

  349.            "Name=buyFlights.y", "Value=8", ENDITEM,

  350.            LAST);

  351.  

  352.    }

  353.  

  354.  

  355.    web_url("bookanother.gif",

  356.        "URL=http://127.0.0.1:1080/WebTours/images/bookanother.gif",

  357.        "Resource=1",

  358.        "RecContentType=image/gif",

  359.        "Referer=http://127.0.0.1:1080/WebTours/reservations.pl",

  360.        "Snapshot=t26.inf",

  361.        LAST);

  362.  

  363.    lr_end_transaction("book-1",LR_AUTO);

  364.  

  365.    return 0;

  366. }

特别点的解释

首先,LoadRunner是类C语言,因此变量要放到函数的开头声明(C语言的变量)。其次,在for循环中,我们看到了几次sprintf,那么这里了测者会详细说一下,第一次 (假设当前i=0), sprintf(temp,"{flytype_%d}",i+1);,其中“{flytype1}”的含义(包含双引号),是将一个指向了存有flytype_1值的内存地址的指针指向的值赋值给了temp。也就是temp指向了一段存储了fltype_1值的内存。sprintf(tempfly,"%s",lrevalstring(temp));将flytype_1这个指针指向的内存地址存储的值赋给了tempfly,也就是说tempfly和flytype_1这两个指针都指向相同内存地址,这段地址存储的就是关联出来的第一张机票的ouboundFlight值。

————————————————————————————————————————————————

给大家推荐一个学习资料分享群(574253227),里面大牛已经为我们整理好了许多的学习资料,有自动化,接口,性能等等的学习资料!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值