LoadRunner,你在找的常用函数

实际,Loadrunner的函数使用在软件自带手册中都有说明,可能英文大家看起来有点困难,现在这里详细对常用的函数罗列下,方便大家使用。

事务函数:

lr_start_transaction(); //标记事务的开始
lr_start_transaction(); /*标记事务的结束,一般情况下,事务开始与结束联合使用*/
lr_get_trans_instance_status(); //得到事务的状态
lr_get_transaction_think_time(); //得到事务的指定思考时间,事务时间=整体事务时间-事务的
lr_stop_transaction(); //停止事务

日志函数:
lr_debug_message(); //发送调试信息到日志文件
lr_error_message(); //发送错误信息到LR输入窗口或日志文件里
lr_get_debug_message(); //返回当前调试的信息
lr_log_message(); //发送信息到用户日志文件
lr_output_message(); //发送信息到输出窗口或日志文件
lr_vuser_status_message(); //发送虚拟用户的状态到LR的Controller

运行时函数:

lr_abort(); //终止执行的脚本
lr_continue_on_error(); //当发生错误后运行的事件
lr_exit(); //从scirpt.ation.iteration中退出
lr_rendezvous_ex(); //设置集合点
lr_think_time(); //设置思考时间,在性能测试中为了更好模拟以后操作,可以根据实际生产环境设置思考时间。
lr_rendezous(); //集合点,可以设置虚拟用户相同操作之前进行集合,通过集合点可以增加被测试应用的压力,从而达到压力测试的目的。

lr_load_dll(); //调用DLL文件
例子:
lr_load_dll("user32.dll");
MessageBoxA(NULL,"This is Testing Message","message_caption",0);
return 0;

系统信息函数:
lr_get_master_host_name(); //返回运行Controller主机的名称
lr_get_user_ip(); //返回虚拟用户的IP地址
lr_user_data_point(); //录制用户自定义的数据样例
lr_user_data_point(); // 记录自己的数据来进行分析,每次要记录一个点时,请使用该函数记录采样名称和值。系统将自动记录采样的时间。执行之后,可以使用用户等义的数据,点击图形分析结果

lr_get_host_name(); //返回主机名
例子:
char * host;
host=lr_get_host_name();
lr_output_message("Computer Name %s",host);
return 0;

WEB应用中常见函数

web_url(); //根据函数中的URL属性加载对应的URL,不需要上下文。
web_image(); //模拟鼠标在指定图片上的单击动作。此函数必须在有前置操作的上下文中使用。
Tips:在Toos—Recording Option,如果录制级别设为基于HMTL的录制方式时,web_image才会被录制到。
Web_link(); //模拟鼠标在由若干个属性集合描述的链接上进行单击。此函数必须在前置动作的上下文中才可以执行。
web_submit_form (); //函数用来提交表单。此函数可能必须在前一个操作的上下文中执行。
Tips:支持Web虚拟用户,不支持WAP虚拟用户。 
web_submit_data(); //函数处理无状态或者上下文无关的表单提交。它用来生成表单的GET或POST请求,这些请求与Form自动生成的请求是一样的。发送这些请求时不需要表单上下文。
web_find(); //此函数的作用是在HTML页面中查找指定的字符串。当指定的HTML请求全部完成以后,开始执行搜索过程,比web_reg_find要慢。
web_find函数在C语言的脚本中已经被web_reg_find所替代,web_reg_find运行速度比较快,而且在HTML-based和URL-based的录制方式中都可以使用。 在C语言脚本中,web_find是向后兼容的。Java和Visual Basic脚本中不再支持它。
web_image_check(); //检查指定的图象是否在HTML页面中出现,此函数仅仅支持基于HTML的脚本。
web_reg_add_cookie(); //是注册类型的函数。它首先注册一个搜索文本字符串的请求。检查动作在后续的Action函数之后进行。如果字符串被找到,就添加到cookie中。
web_reg_find(); //属于注册函数,注册一个在web页面中搜索文本字符串的请求,在接下来的Action(象web_url)类函数中执行搜索。
web_concurrent_start(); //函数是并发组开始的标记。组中所有的函数是并发执行的。并发组的结束web_concurrent_end 函数。
web_reg_save_param(); //关联函数,通过关联可以在测试中保持动态值,从服务器返回的数据库中查找需要关联的数据。
web_add-filter(); // 过滤函数,用于对指定的URL进行过滤,分析URL加载那部分对性能有影响。界面操作Run-time Settings 下的 Internet Protocol的Download Filters。
web_custom_request(); // 自定义请求函数,可以编写自定义请求格式进行接口功能测试。

FTP /IMAP方面:

ftp_logon_ex(); //针对特定会话登录到FTP服务器。
ftp_put_ex(); //在FTP服务器上设置工作目录及上传文件。
ftp_logout_ex(); //注销当前FTP连接。
imap_create(); //创建新的邮件。
imap_check(); //邮件中请求检查点,实现并适用与邮箱内部的内务管理。
Tips:针对IMAP的测试,把函数中的ftp替换成imap即可。

其它常用函数:

Lr_decrypt()  //LR中的解密函数
实例代码

lr_output_message("解密函数测试,解密后数值:%s",lr_decrypt("4e0942869c958e3e"));

Getenv() //得到定义的环境变量值

实例代码:    
char *tmp,logfile[256],dlr_seperator;
logfile;
//Create an environment variable
putenv("LOGFILE_NAME=lr_xiaolintest.txt");
if(tmp = (char*)getenv("TEMP"))
    lr_output_message("Temp Dir = %s",tmp);
else{
    lr_output_message("TEMP environment variable undefined");
    return -1;
}

sprintf(logfile,"%s\%s",tmp,(char*)getenv("LOGFILE_NAME"));

System() //执行操作系统的命令 

char filename[1024],command[1024];
char new_dir[] = "c:\test";
//Create a directory udder root called test and make it the current dir
if(mkdir(new_dir))
    lr_output_message("Create directory :%s failed",new_dir);
else
    lr_output_message("Create new directory %s",new_dir);
    sprintf(filename,"%s\%s",new_dir,"xialin.txt");
    //Executr a dir /b command and directory it to a new_file
    sprintf(command,"dir /b c:\ > %s /w",filename);
    system(command)
    lr_output_message("Create new file %s",filename);

Rand() //得到一个整型的随机值(0到32767)

srand(time(NULL));
//Generate a random number from 0-99
lr_output_message("A number between 0 and 99 is :%d ",rand()%100);
return 0;

Getdrive() and mkdir():返回当前驱动盘的名字。

int ch,drive,curdrive;
static char path[1024];
//Save current drive letter so it can be restored later
curdrive = getdrive();
//If we can switch to the drive,it exists
lr_output_message("Available drives are:");
for(drive = 1;drive <= 26;drive++)
  if (!chdrive(drive))
    lr_output_message("%c:",drive + 'A' -1);
    chdrive(curdrive);//Restore original drive
return 0;

Time() //返回系统的时间

typedef long time_t;
time_t t;
//Get system time and display as number and string
lr_message("Time in seconds since 1/1/88: %ld ",time(&t));
lr_message("Formatted time and date: %s",ctime(&t));
return 0;



留言