loadrunner 关联函数web_reg_save_param

当我们每次访问网站都需要提交从服务器获取的动态文本时就会需要用到关联函数,就好像每次乘坐火车票我们都需要用最新的火车票,如果用旧车票就不能做火车,如果我们采用了录制时的旧动态码如usersession再次访问服务器,跟服务器通信就是失败,于是我们就需要用到web_reg_save_param函数将服务器反馈的动态码保存成文本字符串到一个参数,而这个函数必须要放在动作函数之前,如web_url,放在动作函数之后就无法取到想要的数据。

第一,函数各参数描述

int web_reg_save_param( const char *ParamName, <List of Attributes>, LAST );  

1. ParaName:要创建的参数名,用来保存获取的动态文本

2. List of Attributes包含:

Convert:可选属性

HTML_TO_URL:将html编码的数据转换为url编码的数据格式

HTML_TO_TEXT:将html编码的数据转换为纯文本格式

LB:左边界,不支援正则表达式,如果没有指定则从数据开始的字符算

NOTFOUND:

"Notfound=error",默认设置,当找不到边界时触发一个错误

"Notfound=warning",如果找不到边界,触发一个警告,继续运行脚本

两种设置触发的log message如下2条,一条显示错误,一条显示警告。

submit_login.c(6): 错误 -26377: 找不到所请求参数“usersession”的匹配项。请检查响应数据中是否存在请求的边界。此外,如果要保存的数据超过 256 字节,请使用 web_set_max_html_param_len 增加参数大小 [MsgId: MERR-26377]
submit_login.c(6): 通知: 保存参数“usersession = ”。
submit_login.c(6): web_submit_data("login.pl") 最高严重级别为“ERROR”,795 个正文字节,225 个标头字节 [MsgId: MMSG-26388]

submit_login.c(6): 警告 -26377: 找不到所请求参数“usersession”的匹配项。请检查响应数据中是否存在请求的边界。此外,如果要保存的数据超过 256 字节,请使用 web_set_max_html_param_len 增加参数大小      [MsgId: MWAR-26377]
submit_login.c(6): 通知: 保存参数“usersession = ”。
submit_login.c(6): web_submit_data("login.pl") 最高严重级别为“warning”,795 个正文字节,225 个标头字节      [MsgId: MMSG-26388]

ORD:默认设置为1,把查找到的第1个符合边界设置的文本字串赋值给变量;如果设置为All则把所有找到的结果都赋值给变量,而变量则变成数组类型

RB:右边界,不支援正则表达式,如果为空则使用直到数据结束的所有字符作为边界。

RelFrameID:可选,在GUI脚本中不支持

SaveLen:可选,从指定偏移量中找到的值的子字符串的长度,以便保存到参数中。默认是-1,表示保存到字符串末尾

SaveOffset:可选,找到值的子字符串的偏移量,以保存到参数中。

以下查找到的子字符“125219.910247064zttAQQQpVDDDDDDDDiztfpQQti”,如果设置"SaveLen=5","SaveOffset=8"则保存到参数中的文本为10247。

Search:可选,查找范围,可能的值是header(只搜索header)、Body(只搜索Body数据,而不是Headers)、Noresource(只搜索HTML Body,不包括所有header和资源)或all(搜索Body、Headers和资源)。默认值为ALL

第二,实际应用

Loadrunner自带的飞机订票系统,在登陆的时候需要提供usersession,这是一个动态码,每次访问打开网站都会不同,我们就用关联函数来获取这个值。

查看网页源代码:

</style>
<form method=post action=error.pl  onSubmit='doJSFormSubmit1(this)' target=body>
<input type=hidden name=userSession value=125220.012555691zttVAAtpDQfiDDDDDiztzpAczDHf>
<table border=0><tr><td>&nbsp;</td>

找到userSession所在位置,在设定左右边界时,最好先在源代码中查找一下看设定哪个边界查找结果为1,如设定左边界为“Value=”,又边界为“>“,取到的usersession不一定是我们想要的值。

