一,數(shù)據(jù)類型轉(zhuǎn)換
沒有使用過C編程的LoadRunner腳本編寫者會(huì)發(fā)現(xiàn)在數(shù)據(jù)類型轉(zhuǎn)化方面比較困難。下面介紹這方面的知識(shí)。
1. 相似函數(shù)的輸出在不同的位置
象很多C函數(shù)一樣,使用atoi函數(shù)的結(jié)果即為返回值
如intResult = atoi( charY );
而:itoa的返回結(jié)果為第二個(gè)參數(shù)。
itoa( intX, charY, 10);
第一個(gè)參數(shù)是需要轉(zhuǎn)換的數(shù)字,第二個(gè)參數(shù)是轉(zhuǎn)換后存儲(chǔ)的字符數(shù)組,需要注意的是數(shù)組必須定義為固定的長度,如:char chary[20];
數(shù)組的最大長度為32064(32K),否則會(huì)出現(xiàn)“too many variables”編譯錯(cuò)誤。
如果定義為變長的字符串如char *charY,則程序會(huì)出錯(cuò)。
第三個(gè)參數(shù)不是數(shù)組的長度,而是數(shù)字的基數(shù),10進(jìn)制是最常用的,其他還有二進(jìn)制,八進(jìn)制,十六進(jìn)制。
2. 有一些函數(shù)實(shí)現(xiàn)了同樣的功能
itoa不是一個(gè)標(biāo)準(zhǔn)的ANSI C函數(shù)但是是C的stdlib.h中的一個(gè)函數(shù)。所以它不被包括在unix機(jī)器上的LibC中。我們可以使用標(biāo)準(zhǔn)的sprintf函數(shù)來代替:
sprintf(charY,“%d”,intX);
sprintf
Writes formatted output to a string.
3. 是用%X來轉(zhuǎn)換一個(gè)十六進(jìn)制數(shù)
int intNum;
sscanf(“ffff”,“%X”,&Num);
lr_output_message(“%d”,intNum); // 打印65535 ,ffff的整數(shù)值
sscanf
Reads formatted input from a string.
4. 從文本中提取數(shù)字的規(guī)則
如果第一個(gè)字符不是數(shù)字或者為空,atoi返回0,即“e24”會(huì)返回0
atoi 轉(zhuǎn)換一個(gè)非數(shù)字的字符會(huì)返回組成這個(gè)字符的數(shù)字,如“-3.2”返回-3.0。“123XXX345”返回123。
Converts a string to an integer value.
atoi reads the initial portion of the string only, by stopping at the first non-numerical character.
5. LoadRunner腳本中的參數(shù)必須轉(zhuǎn)換成C字符串。有兩種方式來轉(zhuǎn)化LR的參數(shù)為C語言的數(shù)字。
i = atoi( lr_eval_string("{pX}") );
sprintf( intX, "%d", lr_eval_string("{pX}") );
lr_eval_string
Returns the string argument after evaluating embedded parameters.
The lr_eval_string function returns the input string after evaluating any embedded parameters. If string argument contains only a parameter, the function returns the current value of the parameter.
Embedded parameters must be in brackets.
6. 參數(shù)的算術(shù)運(yùn)算
LoadRunner沒有提供對參數(shù)的算術(shù)運(yùn)算的函數(shù)。所以LR的參數(shù)必須:
1) 轉(zhuǎn)換成C的整數(shù)
2) 使用C的函數(shù)來運(yùn)算最后返回一個(gè)C的字符串
3) 把返回的字符串保存成參數(shù)
view plaincopy to clipboardprint?
char cBuf[10];
int i;
// 1. 轉(zhuǎn)換成C的整數(shù):
i = atoi( lr_eval_string("{pNum_in}") );
// 2. 使用C的函數(shù)來運(yùn)算最后返回一個(gè)C的字符串:
sprintf( cBuf, "%d", i+1);
// 3.把返回的字符串保存成參數(shù):
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}"));
char cBuf[10];
int i;
// 1. 轉(zhuǎn)換成C的整數(shù):
i = atoi( lr_eval_string("{pNum_in}") );
// 2. 使用C的函數(shù)來運(yùn)算最后返回一個(gè)C的字符串:
sprintf( cBuf, "%d", i+1);
// 3.把返回的字符串保存成參數(shù):
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注:除了對于數(shù)字類型的參數(shù)的運(yùn)算之外,對于文本形式的參數(shù)的操作,可以參考我的另一篇文章的內(nèi)容:http://www.51testing.com/?34866/action_viewspace_itemid_75592.html
二.字符串操作
在C語言中,字符串是固定長度的,因?yàn)樗麄儽旧碛瑟?dú)立的字符組成的字符數(shù)組。數(shù)組是只讀的。任何修改字符串長度的函數(shù)調(diào)用都會(huì)報(bào)錯(cuò):
Error: "C interpreter runtime error - memory violation error during replay.
在LoadRunner的as_web.h庫中的字符串函數(shù)可以使用“prototyping”聲明的方式讀寫內(nèi)存:
char *strtok(char *, char *); // tokenizer prototypechar *strstr(char *, char *); // substring prototypechar *strdup(char *); // String duplication prototypefloat 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]; // 創(chuàng)建一個(gè)參數(shù)pDate: lr_save_string("January 2, 2001", "pDate"); // 把參數(shù)放到字符串緩沖Put parameter into a string buffer: //strcpy:Copies one string to another. //lr_eval_string:Returns the string argument after evaluating embedded parameters. strcpy( aBuffer,lr_eval_string("{pDate}")); //在調(diào)試中顯示這個(gè)buffer Show the buffer for debugging: //lr_output_message:Sends a message to the log file and Output window lr_output_message("%s\n",aBuffer); // get first word (to the first blank): //strtok:Returns a token from a string delimited by specified characters. 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 連接兩個(gè)字符串
strchr 返回指向第一個(gè)要查找的字符出現(xiàn)的位置的指針
strcmp 比較兩個(gè)字符
strcpy 復(fù)制字符串到另一個(gè)
stricmp 執(zhí)行一個(gè)大小寫敏感的比較
其他還有strdup,strncat,strncpy,strnicmp,strrchr,strset,strspn,strstr等字符串操作的函數(shù)。
zibeike注:關(guān)于更多字符串操作的腳本編寫,可以參考我的另一篇文章:
http://www.51testing.com/?34866/action_viewspace_itemid_75428.html
zibeike翻譯自:http://www.wilsonmar.com/1lrscrīpt.htm
LoadRunner中常用的字符串操作函數(shù)有:
strcpy(destination_string, source_string);
strcat(string_that_gets_appended, string_that_is_appended);
atoi(string_to_convert_to_int); //returns the integer value
itoa(integer_to_conver_to_string, destination_string, base); // base is 10
strcmp(string1, string2); // returns 0 if both strings are equal
對各函數(shù)的定義:
strcpy( ):拷貝一個(gè)字符串到另一個(gè)字符串中.
strcat( ):添加一個(gè)字符串到另一個(gè)字符串的末尾。
strcmp( ):比較兩個(gè)字符串,如果相等返回0。
atoi():轉(zhuǎn)換一個(gè)ASCII字符串為一個(gè)整型。
itoa():根據(jù)給定的進(jìn)制,轉(zhuǎn)換一個(gè)整型數(shù)據(jù)為ASCII字符串
下面的例子使用了上面這些函數(shù):
view plaincopy to clipboardprint?
Actions()
{
char MyString1[20] = "";
char MyString2[20] = "";
char MyString3[20] = "Mercury2";
char Cstring[10] = "12345";
int Cint;
// MyString1 is empty
//
lr_output_message(">>>>>>>>>> MyString1 = %s",MyString1);
// copy "Mercury1" into MyString1
//
strcpy(MyString1,"Mercury1");
// Now MyString1 contains "Mercury1"
//
lr_output_message(">>>>>>>>>> MyString1 = %s",MyString1);
// Copy MyString3 into MyString2
//
lr_output_message(">>>>>>>>>> MyString2 = %s",MyString2);
strcpy(MyString2,MyString3);
lr_output_message(">>>>>>>>>> MyString2 = %s",MyString2);
// Catenate MyString2 to MyString1
//
strcat(MyString1,MyString2);
lr_output_message(">>>>>>>>>> MyString1 = %s",MyString1);
// Cstring is converted to integer Cint
//
lr_output_message(">>>>>>>>>> Cstring = %s",Cstring);
Cint = atoi(Cstring);
lr_output_message(">>>>>>>>>> Cint = %d",Cint);
// Cint is converted to string
Cint = 100;
itoa(Cint,Cstring,10);
lr_output_message(">>>>>>>>>> Cstring = %s",Cstring);
return 0;
}
Actions()
{
char MyString1[20] = "";
char MyString2[20] = "";
char MyString3[20] = "Mercury2";
char Cstring[10] = "12345";
int Cint;
// MyString1 is empty
//
lr_output_message(">>>>>>>>>> MyString1 = %s",MyString1);
// copy "Mercury1" into MyString1
//
strcpy(MyString1,"Mercury1");
// Now MyString1 contains "Mercury1"
//
lr_output_message(">>>>>>>>>> MyString1 = %s",MyString1);
// Copy MyString3 into MyString2
//
lr_output_message(">>>>>>>>>> MyString2 = %s",MyString2);
strcpy(MyString2,MyString3);
lr_output_message(">>>>>>>>>> MyString2 = %s",MyString2);
// Catenate MyString2 to MyString1
//
strcat(MyString1,MyString2);
lr_output_message(">>>>>>>>>> MyString1 = %s",MyString1);
// Cstring is converted to integer Cint
//
lr_output_message(">>>>>>>>>> Cstring = %s",Cstring);
Cint = atoi(Cstring);
lr_output_message(">>>>>>>>>> Cint = %d",Cint);
// Cint is converted to string
Cint = 100;
itoa(Cint,Cstring,10);
lr_output_message(">>>>>>>>>> Cstring = %s",Cstring);
return 0;
}
//To send an error message to the LoadRunner output window or Application Management agent log, use the lr_error_message function. It is not recommended that you send a message to the output window or agent log in the middle of a transaction, as it will lengthen the execution time. To send a message to the Vuser execution log or Application Management Web site, but not to the Output window, use lr_log_message.