zoukankan      html  css  js  c++  java
  • loadrunner提高篇

      关联函数是一个查找函数,即是从HTML文件内容中查找需要的值,并将其保存在一个变量或数组中。换一个角度看,关联函数不单单可以匹配一些变化的值,同样可以匹配一些固定的内容,并将其保存到一个数据组,供后续业务使用,也即对关联函数进行扩展使用,在处理事务中这种方式经常用到。

      实例:以订票为例,假设要实现这样的事务,选择好出发城市和到达城市后,在选择航班时,需要选择票价最便宜的航班

      实现思路:首先使用关联函数读取HTML代码中票价和航班号,分别将两部分的数据保存在不同的数组中;接着比较票价,选择找到票价最低的是数组中的第几个元素,即最便宜机票所在数据的下标;最后将该下标的值对应地读出航班号,就是最便宜的航班。

      完整代码如下:

    【注:还没运行成功,主要是不懂代码函数的运用】

    Action()
    {
    //录制订票脚本,为了练习关联技术的经典使用
        web_url("WebTours",
            "URL=http://127.0.0.1:1080/WebTours",
            "Resource=0",
            "RecContentType=text/html",
            "Referer=",
            "Snapshot=t2.inf",
            "Mode=HTTP",
            LAST);
    
        web_url("header.html",
            "URL=http://127.0.0.1:1080/WebTours/header.html",
            "Resource=0",
            "RecContentType=text/html",
            "Referer=http://127.0.0.1:1080/WebTours/",
            "Snapshot=t4.inf",
            "Mode=HTTP",
            LAST);
    
        web_concurrent_start(NULL);
    
        web_url("hp_logo.png",
            "URL=http://127.0.0.1:1080/WebTours/images/hp_logo.png",
            "Resource=1",
            "RecContentType=image/png",
            "Referer=http://127.0.0.1:1080/WebTours/header.html",
            "Snapshot=t5.inf",
            LAST);
    
        web_url("webtours.png",
            "URL=http://127.0.0.1:1080/WebTours/images/webtours.png",
            "Resource=0",
            "RecContentType=text/html",
            "Referer=http://127.0.0.1:1080/WebTours/header.html",
            "Mode=HTTP",
            LAST);
    
        web_concurrent_end(NULL);
    
        web_url("welcome.pl",
            "URL=http://127.0.0.1:1080/WebTours/welcome.pl?signOff=true",
            "Resource=0",
            "RecContentType=text/html",
            "Referer=http://127.0.0.1:1080/WebTours/",
            "Snapshot=t6.inf",
            "Mode=HTTP",
            LAST);
    
        web_concurrent_start(NULL);
    
        web_url("nav.pl",
            "URL=http://127.0.0.1:1080/WebTours/nav.pl?in=home",
            "Resource=0",
            "RecContentType=text/html",
            "Referer=http://127.0.0.1:1080/WebTours/welcome.pl?signOff=true",
            "Snapshot=t7.inf",
            "Mode=HTTP",
            LAST);
    
        web_url("home.html",
            "URL=http://127.0.0.1:1080/WebTours/home.html",
            "Resource=0",
            "RecContentType=text/html",
            "Referer=http://127.0.0.1:1080/WebTours/welcome.pl?signOff=true",
            "Snapshot=t8.inf",
            "Mode=HTTP",
            LAST);
    
        web_concurrent_end(NULL);
    
        web_url("mer_login.gif",
            "URL=http://127.0.0.1:1080/WebTours/images/mer_login.gif",
            "Resource=1",
            "RecContentType=image/gif",
            "Referer=http://127.0.0.1:1080/WebTours/nav.pl?in=home",
            "Snapshot=t9.inf",
            LAST);
    
        web_concurrent_start(NULL);
    
        web_url("8afc2fe48db9060fe1bdda2089e1d950.png",
            "URL=http://act.cmcmcdn.com/upload/201507/8afc2fe48db9060fe1bdda2089e1d950.png",
            "Resource=1",
            "RecContentType=image/png",
            "Referer=http://127.0.0.1:1080/WebTours/",
            "Snapshot=t10.inf",
            LAST);
    
        web_url("3b491068507d8f85ea7b35d756da7215.png",
            "URL=http://act.cmcmcdn.com/upload/201507/3b491068507d8f85ea7b35d756da7215.png",
            "Resource=1",
            "RecContentType=image/png",
            "Referer=http://127.0.0.1:1080/WebTours/",
            "Snapshot=t11.inf",
            LAST);
    
        web_concurrent_end(NULL);
    
        lr_start_transaction("登录");
    
        lr_think_time(31);
    
        web_submit_data("login.pl",
            "Action=http://127.0.0.1:1080/WebTours/login.pl",
            "Method=POST",
            "RecContentType=text/html",
            "Referer=http://127.0.0.1:1080/WebTours/nav.pl?in=home",
            "Snapshot=t12.inf",
            "Mode=HTTP",
            ITEMDATA,
            "Name=userSession", "Value=120844.899311462zcVfQfQpfAiDDDDDDHcQfpHfHAf", ENDITEM,
            "Name=username", "Value=test1", ENDITEM,
            "Name=password", "Value=test1", ENDITEM,
            "Name=JSFormSubmit", "Value=off", ENDITEM,
            "Name=login.x", "Value=36", ENDITEM,
            "Name=login.y", "Value=15", ENDITEM,
            LAST);
    
        web_concurrent_start(NULL);
    
        web_url("nav.pl_2",
            "URL=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=home",
            "Resource=0",
            "RecContentType=text/html",
            "Referer=http://127.0.0.1:1080/WebTours/login.pl",
            "Snapshot=t13.inf",
            "Mode=HTTP",
            LAST);
    
        web_url("login.pl_2",
            "URL=http://127.0.0.1:1080/WebTours/login.pl?intro=true",
            "Resource=0",
            "RecContentType=text/html",
            "Referer=http://127.0.0.1:1080/WebTours/login.pl",
            "Snapshot=t18.inf",
            "Mode=HTTP",
            LAST);
    
        web_concurrent_end(NULL);
    
        web_concurrent_start(NULL);
    
        web_url("flights.gif",
            "URL=http://127.0.0.1:1080/WebTours/images/flights.gif",
            "Resource=1",
            "RecContentType=image/gif",
            "Referer=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=home",
            "Snapshot=t14.inf",
            LAST);
    
        web_url("in_home.gif",
            "URL=http://127.0.0.1:1080/WebTours/images/in_home.gif",
            "Resource=1",
            "RecContentType=image/gif",
            "Referer=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=home",
            "Snapshot=t15.inf",
            LAST);
    
        web_url("signoff.gif",
            "URL=http://127.0.0.1:1080/WebTours/images/signoff.gif",
            "Resource=1",
            "RecContentType=image/gif",
            "Referer=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=home",
            "Snapshot=t16.inf",
            LAST);
    
        web_url("itinerary.gif",
            "URL=http://127.0.0.1:1080/WebTours/images/itinerary.gif",
            "Resource=1",
            "RecContentType=image/gif",
            "Referer=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=home",
            "Snapshot=t17.inf",
            LAST);
    
        web_concurrent_end(NULL);
    
        lr_end_transaction("登录", LR_AUTO);
    
        lr_start_transaction("订票");
    
        lr_think_time(11);
    
        web_url("Search Flights Button",
            "URL=http://127.0.0.1:1080/WebTours/welcome.pl?page=search",
            "Resource=0",
            "RecContentType=text/html",
            "Referer=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=home",
            "Snapshot=t19.inf",
            "Mode=HTTP",
            LAST);
    
        web_concurrent_start(NULL);
    
        web_url("reservations.pl",
            "URL=http://127.0.0.1:1080/WebTours/reservations.pl?page=welcome",
            "Resource=0",
            "RecContentType=text/html",
            "Referer=http://127.0.0.1:1080/WebTours/welcome.pl?page=search",
            "Snapshot=t20.inf",
            "Mode=HTTP",
            LAST);
    
        web_url("nav.pl_3",
            "URL=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=flights",
            "Resource=0",
            "RecContentType=text/html",
            "Referer=http://127.0.0.1:1080/WebTours/welcome.pl?page=search",
            "Snapshot=t21.inf",
            "Mode=HTTP",
            LAST);
    
        web_concurrent_end(NULL);
    
        web_url("button_next.gif",
            "URL=http://127.0.0.1:1080/WebTours/images/button_next.gif",
            "Resource=1",
            "RecContentType=image/gif",
            "Referer=http://127.0.0.1:1080/WebTours/reservations.pl?page=welcome",
            "Snapshot=t22.inf",
            LAST);
    
        web_concurrent_start(NULL);
    
        web_url("in_flights.gif",
            "URL=http://127.0.0.1:1080/WebTours/images/in_flights.gif",
            "Resource=1",
            "RecContentType=image/gif",
            "Referer=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=flights",
            "Snapshot=t23.inf",
            LAST);
    
        web_url("home.gif",
            "URL=http://127.0.0.1:1080/WebTours/images/home.gif",
            "Resource=1",
            "RecContentType=image/gif",
            "Referer=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=flights",
            "Snapshot=t24.inf",
            LAST);
    
        web_concurrent_end(NULL);
    
        lr_think_time(5);
    /*分析票价和航班号的左右边界值后(可通过航班信息的HTML源代码分析),在这里插入关联函数,进行关联时Ordinal的属性必须设置为all,这样才可以将所有的值匹配到,并将其保存在一个数组中。*/
    
        //这是航班的关联函数
        web_reg_save_param_ex(
            "ParamName=fligthno",
            "LB=outboundFlight value=",
            "RB=;",
            "Ordinal=all",
            SEARCH_FILTERS,
            LAST);
    
        //这是票价的关联函数
        web_reg_save_param_ex(
            "ParamName=fligthcost",
            "LB=center>$",
            "RB=</TD>",
            "Ordinal=all",
            SEARCH_FILTERS,
            LAST);
    /*设置一个标记flag来保存票价*/
    
        //初始化最贵机票,将第一个航班的机票设置为初始化的最贵的机票
        max = atoi(lr_eval_string ("{wcsparam_text1_1}"));
        //初始化标识位,默认设置为1
        flagno=1;
        //for循环所有机票
        for (i=2;i<=atoi(lr_eval_string ("{wcsparam_text1_count}"));i++) {
            sprintf(cost,"{wcsparam_text1_%d}",i);
        //比较最前航班的机票是否大于max的值,如果大于max的值,即重新对max赋值
                if(atoi(lr_eval_string (cost))>max){
                    max=atoi(lr_eval_string (cost));
                    flagno=i;
                }
        }
    
    /*找到最便宜的机票后,可以得到该数组的下标,那这个数组的下标所对应的航班就是票价最便宜的航班,获取航班号的代码如下:*/
    
        //通过标识位来确定航班
        sprintf(flightelem,
                "{wcsparam_text2_%d}",
                flagno);
        lr_save_string (lr_eval_string (flightelem),
                        "fligthno");
    
        web_submit_data("reservations.pl_2",
            "Action=http://127.0.0.1:1080/WebTours/reservations.pl",
            "Method=POST",
            "RecContentType=text/html",
            "Referer=http://127.0.0.1:1080/WebTours/reservations.pl?page=welcome",
            "Snapshot=t25.inf",
            "Mode=HTTP",
            ITEMDATA,
            "Name=advanceDiscount", "Value=0", ENDITEM,
            "Name=depart", "Value=Denver", ENDITEM,
            "Name=departDate", "Value=04/11/2017", ENDITEM,
            "Name=arrive", "Value=London", ENDITEM,
            "Name=returnDate", "Value=04/12/2017", 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=48", ENDITEM,
            "Name=findFlights.y", "Value=10", ENDITEM,
            LAST);
    
        lr_think_time(4);
    
    /*获得最便宜的票价和航班后,即将这两部分的信息放入到提交的信息中,作为客户端的请求进行提交即可,具体代码如下*/
        web_submit_data("reservations.pl_3",
            "Action=http://127.0.0.1:1080/WebTours/reservations.pl",
            "Method=POST",
            "RecContentType=text/html",
            "Referer=http://127.0.0.1:1080/WebTours/reservations.pl",
            "Snapshot=t26.inf",
            "Mode=HTTP",
            ITEMDATA,
            //"Name=outboundFlight", "Value=023;519;04/11/2017", ENDITEM,
            "Name=outboundFlight", "Value={flightno};{flightnocost};04/11/2017", 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=43", ENDITEM,
            "Name=reserveFlights.y", "Value=6", ENDITEM,
            LAST);
    
        web_submit_data("reservations.pl_4",
            "Action=http://127.0.0.1:1080/WebTours/reservations.pl",
            "Method=POST",
            "RecContentType=text/html",
            "Referer=http://127.0.0.1:1080/WebTours/reservations.pl",
            "Snapshot=t27.inf",
            "Mode=HTTP",
            ITEMDATA,
            "Name=firstName", "Value=test1", ENDITEM,
            "Name=lastName", "Value=test1", ENDITEM,
            "Name=address1", "Value=", ENDITEM,
            "Name=address2", "Value=", ENDITEM,
            "Name=pass1", "Value=test1 test1", ENDITEM,
            "Name=creditCard", "Value=", ENDITEM,
            "Name=expDate", "Value=", ENDITEM,
            "Name=oldCCOption", "Value=", ENDITEM,
            "Name=numPassengers", "Value=1", ENDITEM,
            "Name=seatType", "Value=First", ENDITEM,
            "Name=seatPref", "Value=Window", ENDITEM,
            //"Name=outboundFlight", "Value=023;519;04/11/2017", ENDITEM,
            "Name=outboundFlight", "Value={flightno};{flightnocost};04/11/2017", ENDITEM,
            "Name=advanceDiscount", "Value=0", ENDITEM,
            "Name=returnFlight", "Value=", ENDITEM,
            "Name=JSFormSubmit", "Value=off", ENDITEM,
            "Name=.cgifields", "Value=saveCC", ENDITEM,
            "Name=buyFlights.x", "Value=48", ENDITEM,
            "Name=buyFlights.y", "Value=6", ENDITEM,
            LAST);
    
        web_url("bookanother.gif",
            "URL=http://127.0.0.1:1080/WebTours/images/bookanother.gif",
            "Resource=1",
            "RecContentType=image/gif",
            "Referer=http://127.0.0.1:1080/WebTours/reservations.pl",
            "Snapshot=t28.inf",
            LAST);
    
        lr_end_transaction("订票", LR_AUTO);
    
    return 0;
    }

       附上航班信息源码:

    <!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
    <head>
    <title>Flight Selections</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    </head>
    <body bgcolor="#E0E7F1">
    <style>blockquote {font-family: tahoma; font-size : 10pt}H1 {font-family: tahoma; font-size : 22pt; color: #993333}small {font-family: tahoma; font-size : 8pt}H3 {font-family: tahoma; font-size : 10pt; color: black}TD {font-family: tahoma; font-size : 10pt; color: black}</style><BR><BR><h1><font color=#003366>&nbsp;&nbsp;<b>Find Flight</font></b></h1><blockquote>
    <form method="post" action="reservations.pl" enctype="application/x-www-form-urlencoded">
    
    <center>
    <!-- From Denver (0) To London (2) -->
    <table cellspacing=2 border=0 width=50%>
    <blockquote>Flight departing from <B>Denver</B> to <B>London</B> on <B>04/11/2017</B><BR><BR><tr bgcolor=#5E7884><td align=center><font color=white><B>Flight</B></font>
                  <td align=center><font color=white><B>Departure time</B>
                  <td align=center><font color=white><B>Cost</B>
    //从下面可以看出航班(020、021...)的      左边界是 outboundFlight value=               右边界是  ;
    //              票价($ 635、$ 565...)的  左边界是 center>$                            右边界是  </TD>
    <tr bgcolor=#EFF2F7>
        <td align=center><input type = radio name=outboundFlight value=020;635;04/11/2017 checked >Blue Sky Air 020<td align=center>8am<td align=center>$ 635</TD>
    </TR>
    <tr bgcolor=#EFF2F7>
        <td align=center><input type = radio name=outboundFlight value=021;565;04/11/2017>Blue Sky Air 021<td align=center>1pm<td align=center>$ 565</TD>
    </TR>
    <tr bgcolor=#EFF2F7>
        <td align=center><input type = radio name=outboundFlight value=022;600;04/11/2017>Blue Sky Air 022<td align=center>5pm<td align=center>$ 600</TD>
    </TR>
    <tr bgcolor=#EFF2F7>
        <td align=center><input type = radio name=outboundFlight value=023;519;04/11/2017>Blue Sky Air 023<td align=center>11pm<td align=center>$ 519</TD>
    </TR>
    </table>
    
    <input type="hidden" name="numPassengers" value="1"  /><input type="hidden" name="advanceDiscount" value="0"  /><input type="hidden" name="seatType" value="First"  /><input type="hidden" name="seatPref" value="Window"  />
    <P><center><table width=80% cellspacing=1><tr><td align=center>
    <input type="image" name="reserveFlights" src="/WebTours/images/button_next.gif" border="0" />
    <td>&nbsp;<td align=center></table></center></form></center>
    </blockquote>
    
    </body>
    </html>
    航班信息
  • 相关阅读:
    整数反转问题--正确率极低
    May LeetCoding Challenge9 之 求方差
    May LeetCoding Challenge8 之 交叉相乘
    EXCEL上传DEMO
    三层BOM
    OO alv 获取选择列
    PS 项目创建
    读取EXCEL到内表
    SAP XML 到 内表
    BOM 复制功能
  • 原文地址:https://www.cnblogs.com/Chilam007/p/6670998.html
Copyright © 2011-2022 走看看