Action()
{
    web_reg_save_param("usersession","LB=userSession value=","RB=>",LAST);

    web_url("WebTours", 
        "URL=http://127.0.0.1:1080/WebTours/", 
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=", 
        "Snapshot=t2.inf", 
        "Mode=HTML", 
        LAST);
    web_reg_find("Text=Welcome, <b>llll</b>","SaveCount=count",LAST);

    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=t4.inf", 
        "Mode=HTML", 
        ITEMDATA, 
        "Name=userSession", "Value={usersession}", ENDITEM, 
        "Name=username", "Value=llll", ENDITEM, 
        "Name=password", "Value=1234", ENDITEM, 
        "Name=JSFormSubmit", "Value=off", ENDITEM, 
        "Name=login.x", "Value=43", ENDITEM, 
        "Name=login.y", "Value=10", ENDITEM, 
        LAST);
    if(atoi(lr_eval_string("{count}"))>0)
        lr_output_message("%s login pass",lr_eval_string("{username}"));
    else
        lr_output_message("%s login fail",lr_eval_string("{username}"));
    lr_think_time(4);

    web_url("welcome.pl", 
        "URL=http://127.0.0.1:1080/WebTours/welcome.pl?signOff=1", 
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=home", 
        "Snapshot=t5.inf", 
        "Mode=HTML", 
        LAST);

    return 0;
}

第三,查找结果多个的使用方法:

在LR自带的飞机登陆网站,源代码如下,我们用Web_reg_save_param关联函数来关联href内容,期待结果应该是源代码中标记蓝色底的部分。

<HTML>
<HEAD>
    <title>Web Tours</title>
</head>
<style> 
    blockquote {font-family: tahoma; font-size : 10pt}
    H1 {font-family: tahoma; font-size : 22pt; color: #003366}
    small {font-family: tahoma; font-size : 8pt}
    H3{font-family: tahoma; font-size : 10pt; color: black}
    A {FONT-WEIGHT: bold; FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Tahoma; TEXT-DECORATION: none}
    A:hover{ FONT-WEIGHT: bold; FONT-SIZE: 10pt; COLOR: #993333; FONT-FAMILY: Tahoma;TEXT-DECORATION: underline}
    TD {font-family: tahoma; font-size : 10pt; color: black}
</style>
<body bgcolor=#e0e7f1>
<BR>
<BR>
<BR>
<br >
<table border=0>
<tr><td></td></tr>
<TR><TD></TD></TR>
<TR><TD>
<blockquote>
<H1><b>Web Tours</b></H1>
Welcome to the Web Tours site.
<br>
To make reservations,please enter your account information to the left.
<br>
If you haven't registered yet, <A href="login.pl?username=&amp;password=&amp;getInfo=true" ><B>sign up now</B></A> to get access to all our resources.
<br>
To configure the server options, use <A href="admin.pl" target=body ><b>administration</b></A> link.
<br><br><br><br><br><br>
<br><small>
This product uses parts of the SMT Kernel, Copyright (c) 1991-99 <A href="http://www.imatix.com" target=new><B><small>iMatix Corporation</small></B></A> 
</small>
</blockquote></TD>
<TD></TD></TR>
</table>
</body>
</html>

设置左边界LB为href=",右边界RB为",要保存所有的href到参数所以ORD设置成all,下边是代码内容:

Action()
{
    int i=1,count=0;
    char hrefname[50];
    web_reg_save_param("hrefs","LB=href="","RB="","ORD=all",LAST);//把所有找到的href值保存到hrefs数组中

    //打开hp lr自带的飞机订票系统
    web_url("WebTours", 
        "URL=http://127.0.0.1:1080/WebTours/", 
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=", 
        "Snapshot=t2.inf", 
        "Mode=HTML", 
        LAST);

    count=atoi(lr_eval_string("{hrefs_count}"));//总共找到的href项目,把总数保存到count
    lr_output_message("Number of items found = %d",count);
    for (i=1;i<=count;i++) {
        sprintf(hrefname,"{hrefs_%d}",i);//将hrefs参数名称格式化到变量hrefname中
        lr_output_message("%s=%s",hrefname,lr_eval_string(hrefname));//输出各href值
    }

    return 0;
}

输出结果符合我们在源代码中标记的绿底部分.

Action.c(17): Number of items found = 3
Action.c(20): {hrefs_1}=login.pl?username=&amp;password=&amp;getInfo=true
Action.c(20): {hrefs_2}=admin.pl
Action.c(20): {hrefs_3}=http://www.imatix.com
原文地址:https://www.cnblogs.com/springyun/p/10182765.html