Loadrunner常用函数

一、常用函数:
1、lr_output_message()函数和printf打印类似,如 lr_output_message("%s",x)
2、lr_eval_string("{参数名}");该函数可以从参数中取得对应的值,并转化为字符串
lr_save_string()函數將一個字符串保存為一個參數
參數和變量最大區別:參數必須在雙引號才能應用,而變量使用雙引號則會變成一個字符串

  Action(){
    	lr_output_message(lr_eval_string("{param}"));
    	lr_save_string("http://www.51testing.com", "website");
    	web_url("51testing", "URL={website}",LAST);
    	return 0;
    }

3、lr_save_datetime()函數生成時間

lr_save_datetime("%Y- %m- %d %H:%M:%S", DATE_NOW+TIME_NOW, "datetimes");
lr_save_datetime("%Y- %m- %d ", DATE_NOW, "times");

4、lr_load_dll()函數實現調用外部dll拓展腳本的功能
lr_load_dll(“user32.dll”);

5、lr_free_parameter()可以將參數的值釋放掉,減少內存佔用
lr_save_int()函數可將數字寫入參數

6、lr_param_sprintf()函數,可把一定的格式寫成參數

int index = 56;
char * suffix ="txt";
lr_param_sprintf("LOG_NAME_PARAM", "log_%d.%s", index, suffix,100);
lr_output_message("The new file name is %s", lr_eval_string("{LOG_NAME_PARAM}"));

7、关联函数web_reg_save_param_ex():将服务器返回的内容静下收集过滤
web_set_max_html_param_len(),大多因为关联边界设置不合理导致没有关联到

8、lr_paramarr_idx()函数直接从参数组选取一个随机的值,范围由lr_paramarr_len()决定

二、脚本模式(Script type)两种:
(1)A script describing user actions基于解释用户行为脚本,脚本录制处理类似QTP基于对象识别操作的模式
优点:脚本简洁,基于用户操作模拟,浅显易懂,自身就包含对象检查过程,无需校验
缺点:当页面存在多个同名链接难以区分,故推荐脚本模式A script containing explicit URLs only(e.g.web_url, web_submit_data)

1、web_url();可以模拟用户请求
    web_url("在测试结果显示名称",
    "URL=需要访问地址",
    LAST);
2、模拟用户单击一个超链接的操作,Snapshot=t4.inf(指抓图保存在文件中)
    web_link(“测试名称”,
              “TEXT=连接名”,"Snapshot=t4.inf",
              LAST);
3、若有中文出现编码问题,用UTF-8方式编码,也可以转化UNICODE编码,如下
       lr_convert_string_encoding(
       "门户",
       LR_ENC_SYSTEM_LOCALE,
       LR_ENC_UTF8,
       "param");
       
       例子:
       lr_output_message(lr_eval_string("param"));
       lr_save_string(lr_eval_string("{param}"), "param");
       web_url("phpwind","URL=http://127.0.0.1:8000/phpwi/",LAST)
       web_link("门户","TEXT={param}",LAST);

4、一个页面有多个同名链接,web_link()如何处理?可使用Ord关键字来判断对象次序,也可用在函数中,如下:
    web_link("click link",
    "Text=link",
    Ord=2,
    LAST);
    注意:同名链接顺序是根据HTML代码的解释顺序(左到右,上到下)决定

5、 web_submit_from()和 web_submit_data()表示提交表单,实现HTTP请求中的POST方法;web_link()和web_url()都是页面访问型函数,实现HTTP请求的GET方法
web_submit_from()该函数自动检测当前页面是否存在form表单,然后将后面的ITEMDATA数据进行传送;当页面有多个表单时无法录制得到 web_submit_from()函数,LR会自动使用 web_submit_data()替代,web_submit_data()函数无需前面页面支持,直接发送页面香港数据即可

web_submit_from(
 "searcher.php",
 "Snapshot=t8.inf",
 ITENDATA,
 "Name=keyword", "Value=search",  ENDITEM,
 EXTRARES,
 "URL=js/lang/zh_cn.js", ENDITEM,
 "URL=images/wind85/topbar.png", ENDITEM,
 "URL=images/wind85/nava.png", ENDITEM,
 LAST);
 只需传送keyword数据即可,使用隐藏标记的数据函数会自动处理
 
 web_submit_data(
 "searcher.php",
 "Action=http://localhost:8000/phpwind"
 "Method=POST",
 "TargetFrame=",
 "Snapshot=t8.inf",
 ITENDATA,
 "Name=keyword", "Value=search",  ENDITEM,
 "Name=keyword1", "Value=search1",  ENDITEM,
 EXTRARES,
 "URL=js/lang/zh_cn.js", ENDITEM,
 "URL=images/wind85/topbar.png", ENDITEM,
 "URL=images/wind85/nava.png", ENDITEM,
 LAST)
 );
    web_add_cookie、web_cleanup_cookies();

(2)A script containing explicit URLs only(e.g.web_url, web_submit_data) 基于URL请求的脚本录制类型,与基于用户行为模拟不同在于,这种不用考虑用户操作,只考虑客户端发送请求,注重系统实际做了什么

