个人网站: www.7dtest.com 7点测试群:(61369656)------(77273408)------(35710365)------(9410090)

LR中常用的C函数(已更新,以后再更新)

上一篇 / 下一篇  2007-01-22 17:46:53 / 个人分类:Zee的生活

查看( 4458 ) / 评论( 34 )
原文见:http://blog.csdn.net/zeeslo.

1        strcat

char *strcat ( char *to, const char *from );

功能:链接两个字符串。

例子:

这个例子是用strcat链接字符串:zee和slo@hotmail.com

脚本如下:

     char test[1024], *a = "slo@hotmail.com";

     strcpy(test, "zee");

     strcat(test, a);

     lr_output_message("We can see %s",test);


运行后在executon log中看到如下语句:

Starting action Action.
Action.c(16): We can seezeeslo@hotmail.com

2        strchr
char *strchr ( const char *string, int c );

功能:返回字符串中指定字符后面的字符串。

例子:

这个例子是返回第一个出现e字符以后所有的字符,和最后一次出现e字符以后所有的字符。

脚本如下:
     char *string = "Zee is a tester";
     char *first_e, *last_e;

     first_e = (char *)strchr(string, 'e');
     lr_output_message("We can see the first occurrence of e: %s",first_e);
     last_e = (char *)strrchr(string, 'e');
     lr_output_message("We can see the last occurrence of e: %s", last_e);

运行后在executon log中看到如下语句:
Starting action Action.
Action.c(12): We can see the first occurrence of e: ee is a tester
Action.c(14): We can see the last occurrence of e: er
3        Strcmp&stricmp
int strcmp ( const char *string1, const char *string2 );大小写敏感。
int stricmp ( const char *string1, const char *string2 );大小写不敏感。

功能:比较字符串。

例子:

按是否区分大小写对比两个字符串,并打印出它们的大小关系。

脚本如下:
     int result;
     char tmp[20];
     char string1[] = "We can see the string:ZEE";
     char string2[] = "We can see the string:zee";

     result = strcmp( string1, string2 ); /*区分大小写,比较字符串 */

     if( result > 0 )
          strcpy( tmp, "大于" );
     else if( result < 0 )
          strcpy( tmp, "小于" );
     else
          strcpy( tmp, "等于" );

     lr_output_message( "strcmp: String 1  %s string 2", tmp );

     result = stricmp( string1, string2 ); /* 不区分大小写,比较字符串 */

     if( result > 0 )
          strcpy( tmp, "大于" );
     else if( result < 0 )
          strcpy( tmp, "小于" );
     else
          strcpy( tmp, "等于" );

     lr_output_message( "stricmp: String 1 %s string 2", tmp );     

运行后在executon log中看到如下语句:
Starting action Action.
Action.c(22): strcmp: String 1  小于 string 2
Action.c(33): stricmp: String 1 等于 string 2
   
4        strcpy
char *strcpy ( char *dest, const char *source );

功能:复制一个字符串到另一个字符串中。

例子:

复制一个字符串到字符数组中,并打印出来。

脚本如下:

     char test[1024];

     strcpy(test, "what can we see?       ");

     lr_output_message("%s", test);

运行后在executon log中看到如下语句:
Starting action Action.
Action.c(10): what can we see?      
5        Strdup
char *strdup ( const char *string );

复制一个字符串。

例子:

在这个例子中,Vuser的组名被转换为小写字母。但是lr_whoami把组名作为静态buffer返回。这样的buffer不能被操作。如果有操作需要,就复制这个静态buffer。

脚本如下:

     int id;
     char *groupname_static, *groupname;

     /* 从VuGen中得到组名 */
     lr_whoami(&id, &groupname_static, NULL);
     lr_output_message("groupname=%s", groupname_static);

     /*复制这个静态组名以便我们可以操作它  */
     groupname = (char *)strdup(groupname_static);
     groupname = (char *)strlwr(groupname);
     lr_output_message("lower case groupname=%s", groupname);

     free(groupname);
上述脚本用vugen保存为:CHANGE

在controller中运行(设置为总是发送消息)
运行后在log中看到如下语句:
Starting action Action.        [MsgId: MMSG-15919]
Action.c(11): groupname=CHANGE        [MsgId: MMSG-17999]
Action.c(16): lower case groupname=change        [MsgId: MMSG-17999]

[本帖最后由 Zee 于 2006-11-25 13:39 编辑]

TAG: Zee的生活

明天与明天 yours-123 发布于2006-11-20 11:31:18
谢谢楼主,收藏了。
如果Zee还在工作 Zee 发布于2006-11-20 13:38:50
为什么每次我发帖都是很少有人顶呢,有什么意见告诉我,我好改进嘛。
netlrs发布于2006-11-20 14:05:51
不错,顶
不错,顶

