在LoadRunner中使用檔案的相關函式
在LoadRunner測試過程中,有時候需要使用檔案,可以封裝出幾個常用的檔案操作函式。
參考:
http://www.jds.net.au/tech-tips/vugen-code-snippets/
/*
Writes a string to the end of a file.
Arguments:
- file_name: Include the full path in the file name, and escape any slashes. E.g. "C://TEMP//output.txt". Note that file does not have to exist beforehand, but directory does.
- string: If attempting to write a single line, include a newline character at the end of the string.
Returns 0 on success. On failure, function will raise lr_error_message and return -1.
*/
int jds_append_to_file(char* file_name, char* string) {
int fp; // file pointer
int rc; // return code
int length = strlen(string);
// Check that file_name is not NULL.
if (file_name == NULL) {
lr_error_message("Error. File name is NULL");
return -1;
}
fp = fopen(file_name, "a"); // open file in "append" mode.
if (fp == NULL) {
lr_error_message("Error opening file: %s", file_name);
return -1;
}
rc = fprintf(fp, "%s", string);
if (rc != length) {
lr_error_message("Error writing to file: %s", file_name);
return -1;
}
rc = fclose(fp);
if (rc != 0) {
lr_error_message("Error closing file: %s", file_name);
return -1;
}
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////
// Checks if a file already exists on the filesystem.
// Arguments:
// - file_name: Include the full path in the file name.
// Returns TRUE (1) if file exists and user has read access to the file, otherwise function returns FALSE (0).
int jds_file_exists(char* file_name) {
int fp; // file pointer
fp = fopen(file_name, "r+"); // open file in read mode. File must already exist.
if (fp == NULL) {
return FALSE;
} else {
fclose(fp);
return TRUE;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
// Saves a file to the hard disk.
// Arguments:
// - file_name: Include the full path in the file name. Note that file must not exist before function is called.
// - file_content: The data to save to the file. Can be binary or string data.
// - file_size: The size/length of the data to save to the file. If it is string data, you can find this using strlen(). If you are saving binary data from a web page, use web_get_int_property(HTTP_INFO_DOWNLOAD_SIZE).
// Returns 0 on success. On failure, function will raise lr_error_message and return -1.
int jds_save_file(char* file_name, void* file_content, unsigned int file_size) {
int rc; // function return code
int fp; // file pointer
// Check input values
if (file_name == NULL) {
lr_error_message("File name is NULL");
return -1;
} else if (file_content == NULL) {
lr_error_message("File content is NULL");
return -1;
} else if (file_size < 1) {
lr_error_message("Invalid file size: %d", file_size);
return -1;
}
// Does the file already exist?
if (jds_file_exists(file_name) == TRUE) {
lr_error_message("File %s already exists", file_name);
return -1;
}
fp = fopen(file_name, "wb"); // open file in "write, binary" mode.
if (fp == NULL) {
lr_error_message("Error opening file: %s", file_name);
return -1;
}
rc = fwrite(file_content, file_size, 1, fp);
if (rc != 1) {
lr_error_message("Error writing to file. Items written: %d", rc);
return -1;
}
rc = fclose(fp);
if (rc != 0) {
lr_error_message("Error closing file: %s", file_name);
return -1;
}
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////
使用的例子:
Action()
{
int is_exists=0;
char *str = "string to write!";
char *file= "D://test.txt";
is_exists = jds_file_exists( file );
//lr_save_int(is_exists,"File_Exist");
//lr_output_message(lr_eval_string("{File_Exist}"));
if(is_exists)
{
jds_append_to_file(file,str);
}
else
{
jds_save_file(file,str,strlen(str));
}
return 0;
}
在LR的幫助文件中,也有關於檔案、目錄操作函式的詳細介紹:
相關文章
- Api函式列表——與檔案相關 (轉)API函式
- Loadrunner C/S關聯函式(LSP)AND(LSSS)使用-案例函式
- 在LoadRunner中轉換字串大小寫的C語言函式字串C語言函式
- 字串的相關函式字串函式
- 學習PHP中的URL相關操作函式PHP函式
- 檔案鎖fcntl()函式的使用函式
- loadrunner常用函式函式
- Oracle中關於函式的使用Oracle函式
- Loadrunner 使用者自定義函式使用[轉]函式
- LoadRunner中檔案操作的一個例子
- 執行緒中的幾個退出相關函式執行緒函式
- 一張C/C++檔案io相關函式mode對應表C++函式
- 物件及函式相關物件函式
- SAP PP相關函式函式
- Python 函式相關Python函式
- 字串相關函式的實現字串函式
- 正規表示式☞相關函式函式
- LR中winsock相關函式彙總函式
- linux使用者相關檔案Linux
- 誰知道在websphere中如何配置rmi相關的安全策略檔案(policy)?Web
- 在CPP檔案中自動新增函式註釋的ADDIN (轉)函式
- 在Excel中如何使用TEXT函式?Excel函式
- 與 RMAN 相關的檔案
- ES6 函式相關函式
- 3.3 數學相關函式函式
- SQL 日期格式相關函式SQL函式
- 函式組:CSAP(BOM 相關)函式
- loadrunner常用函式總結函式
- VC 6中使用不同呼叫規範的函式在符號檔案裡的表示方式函式符號
- Python資料分析--Numpy常用函式介紹(5)--Numpy中的相關性函式Python函式
- 在iOS專案中自動生成函式呼叫關係圖(CallGraph)iOS函式
- 【開發篇sql】 分析函式(一) 評級相關的函式SQL函式
- 檔案下載相關
- 關於在oracle 的group by中使用自定義函式Oracle函式
- Python之函式的相關介紹Python函式
- C語言fgetc()函式:讀檔案函式(由檔案中讀取一個字元)C語言函式字元
- Processing中PImage類和loadImage()、createImage()函式的相關解析函式
- LoadRunner函式學習筆記函式筆記