LoadRunner-使用web_get_int_property、关联、file操作函数进行文件下载保存

上一篇 / 下一篇  2015-11-30 15:00:15 / 个人分类:LoadRunner


LoadRunner进行文件下载保存,主要利用如下函数

一、web_get_int_property函数
1、函数用途
记录http响应的信息
2、函数属性
1)HTTP_INFO_RETURN_CODE:        返回http代码
2)HTTP_INFO_DOWNLOAD_SIZE:    返回页面字节数
3)HTTP_INFO_DOWNLOAD_TIME:    返回页面下载时间

二、web_reg_save_param函数
1、函数用途
关联函数,保存文件下载信息

三、file操作函数
1、函数用途
1)fopen:文件打开函数
2)fwrite:文件内容写入函数
3)fclose:文件关闭函数


代码如下:

    int iflen; //文件大小
    long lfbody;  //响应数据内容大小
    int HttpRetCode; //HTTP返回码
    
    ......
   
    web_set_max_html_param_len("1024000");

    //将响应信息存放到fcontent变量,SEARCH范围为BODY
    web_reg_save_param("fcontent","LB=","RB=","SEARCH=BODY",LAST);

    web_link("下载文件",
        "Text=下载文件",
        "Snapshot=t9.inf",
        LAST);
    HttpRetCode = web_get_int_property(HTTP_INFO_RETURN_CODE);
   
    //返回200,表示成功
    if (HttpRetCode == 200)
        lr_output_message("The script. successfully accessed the page");
    else
        lr_error_message("The script. failed to access the page ");

    //获取响应大小
    iflen = web_get_int_property(HTTP_INFO_DOWNLOAD_SIZE);
    lr_output_message("iflen=%d",iflen);

    if(iflen > 0)   
    {
        //以写方式打开文件
        if((lfbody = fopen("E:\\test.rar", "wb")) == NULL)
        {
            lr_error_message("文件操作失败!");
            return -1;
        }
        //写入文件内容
        fwrite(lr_eval_string("{fcontent}"), iflen, 1, lfbody);
        //关闭文件
        fclose(lfbody);
    }

回放脚本,相关信息如下:

Action.c(164): web_set_max_html_param_len was successful      [MsgId: MMSG-26392]
Action.c(167): Registering web_reg_save_param was successful      [MsgId: MMSG-26390]
Action.c(169): Linking to "http://xx.xx.xx/Download/detail/id/5.html", Target Frame=""      [MsgId: MMSG-27994]
Action.c(169): HTML parsing not performed for Content-Type "application/force-download" ("ParseHtmlContentType" Run-Time Setting is "TEXT"). URL="http://31.match.peak.com/Download/detail/id/5.html"      [MsgId: MMSG-26548]
Action.c(169): web_link("下载文件") was successful, 592497 body bytes, 418 header bytes      [MsgId: MMSG-26386]
Action.c(173): web_get_int_property was successful      [MsgId: MMSG-26392]
Action.c(177): The script. successfully accessed the page
Action.c(184): web_get_int_property was successful      [MsgId: MMSG-26392]
Action.c(185): iflen=592915
Action.c(202): Notify: Transaction "xiazai" ended with "Pass" status (Duration: 0.7112 Wasted Time: 0.1642).


细心的你,也许会发现:
Action.c(169): web_link("下载文件") was successful, 592497 body bytes, 418 header bytes
Action.c(185): iflen=592915
下载保存的文件,有592915字节,而实际文件body只有592497字节,差的418正好为header字节数

原因是:
iflen = web_get_int_property(HTTP_INFO_DOWNLOAD_SIZE);
iflen为HTTP_INFO_DOWNLOAD_SIZE = header + body

所以正确的fwrite写入大小应为:fcontent的大小
web_reg_save_param("fcontent","LB=","RB=","SEARCH=BODY",LAST);

但strlen(lr_eval_string("{fcontent}")),无法取到fcontent的大小
因为:fcontent为二进制数据,strlen只能获取以'\0'为结束符的字符串的长度;

没有更好的方法,暂且用lflen代替body长度;

对比过LR保存的文件和真实下载的文件,只在结尾处,多了NUL,即空;正常打开时也不影响。
(注:测试使用的文件为RAR文件,可正常解压,解压后的doc文件大小相同)

TAG: 使用 file 下载 文件 LoadRunner 操作 关联 函数 进行 保存

 

评分:0

我来说两句

Open Toolbar