最近在论坛上看到有人提问LoadRunner如何对两个字符串进行比较,其脚本中两个字符串进行比较结果总是不一样的。我把问题整理了一下以便注意这个容易被忽略的错误。
脚本如下:
...
lr_save_string( "Hello World!","string1" );
lr_save_string( "Hello World!","string2" );
result = strcmp("string1","string2");
if ( result == 0 )
{
lr_output_message("the result is 0.");
}
else
{
lr_output_message("the result is not 0.");
}
大家可以看出脚本那里错了吗?
问题错在result = strcmp("string1","string2");这个上,这样变成了对字符串"string1"和"string2"的比较,而不是对变量的值进行比较,因此比较结果肯定是不一样的。
正确的写法有两种:
result = strcmp(&string1,&string2);
result = strcmp(lr_eval_string("{string1}"),lr_eval_string("{string2}"));
本文详细解析了LoadRunner脚本中字符串比较的常见错误及正确做法。通过具体案例,指出直接比较字符串名称而非其值的问题,并提供两种有效的解决方法。
961

被折叠的 条评论
为什么被折叠?



