LR Java script writing method

  Before a bank I have come across a performance interface script written in java, because of recent projects also need to write java interface performance test scripts, look stupid to force his head, a little do not know where to start. Then checked the Internet relevant information, but it is a bit summed up what common share with you ha ~

  First, create a Java Vuser script, they need to be clear script project jar package, and then in the classpath Run-time Setting Setting --Java environment LoadRunner, additions local jdk bin / lib and following dt.jar tools.jar packages and packages required for some of the project jar package. (Note: I am using a version of the LR12 can support jdk1.8; if LR11, supports up to jdk version 1.6)

 

 

   Secondly, in the script, import the required import their own bag. E.g:

    import lrapi.lr;
    import java.text.SimpleDateFormat;
    import java.util.Date;

  In particular script, a script for example upload pictures for your reference, some may not apply, but there are some methods in common, which includes obtaining the current time, the interception of the contents of fields, judgment and affairs.

 1 /*
 2  * LoadRunner Java script. (Build: _build_number_)
 3  * 
 4  * Script Description: 
 5  *                     
 6  */
 7 
 8 import lrapi.lr;
 9 import com.client.SunEcmClientApi;
10 import com.client.bean.ClientBatchBean;
11 import com.client.bean.ClientBatchFileBean;
12 import com.client.bean.ClientBatchIndexBean;
13 import com.client.bean.ClientFileBean;
14 import com.client.impl.SunEcmClientSocketApiImpl;
15 import java.text.SimpleDateFormat;
16 import java.util.Date;
17 
18 public class Actions
19 {
20 
21     public int init() throws Throwable {
22         return 0;
23     }//end of init
24     
25     public int action() throws Throwable {
26 
27             SunEcmClientApi clientApi=new SunEcmClientSocketApiImpl("188.177.155.87", 8021);
28             String busiNo="TEST02-"+System.currentTimeMillis();
29 
30             Date date = new Date();
31             SimpleDateFormat sim=new SimpleDateFormat("yyyyMMdd");//该日期格式例如20200116
32             String dateStr=sim.format(date);
33             //lr.message("#######当前时间:["+dateStr+"]#######");
34             
35             ClientBatchBean batchBean=new ClientBatchBean();
36             String rsg="";
37             batchBean.setModelCode("TEST");
38             batchBean.setUser("uesr");
39             ClientBatchIndexBean batchIndexBean=new ClientBatchIndexBean();
40        batchIndexBean.addCustomMap("BUSI_SERIAL_NO", busiNo);
         batchIndexBean.addCustomMap("BUSI_START_DATE", dateStr); 41 ClientBatchFileBean batchFileBean=new ClientBatchFileBean(); 42 batchFileBean.setFilePartName("TEST"); 43 44 lr.start_transaction("01_上传"); 45 46 for(int i=1; i<3; i++){ 47 ClientFileBean fileBean=new ClientFileBean(); 48 fileBean.setFileName("E:/CSS/ceshi"+i+".jpg"); 49 fileBean.setFileFormat("jpg"); 51 fileBean.addOtherAtt("FILE_CN_NAME", "ceshi"+i+".jpg"); 52 fileBean.addOtherAtt("BUSI_FILE_TYPE", "<Type>");//参数化调用 53 fileBean.addOtherAtt("BUSI_FILE_PAGENUM","1"); 54 batchFileBean.addFile(fileBean); 55 } 56 57 batchBean.setIndex_Object(batchIndexBean); 58 batchBean.addDocument_Object (batchFileBean); 59 the try { 60 RSG = clientApi.upload (batchBean, "123" ); 61 is lr.message ( "upload information returned call ####### [" + rsg + "] # ###### " ); 62 is string = rsg.split code (" << >> :: ") [0 ]; // string shear extracted character string SUCCESS 63 is // lr.message (" # cODE ######: [ "+ + code"] ####### "); 64 IF (code.equals (" SUCCESS " )) { 65 lr.end_transaction (" 01_ upload " , LR .PASS); 66 }else{ 67 lr.end_transaction("01_上传",lr.FAIL); 68 } 69 } catch (Exception e) { 70 e.printStackTrace(); 71 lr.end_transaction("01_上传",lr.FAIL); 72 lr.message(" 抛错信息 : " + e.toString() ); 73 } 74 75 76 return 0; 77 }//end of action 78 79 80 public int end() throws Throwable { 81 return 0; 82 }//end of end 83 84 85 86 }

Guess you like

Origin www.cnblogs.com/zwh-Seeking/p/12199834.html