最近在論壇上看到有人提問LoadRunner如何對兩個字符串進行比較,其腳本中兩個字符串進行比較結果總是不一樣的。我把問題整理了一下以便注意這個容易被忽略的錯誤。
腳本如下:
...
lr_save_string( "Hello World!","string1" );
lr_save_string( "Hello World!","string2" );
result = strcmp("string1","string2");
//如果result等於0說明2個字符串相等
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}"));