LoadRunner腳本編寫(6)— 數據類型轉換和字符串操作
一,數據類型轉換
沒有使用過C編程的LoadRunner腳本編寫者會發現在數據類型轉化方面比較困難。下面介紹這方面的知識。
1. 相似函數的輸出在不同的位置
象很多C函數一樣,使用atoi函數的結果即為返回值
如intResult = atoi( charY );
而:itoa的返回結果為第二個參數。
itoa( intX, charY, 10);
第一個參數是需要轉換的數字,第二個參數是轉換后存儲的字符數組,需要注意的是數組必須定義為固定的長度,如:char chary[20];
數組的最大長度為32064(32K),否則會出現“too many variables”編譯錯誤。
如果定義為變長的字符串如char *charY,則程序會出錯。
第三個參數不是數組的長度,而是數字的基數,10進制是最常用的,其他還有二進制,八進制,十六進制。
2. 有一些函數實現了同樣的功能
itoa不是一個標准的ANSI C函數但是是C的stdlib.h中的一個函數。所以它不被包括在unix機器上的LibC中。我們可以使用標准的sprintf函數來代替:
sprintf(charY,“%d”,intX);
3. 是用%X來轉換一個十六進制數
int intNum;
sscanf(“ffff”,“%X”,&Num);
lr_output_message(“%d”,intNum); //打印65535 ,ffff的整數值
4. 從文本中提取數字的規則
如果第一個字符不是數字或者為空,atoi返回0,即“e24”會返回0
atoi轉換一個非數字的字符會返回組成這個字符的數字,如“-3.2”返回-3.0。“123XXX345”返回123。
5. LoadRunner腳本中的參數必須轉換成C字符串。有兩種方式來轉化LR的參數為C語言的數字。
i = atoi( lr_eval_string("{pX}") );
sprintf( intX, "%d", lr_eval_string("{pX}") );
6. 參數的算術運算
LoadRunner沒有提供對參數的算術運算的函數。所以LR的參數必須:
1) 轉換成C的整數
2) 使用C的函數來運算最后返回一個C的字符串
3) 把返回的字符串保存成參數
char cBuf[10];
int i;
// 1. Evaluate parameter into a C integer:
i = atoi( lr_eval_string("{pNum_in}") );
// 2. Do the math and output the result to a C string:
sprintf( cBuf, "%d", i+1);
// 3. Save the string as a parameter to be passed on:
lr_save_string( cBuf, "pNum_out");
//Print out the parameter value after incrementing it.
lr_message("**** Parameter from %s to %s",
lr_eval_string("{pNum_in}"));
lr_eval_string("{pNum_out}"));
zibeike注:除了對於數字類型的參數的運算之外,對於文本形式的參數的操作,可以參考我的另一篇文章的內容:http://www.51testing.com/?34866/action_viewspace_itemid_75592.html
二.字符串操作
在C語言中,字符串是固定長度的,因為他們本身由獨立的字符組成的字符數組。數組是只讀的。任何修改字符串長度的函數調用都會報錯:
Error: "C interpreter runtime error - memory violation error during replay.
在LoadRunner的as_web.h庫中的字符串函數可以使用“prototyping”聲明的方式讀寫內存:
char *strtok(char *, char *); // tokenizer prototype
char *strstr(char *, char *); // substring prototype
char *strdup(char *); // String duplication prototype
float atof(); // alpha to return float datatype
#include "as_web.h"
char *strtok(char *, char *); // prototype function call.
ActionX()
{
char aBuffer[256]; // input string to be parsed.
char *cToken; // individual token from strtok.
char cSeparator[] = " "; // blank separator.
int i; // incrementer
char val[3][20]; // output array of strings.
char modified_val[20];
// Create a parameter named pDate:
lr_save_string("January 2, 2001", "pDate");
// Put parameter into a string buffer:
strcpy( aBuffer,lr_eval_string("{pDate}"));
// Show the buffer for debugging:
lr_output_message("%s\n",aBuffer);
// get first word (to the first blank):
cToken = strtok( aBuffer,cSeparator);
i = 1;
if(!token) { // first token was not found:
lr_output_message("No tokens found in string!");
return( -1 );
} else {
while( cToken != NULL) { // tokens are not NULL:
lr_output_message("Token=%s", cToken);
// Stuff in another array:
strcpy( val[i], cToken );
// Get next token:
cToken =strtok( NULL, cSeparator);
i++; // increment
}
lr_output_message("Val #1 is: %s", val[1]);
lr_output_message("Val #2 is: %s", val[2]);
lr_output_message("Val #2 is: %s", val[3]);
strncpy( modified_val, val[2], 1 );
modified_val[2] = '\0';
while (modified_val[2] != NULL) {
lr_output_message("===>%s", modified_val);
modified_val[2] = strtok(NULL, " ");
}
}
return 0;
}
strcat連接兩個字符串
strchr返回指向第一個要查找的字符出現的位置的指針
strcmp比較兩個字符
strcpy復制字符串到另一個
stricmp執行一個大小寫敏感的比較
其他還有strdup,strncat,strncpy,strnicmp,strrchr,strset,strspn,strstr等字符串操作的函數。
zibeike注:關於更多字符串操作的腳本編寫,可以參考我的另一篇文章:
http://www.51testing.com/?34866/action_viewspace_itemid_75428.html
zibeike翻譯自:http://www.wilsonmar.com/1lrscrīpt.htm