#include “web_api.h”
char* randstring(int slen) {
char* result = (char*)malloc(slen + 1); //在loadrunner使用的C98语法,需要将所有定义都定义在函数头部,否则提示报错illegal statement termination
char character_set[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int character_set_length = 52; //在loadrunner中使用sizeof计算字符串长度会提示Syntax error on line 5 near "character set'
int i = 0;
int randid;
if (result == NULL) return NULL; for (i; i < slen; i++) { randid = rand() % character_set_length; result[i] = character_set[randid];
} result[slen] = '\0';
return result;
}
在Action()内,添加:
int length = 8; lr_save_string(lr_eval_string(randstring(length)),"randomStr");
在引用参数处,使用:{randomStr}
在此处我们要明白lr_save_string 与lr_eval_string的区别:
int lr_save_string( const char *param_value, const char *param_name); //将变量或常量保存为参数;
函数目的:将param_value值保存到param_name变量中
参数介绍:param_value:要保存的值
param_name: 变量名称
char *lr_eval_string( const char *instring ); //返回脚本中的一个参数当前的值;
eg:
char *temp=“hello”;
lr_save_string(“777”,“param1”);
lr_output_message(lr_eval_string(“{param1}”));
lr_save_string(temp,“param1”);
lr_output_message(lr_eval_string(“{param1}”));
结果:777
hello