LoadRunner JAVA Vuser接口测试

时间:2024-06-14 22:06:44

注:JDK只支持1.6

1、创建工程Test
2、写个经典的HelloWorld类。
3、Runas---》Java Application运行下
4、将工程下的整个com包拷贝到loadrunner安装目录下的classes目录下。例如我的是:D:\Program Files\HP\LoadRunner\classes
5、新建Virtual User:选Java Vuser
6、生成脚本Vuser_init和vuser_end不用管在Actions里写测试脚本
7、运行脚本结果,看到如图红色部分,说明脚本运行成功了。

示例:

1、JAVA代码:

需要引用json、dom4j外部包

package com.lr.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class CloudTestScr
{

    public static void main(String[] args)
    {
        // TODO Auto-generated method stub
        String get = "";String result = "";
        CloudTestScr CloudTestScr = new CloudTestScr();
        get = "http://was.wondershare.com/v1/?method=app.info.upgrade&app_id=com.wondershare.mobilego&client_sign={6D56BE6F-A0C1-4E81-BA91-1AD25EFA3A9E}&lang=en-US&member_id=T5CD48D5F021EA0507&sku=sku-ween&timestamp=1415865275&key=47342D1BEE153385294760BDDB8A7F49&vc=5817d988bce3a3f9e509e8615d1d7cc4";
        result = getHttpByUrl(get);

        System.out.println(result);
        String test1 = getJosnsString(result, "apps");
        System.out.println(test1);
        String test2 = getJosnsarray(test1, 0);
        System.out.println(getJosnsString(test2, "app_id"));

    }     

    public static String postHttpByUrl(String address, String param)
    {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try
        {
            URL realUrl = new URL(address);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());
            // 发送请求参数
            out.print(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null)
            {
                result += line;
            }
        }
        catch (Exception e)
        {
            System.out.println("发送 POST 请求出现异常!" + e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输出流、输入流
        finally
        {
            try
            {
                if (out != null)
                {
                    out.close();
                }
                if (in != null)
                {
                    in.close();
                }
            }
            catch (IOException ex)
            {
                ex.printStackTrace();
            }
        }
        return result;
    }

    public static String getHttpByUrl(String address, String param)
    {

        String res = "";

        try
        {
            String urlString = address + "?" + param;
            URL url = new URL(urlString);

            java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url
                    .openConnection();

            conn.setDoOutput(true);
            conn.setRequestMethod("GET");
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    conn.getInputStream(), "UTF-8"));
            String line;

            if (in != null)
            {

                while ((line = in.readLine()) != null)
                {

                    res += line;
                }

            }

            in.close();
            conn.disconnect();
        }
        catch (MalformedURLException e)
        {
            System.out.println("get异常");
            e.printStackTrace();
        }
        catch (IOException e)
        {
            System.out.println("get异常");
            e.printStackTrace();

        }

        // System.out.println(res);
        return res;

    }

    public static String getJosnsString(String s, String keyString)
    {
        String value = s, key = "", key2 = keyString;
        JSONObject jsonString;
        JSONArray jsonArray;

        // key值为空,回调结束
        if (keyString.equals(""))
        {
            return s;
        }

        try
        {
            if (value.substring(0, 1).equals("["))
            {
                jsonArray = new JSONArray(value);
                value = jsonArray.get(0).toString().trim();
            }
        }
        catch (JSONException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try
        {
            if (value.substring(0, 1).equals("{"))
            {

                if (keyString.contains("/"))
                {
                    key = keyString.substring(0, keyString.indexOf("/"));
                    key2 = keyString.substring(keyString.indexOf("/") + 1);
                }
                else
                {
                    key = keyString;
                    key2 = keyString;
                }
                jsonString = new JSONObject(value);
                value = jsonString.get(key).toString().trim();

            }
            else
            {
                System.out.println("no json");
            }
        }
        catch (JSONException e)
        {
            // TODO Auto-generated catch block
            // e.printStackTrace();
        }
        if (keyString.contains("/"))
        {
            return getJosnsString(value, key2);
        }
        else
        {
            return value;
        }

    }

    public static String getJosnsarray(String jsonString, int index)
    {
        String value = "";
        JSONArray jString;
        try
        {
            jString = new JSONArray(jsonString);
            value = jString.get(index).toString();
        }
        catch (JSONException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            value = "jsonstring not found key";

        }

        return value;
    }

    public static String getXmlString(String XmlString, String keyString)
    {
        String value = "";
        Document document;
        try
        {
            document = DocumentHelper.parseText(XmlString);
            // Element root = document.getRootElement();
            Element foo = null;
            @SuppressWarnings("unchecked")
            List<Element> list = document.selectNodes(keyString);
            // List list=root.elements("status");
            for (int i = 0; i < list.size(); i++)
            {
                foo = list.get(i);
                value += foo.getText();

            }
            // System.out.print(value);
        }
        catch (DocumentException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return value.trim();
    }

    public static String getString(String httpRes, String keyString)
    {
        String fristchar = "";
        fristchar = httpRes.substring(0, 1);

        if (fristchar.equals("<"))
        {
            return getXmlString(httpRes, keyString);
        }
        else if (fristchar.equals("{"))
        {
            // System.out.println(keyString.substring(keyString.lastIndexOf("/")+1));
            return getJosnsString(httpRes,
                    keyString.substring(keyString.lastIndexOf("/") + 1));
        }
        return "不是合法的输入" + httpRes;

    }

}

LR代码:

/*
 * LoadRunner Java script. (Build: _build_number_)
 *
 * Script Description:
 *
 */

import lrapi.lr;
import com.lr.test.*;

public class Actions
{

    public int init() throws Throwable {
        return 0;
    }//end of init

    public int action() throws Throwable {

         String brand ="<brand>";
        //String email ="email";
            String serial_number ="<serial_number>";
        String address_url ="http://192.168.9.51:8080/MobileGo/interface/index.php/v1/luckydraw?brand="+brand+"&email=jiangyfll@wondershare.cn&serial_number="+serial_number+"&timestamp=144913561&vc=7e62bf6a3457d1342ef203ae688202b";
        //String address_url ="http://192.168.9.51:8080/MobileGo/interface/index.php/v1/luckydraw?brand="+brand+"&email="+email+"&serial_number="+serial_number+"&timestamp=144913561&vc=7e62bf6a3457d1342ef203ae688202b";
        String result = "";

        lr.rendezvous("ReadGO");
        lr.start_transaction("luckydraw");
        lr.start_sub_transaction("winning","luckydraw");
        lr.start_sub_transaction("L_winning","luckydraw");
        lr.start_sub_transaction("participated","luckydraw");
        result = CloudTestScr.getHttpByUrl(address_url);

        //中大奖
        if(!CloudTestScr.getString(result,"award_no").equals("12") && CloudTestScr.getString(result,"result").equals("winning")){
        lr.end_transaction("luckydraw", lr.PASS);
        lr.end_sub_transaction("winning", lr.PASS);
        lr.end_sub_transaction("participated", lr.FAIL);
        lr.end_sub_transaction("L_winning",lr.FAIL);
                lr.error_message(serial_number);

        //幸运奖
        }else if (CloudTestScr.getString(result,"award_no").equals("12") && CloudTestScr.getString(result,"result").equals("winning")){
        lr.end_transaction("luckydraw", lr.PASS);
        lr.end_sub_transaction("winning", lr.FAIL);
        lr.end_sub_transaction("L_winning",lr.PASS);
        lr.end_sub_transaction("participated", lr.FAIL);
        lr.error_message(serial_number);

        //抽过奖
        }else if(CloudTestScr.getString(result,"result").equals("participated")){
        lr.end_transaction("luckydraw", lr.FAIL);
        lr.end_sub_transaction("winning", lr.FAIL);
        lr.end_sub_transaction("L_winning",lr.FAIL);
        lr.end_sub_transaction("participated", lr.PASS);
        lr.error_message(serial_number);
        }else{
        lr.error_message(result);
        lr.end_transaction("luckydraw", lr.FAIL);
        lr.end_sub_transaction("winning", lr.FAIL);
        lr.end_sub_transaction("L_winning",lr.FAIL);
        lr.end_sub_transaction("participated", lr.FAIL);
        }

        return 0;
    }//end of action

    public int end() throws Throwable {
        return 0;
    }//end of end
}