有数字转字符,字符转数字的不
dandan的个人空间 dandan 发布于2006-11-20 14:27:21
顶啊............................
wuhuawu09的个人空间 wuhuawu09 发布于2006-11-20 15:04:56
LZ发贴的时间(Zee 发表于 2006-11-20 00:34  )哪会有多少人在线呀?LZ也不要太辛苦到半夜发贴去哟...顶一下.
kycq0102发布于2006-11-20 15:48:59
我录制的代码:全是URL的,一些输入框信息就得不到,比如输入框名、输入框中输入的信息。
kycq0102发布于2006-11-20 15:50:31
我录制的代码:
按钮全是URL的,一些输入框信息就得不到,比如输入框名、输入框中输入的信息。
请问楼主,这是什么原因
wuhuawu09的个人空间 wuhuawu09 发布于2006-11-20 15:52:30
楼上,你用LR录制后看脚本就有那些输入框名,值......
bingbingyang11的个人空间 bingbingyang11 发布于2006-11-20 16:10:08
呵呵,收藏了
不冲动是年轻人吗? wssgily 发布于2006-11-20 20:17:49
顶!
kycq0102发布于2006-11-20 22:27:40
我的问题解决了。哈哈,真爽
如果Zee还在工作 Zee 发布于2006-11-20 23:50:55
能解决问题是最好的。哈哈。
pingglv5858发布于2006-11-21 10:27:39
恩,不错.尤其是strchr,strrchr,这两个容易混淆!
楼主继续!
crystalbelle发布于2006-11-21 11:06:18
谢谢楼主
为什么第5个我的执行结果是Starting action Action.
Action.c(9): groupname=None
Action.c(14): lower case groupname=none
Ending action Action.呢?
我已经保存为别的名字了
pingglv5858发布于2006-11-21 11:28:37
你是不是在controller里运行的呢?
只有在controller里运行才涉及到组名,才会显示你所运行的脚本的组名
只在vugen中是不会显示的
毛毛虫 Joan2005 发布于2006-11-21 17:05:49
不仅能学习了C函数,还了解了lr_whoami  sprintf.
dandan的个人空间 dandan 发布于2006-11-21 17:34:27
好东东,顶一下...........................................
crystalbelle发布于2006-11-22 11:07:33
谢谢pingglv5858,我的问题已经解决了^_^
如果Zee还在工作 Zee 发布于2006-11-25 13:16:41
6        Strlen
size_t strlen ( const char *string );

功能:返回字符串长度(bytes).

例子:

这个例子很简单,就是得到一个字符串中的字符的个数。然后打印出来。

脚本如下:

     char *str = "Zee is a tester";
     unsigned int len;

     len = strlen(str);

     lr_output_message("The  sentence has %d letters",len);

运行后在log中看到如下语句:
Action.c(13): The  sentence has 15 letters
7        Strncat
char *strncat ( char *to_string, const char *from_string, size_t n );

把一个字符串连接到另一个字符串后面。

例子:

在这里,我随便写了两个字符串,用此函数把他们连接起来,并打印出来。

脚本如下:


   char str1[]="Zee is ";
    char str2[]="a tester.";
    lr_output_message("What can we see?");
    lr_output_message("The str1 is %s.",str1);

    strncat(str1,str2,20);
lr_output_message("The str1 is %s.",str1);


运行后在log中看到如下语句:
Action.c(9): What can we see?
Action.c(10): The str1 is Zee is .
Action.c(13): The str1 is Zee is a tester..
注:我们可以看到,没有连接前的str1是:Zee is,连接后的字符串是:Zee is a tester。也可以看看strcat函数。
8        strncmp
int strncmp ( const char *string1, const char *string2, size_t n );

对比两个字符串的前n位。

例子:

对比两个字符串,并把对比结果打印出来。这里我和上面的strcmp一起写。

脚本如下:

    char result;
    char str1[]="Zee is a tester.";
    char str2[]="Zee is a tester.";
    char str3[]="zee is a tester?";
   
    result = strcmp(str1,str2);

    if(result > 0)
         lr_output_message("str1 is greater than str2.");
    else if(result < 0)
         lr_output_message("str1 is less than str2.");
    else
         lr_output_message("str1 is equal to str2.");
   
    result = strncmp( str1, str3 , 30);


    if(result > 0)
         lr_output_message("str1 is greater than str3.");
    else if(result < 0)
         lr_output_message("str1 is less than str3.");
    else
         lr_output_message("str1 is equal to str3.");

运行后在log中看到如下语句:
Starting iteration 1.
Starting action Action.
Action.c(18): str1 is equal to str2.
Action.c(28): str1 is less than str3.
kycq0102发布于2006-11-29 11:24:34
下面是录制代码中的一部分(LR自带的):
web_submit_form("login.pl",
                "Snapshot=t2.inf",
                ITEMDATA,
                "Name=username", "Value=jojo", ENDITEM,
                "Name=password", "Value=bean", ENDITEM,
                "Name=login.x", "Value=0", ENDITEM,
                "Name=login.y", "Value=0", ENDITEM,
                LAST);

有没有函数能获得username的值?
我来说两句

(可选)

我的栏目

日历

« 2024-04-15  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 157699
  • 日志数: 146
  • 图片数: 1
  • 建立时间: 2006-12-05
  • 更新时间: 2012-11-16

RSS订阅

Open Toolbar