参考《Search & Replace function forLoadRunner》:
http://ptfrontline.wordpress.com/2009/03/13/search-replace-function-for-lr/
LoadRunner中没有直接的函数支持查找并替换字符串,因此可以封装一个lr_replace函数出来:
// ----------------------------------------------------------------------------
//
// Description:
// Search for and replace text within a string.
//
// Parameters:
// src (in) - pointer to source string
// from (in) - pointer to search text
// to (in) - pointer to replacement text
//
// Returns:
// Returns a pointer to dynamically-allocated memory containing string
// with occurences of the text pointed to by 'from' replaced by with
// the text pointed to by 'to'.
//
// Notes:
// Do not use this directly in scripts unless you are a more advanced
// user who knows C and string handling. See below for the function you
// should use!
//
// ----------------------------------------------------------------------------
char *strReplace(const char *src, const char *from, const char *to)
{
char *value;
char *dst;
char *match;
int size;
int fromlen;
int tolen;
// Find out the lengths of the source string, text to replace, and
// the replacement text.
size = strlen(src) + 1;
fromlen = strlen(from);
tolen = strlen(to);
// Allocate the first chunk with enough for the original string.
value = (char *)malloc(size);
// We need to return 'value', so let's make a copy to mess around with.
dst = value;
// Before we begin, let's see if malloc was successful.
if ( value != NULL )
{
// Loop until no matches are found.
for ( ;; )
{
// Try to find the search text.
match = (char *) strstr(src, from);
if ( match != NULL )
{
// Found search text at location 'match'.
// Find out how many characters to copy up to the 'match'.
size_t count = match - src;
// We are going to realloc, and for that we will need a
// temporary pointer for safe usage.
char *temp;
// Calculate the total size the string will be after the
// replacement is performed.
size += tolen - fromlen;
// Attempt to realloc memory for the new size.
//
// temp = realloc(value, size);
temp = (char *)realloc(value, size);
if ( temp == NULL )
{
// Attempt to realloc failed. Free the previously malloc'd
// memory and return with our tail between our legs.
free(value);
return NULL;
}
// The call to realloc was successful. But we'll want to
// return 'value' eventually, so let's point it to the memory
// that we are now working with. And let's not forget to point
// to the right location in the destination as well.
dst = temp + (dst - value);
value = temp;
// Copy from the source to the point where we matched. Then
// move the source pointer ahead by the amount we copied. And
// move the destination pointer ahead by the same amount.
memmove(dst, src, count);
src += count;
dst += count;
// Now copy in the replacement text 'to' at the position of
// the match. Adjust the source pointer by the text we replaced.
// Adjust the destination pointer by the amount of replacement
// text.
memmove(dst, to, tolen);
src += fromlen;
dst += tolen;
}
else // No match found.
{
// Copy any remaining part of the string. This includes the null
// termination character.
strcpy(dst, src);
break;
}
} // For Loop()
}
return value;
}
// ----------------------------------------------------------------------------
//
// Description:
// Find and replace text within a LoadRunner string.
//
// Parameters:
// lrparam (in) - pointer to LoadRunner Parameter Name
// findstr (in) - pointer to text top search for
// replacestr (in) - pointer to text to use as replacement
//
// Returns:
// Returns an integer. 0=Error, 1=Success.
//
// Example:
// lr_save_string( "This is a small test of the search and replace function", "LRParam");
// lr_replace( "LRParam", "a", "-x-" );
// lr_output_message( "%s", lr_eval_string("{LRParam}") );
//
// ----------------------------------------------------------------------------
int lr_replace( const char *lrparam, char *findstr, char *replacestr )
{
int res = 0;
char *result_str;
char lrp[1024];
// Finalize the LR Param Name
sprintf( lrp, "{%s}", lrparam);
// Do the Search and Replace
result_str = strReplace( lr_eval_string(lrp), findstr, replacestr );
// Process results
if (result_str != NULL )
{
lr_save_string( result_str, lrparam );
free( result_str );
res = 1;
}
return res;
} // EOF
在脚本中引用包括上面代码的头文件“lr_replace.h”,使用lr_replace函数的例子如下所示:
#include "lr_replace.h"
Action()
{
// Store a string into "MyPar" parameter
lr_save_string("This is a string", "MyPar");
// For examples sake, convert it to URL encoded format
web_convert_param( "MyPar",
"SourceEncoding=PLAIN",
"TargetEncoding=URL", LAST);
// Output the current result
lr_output_message("%s", lr_eval_string("{MyPar}"));
// Replace the ? characters with %20
lr_replace("MyPar", "+", "%20" );
// Output new result
lr_output_message("%s", lr_eval_string("{MyPar}"));
return 0;
}
分享到:
相关推荐
LoadRunner中转换字符串到URL编码C代码及转换工具
loadrunner 安装指南(含替换文件和deletelicense
Loadrunner11破解替换文件 lm70.dll,mlr5lprg.dll 和deletelicense.exe,Loadrunner11破解方法 破解简单
LoadRunner 9.5中文帮助文档
如何在LoadRunner中增加QTP脚本.pdf如何在LoadRunner中增加QTP脚本.pdf
本文主要是loadrunner11常用函数的中文解释,包括一些实际使用中的了例子,各函数参数等
在loadrunner中调用dll~
loadrunner中文教程,9.5版本
LoadRunner中文简介 LoadRunner® 是...名用户和实施实时性能监测来确认和查找问题,LoadRunner能够对整个企业架构进行 测试。通过使用LoadRunner,企业能最大限度地缩短测试时间,优化性能和加速应用 系统的发布周期。
详细介绍 为什么要在LoadRunner中进行关联 如何在LoadRunner 脚本中进行关联
通过以模拟上千万用户实施并发负载及实时性能监测的方式来确认和查找问题,LoadRunner能够对整个企业架构进行测试。企业使用LoadRunner能最大限度地缩短测试时间,优化性能和加速应用系统的发布周期。 LoadRunner可...
如何在 LoadRunner 脚本中做关联
MI 官方 Loadrunner 中文教程
LoadRunner 9.5 中文帮助文档 中文文档 中文手册
Loadrunner中参数的设置
LoadRunner11/12中文使用手册,0积分下载,造福大众~
第 1 课介绍 LoadRunner 和测试概念。 第 2 课提供一个测试示例,说明 LoadRunner 工具的功能。 第 3 课至第 6 课说明如何使用 HP Virtual User Generator 创建脚本。 第 7 课至第 9 课说明如何使用 LoadRunner ...
loadrunner11与 loadrunner12 中文 教程,详细,高清
Loadrunner中参数设置详细分析,很详细的