zoukankan      html  css  js  c++  java
  • 性能测试学习第三天-----loadrunner接口测试&中文乱码处理

    loadrunner 接口测试:   get、post(3种参数格式)、cookie及token处理、加密接口、webservice、socket、文件上传接口、文件下载接口     &  中文乱码处理

    1.get请求

    Action()
    {
        //开始事务--get请求
        lr_start_transaction("get");
    
        //关联函数 获取接口返回信息
        web_reg_save_param("msg",
            "LB=message":"",
            "RB="",
            "Ord=1",
            LAST);
    
        //get请求
        web_url("get", 
        "URL=http://localhost:8080/pinter/com/getSku?id={num}", 
        LAST ); 
    
        // strcmp函数用来判断两个字符串是否相等,如果函数返回值==0,代表两个字符串相等
        if (strcmp(lr_eval_string("{msg}") ,"success") == 0) {        
            lr_end_transaction("get", LR_PASS);
        }else{        
            lr_end_transaction("get", LR_FAIL);
        }
        return 0;
    }

    2.post请求--参数为key=value形式

    Action()
    {    
        lr_start_transaction("post-1");
    web_reg_save_param("msg", "LB=message":"", "RB="", "Ord=1", LAST); web_custom_request("post-1", "Method=POST", "URL=http://localhost:8080/pinter/com/login", "Body=userName=admin&password=1234", LAST ); if (strcmp(lr_eval_string("{msg}") ,"success") == 0) { lr_end_transaction("post-1", LR_PASS); }else{ lr_end_transaction("post-1", LR_FAIL); } return 0; }

    3.post请求--参数为json形式

    Action()
    {    
        lr_start_transaction("post-2");
    
        web_reg_save_param("msg",
            "LB=message":"",
            "RB="",
            "Ord=1",
            LAST);
    
        // json接口都需要添加一个content-type的信息头
        web_add_header("Content-type","application/json");
    
        web_custom_request("post-2", "Method=POST", 
    
        "URL=http://localhost:8080/pinter/com/register", 
    
        "Body={"userName":"test","password":"1234","gender":1,"phoneNum":"110","email":"beihe@163.com","address":"北京市"}", 
    
        LAST ); 
    
        // 把服务器返回的utf-8格式的数据,从utf-8转换为system_locale,并且把转换后的数据保存到一个新的web参数afterMsg
        lr_convert_string_encoding(lr_eval_string("{msg}") ,LR_ENC_UTF8 ,LR_ENC_SYSTEM_LOCALE ,"afterMsg" );
    if (strcmp(lr_eval_string("{afterMsg}") ,"注册成功") == 0) {
    lr_end_transaction("post-2", LR_PASS); }else{ lr_end_transaction("post-2", LR_FAIL); } return 0; }

    4.post请求:参数为data={}格式

    Action()
    {
        lr_start_transaction("post-3");
    
        web_reg_save_param("msg",
            "LB=message":"",
            "RB="",
            "Ord=1",
            LAST);
    web_custom_request("post-3", "Method=POST", "URL=http://localhost:8080/pinter/com/buy", "Body=param={"skuId":123,"num":10}", LAST ); if (strcmp(lr_eval_string("{msg}") ,"success") == 0) { lr_end_transaction("post-3", LR_PASS); }else{ lr_end_transaction("post-3", LR_FAIL); } return 0; }

    5.需要cookie的接口

     两种解决方案:

      方案一:loadrunner会自动处理cookie,只需在前面增加登录接口,后续请求会自动带上cookie;

      方案二:通过抓包获取cookie后,存入参数文件中,后续请求增加web_add_cookie(),增加cookie参数信息。

    6.需要token的接口

      通过抓包获取token后,存入参数文件中,后续请求增加web_add_header(),增加token参数信息。

    7.加密的接口,例如MD5

       1. loadrunner脚本中需要调用外部函数( CMd5 )

      1、将外部函数.h文件添加到LR中

      2、在globals.h文件中引入添加的头文件,如#include “md5.h”
      3、在脚本中直接调用对应的函数即可,需要注意,C语言函数返回的都是C语言参数,如果想在
      LR的web函数中使用,需要进行转换

       2.例如:  {"phoneNum":"123434","optCode":"testfan","timestamp":"1211212","sign":"fdsfdsaafsasfas"}

      其中,sign字段是按照特定算法进行加密后的数据,接口的签名算法为:sign=Md5(phoneNum+ optCode+ timestamp),签名过程中涉及到的C语言函数字符串拼接

    char str[50];
    web_save_timestamp_param("tStamp", LAST );  // 保存当前时间戳 memset(str,0,sizeof(str)); // 清除str数组,重置内存,从0开始到数组最大长度。如果不清除,多次运行时str会累计
    strcat(str,"123434");   //字符串拼接
    strcat(str,"testfan");
    strcat(str,"tStamp");

    lr_save_string(CMd5(str),"md5sign") ; // 转换C语言参数为 web类型参数
    web_add_header("Content-type","application/json");
    web_custom_request("sign", 
    "Method=POST",
    "URL=http://localhost:8080/pinter/com/buy",
    "Body={"phoneNum":"123434","optCode":"testfan","timestamp":"{tStamp}","sign":"{md5sign}"}",
    LAST );

    8.webservice接口: 可以直接使用http协议post请求来测试

       使用web_custom_request函数:http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
       接口描述:http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx
       要点

           1、添加header Content-type:text/xml
           2、请求报文为xml格式,直接放在body中,报文内容可以查看接口文档,或者通过soupUI工具导入wsdl地址,可以看到请求报文
           3、使用bejson压缩请求报文后,对引号转义;

            

           4、使用web_custom_request函数

    Action()
    {
        web_add_header("Content-type","text/xml");
    
        web_custom_request("webservice", "Method=POST", 
        "URL=http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx", 
        "Body=<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><getMobileCodeInfo xmlns="http://WebXml.com.cn/"><mobileCode>139{num}</mobileCode><userID></userID></getMobileCodeInfo></soap:Body></soap:Envelope>", 
        LAST );
        return 0;
    } 

     9.socket接口

    #include "lrs.h"
    Action()
    {
        lrs_set_recv_timeout2(0,0);
        
        lr_start_transaction("tcp");
    
        // 创建一个tcp链接
        lrs_create_socket ("socket0", "TCP", "RemoteHost=127.0.0.1:8888", LrsLastArg);
    
        // 发送数据
        lrs_send ("socket0", "buf0", LrsLastArg );
    
        // 接受数据
        lrs_receive ("socket0", "buf1", LrsLastArg );
    
        // 关联函数
        //lrs_save_param ("socket0",NULL,"RecivedData", 3,7);
        
        lrs_save_searched_string("socket0",NULL,"RecivedData","LB/BIN=|",NULL, 2,2,2);
    
        if (strcmp(lr_eval_string("<RecivedData>") , "SUCCESS") == 0) {        
            lr_end_transaction("tcp", LR_PASS);
        }else{        
            lr_end_transaction("tcp", LR_FAIL);
        }
    
        // 关闭链接
        lrs_close_socket ("socket0");
        return 0;
    }

    10. 文件上传接口

    Action()
    {    
        web_reg_save_param("msg",
            "LB=",
            "RB=",
            "Ord=1",
            "Search=Body",
            LAST);
    
        //web_add_header("Content-type","multipart/form-data");
        web_submit_data("Attachments", 
        "Action=http://localhost:8080/pinter/file/api/upload", 
        "Method=POST", 
        "EncType=multipart/form-data", 
        "TargetFrame=", 
        "RecContentType=text/html", 
        "Snapshot=t5.inf", 
        "Mode=HTML", 
        ITEMDATA, 
            "Name=file", 
            "Value=C:\username.dat", 
            "File=yes", 
        ENDITEM, 
        LAST ); 
        return 0;
    }

    11.文件下载接口

    Action()
    {
        int size;
    lr_start_transaction("download");
    web_url("get", "URL=http://localhost:8080/pinter/file/api/download?id={fileId}", LAST );
      size = web_get_int_property(HTTP_INFO_DOWNLOAD_SIZE); lr_output_message("下载的字节数总大小为:%d", size); if (size > atoi(lr_eval_string("{filesize}"))) { lr_end_transaction("download", LR_PASS); }else{ lr_end_transaction("download", LR_FAIL); } return 0; }

    中文乱码处理

    录制产生的脚本中显示为乱码:-- 录制时设置-Option-Advance-勾选utf-8
    ◼ 接口返回数据显示为乱码,原因是国内服务器采用UTF-8编码方式,但loadrunner自身使用的是GBK编码方式。
      使用lr_convert_string_encoding来做编码转换
      lr_convert_string_encoding(lr_eval_string(“{recv}”), LR_ENC_UTF8, LR_ENC_SYSTEM_LOCALE, “afterEncode");
    1、 从客户端到服务端提交中文乱码
      LR_ENC_SYSTEM_LOCALE → LR_ENC_UTF8
      注意:中文数据转换后,需要使用strcpy函数去除尾部空字符串
    2、从服务端接收中文乱码
      LR_ENC_UTF8 → LR_ENC_SYSTEM_LOCALE

  • 相关阅读:
    20165326 Linux系统安装及学习
    20165326 学习基础和c语言基础调查
    20165326 我期望的师生关系
    2017-2018-2 20165325 实验四《Android程序设计》实验报告
    20165325《Java程序设计》第九周学习总结
    2017-2018-2 20165325 实验三《Java面向对象程序设计》实验报告
    20165325 2017-2018-2 《Java程序设计》结对编程_第二周:四则运算
    20165325 2017-2018-2 《Java程序设计》 第八周学习总结
    20165325 2017-2018-2 《Java程序设计》结对编程_第一周:四则运算
    20165325 2017-2018-2 《Java程序设计》第七周学习总结
  • 原文地址:https://www.cnblogs.com/qingyuu/p/11263297.html
Copyright © 2011-2022 走看看