web_add_cookie("login=admin; DOMAIN=127.0.0.1");
web_url("phpwind8",
           "URL=http://localhost:8000/phpwind8/",
           "TargetFrame=",
           "Resource=0",
           "RecContentType=text/html",
           "Referer=",
           "Snapshot=t1.inf",
           "Mode=HTML",
           EXTRARES,
           "URL=js/lang/zh_cn.js", ENDITEM,
           "URL=images/wind85/topbar.png", ENDITEM,
           "URL=images/wind85/nava.png", ENDITEM,
);
对比第一张脚本模式,web_link()函数被web_url()替代了
EXTRARES,
出现如下大量内容,说明载入这个页面是还有其他图片或附属资源需要下载,由于web_url或web_link函数是使用HTML Mode的,无论脚本中是否存在EXTRARES段,完成函数时都会下载该HTML对应的所有资源,EXTRARES段是一种拓展验证机制,验证对象是否存在,录制得到的EXTRARES不是简单将图片拆出而是主请求请求不到内容,最好不要删除EXTRARES,多了只是多点数据流量,删了可能请求不到,一般不做参数化或关联
 "URL=js/lang/zh_cn.js", ENDITEM,
 "URL=images/wind85/topbar.png", ENDITEM,
 "URL=images/wind85/nava.png", ENDITEM,

(3)web_custom_request()当页面访问请求和数据提交不能使用以上函数表述时使用

web_custom_request(
"web_custom_request",
"URL=http:...",
"Method=GET",
"TargetFrame=",
"Resource=0",
"Referer=",
"Mode=HTML",
"Body=",
LAST);

web_custom_request(
"web_custom_request",
"URL=http:...",
"Method=POST",
"TargetFrame=",
"Resource=0",
"Referer=",
"Mode=HTML",
"Body=jumpurl=&step=2&pwuser=admin&pwpwd=admin&head_login=&lgt=0",
LAST);

例如添加更多链接,列表会增加一项空值,要是POST请求是一个动态长度的内容;
先录制脚本,新增内容都是相同数据段组成,切换视图到Tree模式HTTP view,通过RawData标签获得整个请求数据包,LR会将“[”修改为%5B、“]”修改为%5D,即可找到该段内容;找到区别板块名称关键词name%5B%5D
在这里插入图片描述在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2、调用外部DLL的方法有两种:https://www.cnblogs.com/coderzh/archive/2008/04/02/1135118.html
(单个脚本中)在脚本中是使用 lr_load_dll 函数。
(全局设置,所有脚本)通过修改 mdrv.dat 文件实现。
3、实现动态链接库https://blog.csdn.net/jiang1986829/article/details/48467269
4、替换字符串中指定字符https://blog.csdn.net/flyaway_07/article/details/50260945
5、函数名:strcat
功能:字符串拼接
函数定义:char *strcat( char *to, const char *from);

 char fullpath[1024], * filename = "logfile.txt"; 
    strcpy(fullpath, "c:\\tmp");  
    strcat(fullpath, "\\"); 
    strcat(fullpath, filename);  
    lr_output_message ("Full path of file is %s", fullpath); 
    Output:
    Action.c(9): Full path of file is c:\tmp\logfile.txt

函数名:strcmp
功能:两个字符串比较
函数定义:int strcmp( const char *string1, const char *string2);

 int result; 
  char tmp[20]; 
  char string1[] = "The quick brown dog jumps over the lazy fox"; 
  char string2[] = "The QUICK brown dog jumps over the lazy fox"; 
  result = strcmp( string1, string2); // Case-sensitive comparison 
  if(result > 0) 
      strcpy(tmp, "greater than"); 
  else if(result < 0) 
      strcpy(tmp, "less than"); 
  else 
      strcpy(tmp, "equal to"); 
  lr_output_message ("strcmp: String 1 is %s string 2", tmp); 
  result = stricmp(string1, string2 ); // Case-insensitive comparison 
  if( result > 0 ) 
      strcpy( tmp, "greater than" ); 
  else if( result < 0 ) 
      strcpy( tmp, "less than" ); 
  else 
      strcpy( tmp, "equal to" ); 
  lr_output_message( "stricmp: String 1 is %s string 2", tmp ); 

Output:
Action.c(17): strcmp: String 1 is greater than string 2
Action.c(28): stricmp: String 1 is equal to string 2
返回值	描述
<0	string1 小于string2
0	string1 等于string2
>0	string1 大于string2

函数名:strcpy
功能:将一个字符串复制到另一个字符串
函数定义:char *strcpy( char *dest, const char *source);

 char fullpath[1024], * filename = "logfile.txt"; 
 strcpy(fullpath, "c:\\tmp"); 
 lr_output_message ("fullpath after strcpy: %s", fullpath); 
 strcat(fullpath, "\\"); 
 strcat(fullpath, filename); 
 lr_output_message ("Full path of file is %s", fullpath);} 
Output:
Action.c(6): fullpath after strcpy: c:\tmp
Action.c(10): Full path of file is c:\tmp\logfile.txt 

函数名:strlen
功能:返回指定字符串的长度
函数定义:size_t strlen( const char *string );

 int is_digit, i = 0; 
 char * str = "1234567k89"; 
 unsigned int len = strlen(str); 
 do { 
     if (i == len) { 
         lr_output_message ("No letters found in string"); 
         return -1; 
     } 
     is_digit = isdigit(str[i++]); 
 } while (is_digit); 
 lr_output_message ("The first letter appears in character %d of string", i);} 
Output:
Action.c(18): The first letter appears in character 8 of string<strong>
</strong>

该段来源:https://blog.csdn.net/jiang1986829/article/details/48545761

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值